如何使用wpf将mp3歌曲添加到列表框?

时间:2011-08-25 06:59:25

标签: wpf listbox controls mediaelement

我的解决方案资源管理器中有音乐文件夹..然后我想将这些歌曲添加到列表框控件中,之后我想使用wpf播放媒体元素中列表框中的选定歌曲?
请帮我。 感谢

4 个答案:

答案 0 :(得分:1)

要点击按钮进行播放行为eexplici,请参阅:

Xaml:

<MediaElement x:Name="media" Source="{Binding   
             ElementName=listbox,Path=SelectedItem}" 
             LoadedBehavior="Manual" UnloadedBehavior="Manual"/> 
 <Button Click="Button_Click" Height="27" VerticalAlignment="Bottom"   
         HorizontalAlignment="Left" Width="62">Play</Button>

代码背后: -

private void Button_Click (object sender, RoutedEventArgs e) {
       media.Play ();
}

答案 1 :(得分:0)

  • 您应该实现业务逻辑以浏览您要定位的目录。准备一组项目。将这些绑定到Listbox
  • 要播放歌曲,请将所选项目绑定到MediaElement。

如果您还需要进一步的帮助,我会尝试编译一些简单的解决方案并进行更新。

更新简单解决方案:

的Xaml:

<StackPanel Orientation="Vertical">
    <ListBox ItemsSource="{Binding}" x:Name="fileList"></ListBox>
    <MediaElement x:Name="mediaElement" Source="{Binding ElementName=fileList, Path=SelectedItem}"/>
</StackPanel>

代码背后:

public partial class Window1 : Window {
    ObservableCollection<string> mFileList;

    public Window1 () {
        InitializeComponent ();
        GetFiles(@"..\songs");

        this.DataContext = mFileList;

    }

    private void GetFiles (string folderPath) {
       string[] files = Directory.GetFiles(folderPath);
       mFileList = new ObservableCollection<string> (files);
    }

}

答案 2 :(得分:0)

You need to handle the mediaended event as below :-

<MediaElement x:Name="media" Source="{Binding ElementName=listbox,Path=SelectedItem}"   MediaEnded="media_MediaEnded"
                 ></MediaElement>

Codebehind :-
` private void media_MediaEnded (object sender, RoutedEventArgs e) {
        if (listbox.SelectedIndex < listbox.Items.Count - 1) {
            listbox.SelectedIndex = listbox.SelectedIndex + 1;
        }`

答案 3 :(得分:0)

You need to handle the mediaended event as below :-     
<MediaElement x:Name="media" Source="{Binding ElementName=listbox,Path=SelectedItem}" Margin="0,119,78,64"  MediaEnded="media_MediaEnded"
                 ></MediaElement>

   private void media_MediaEnded (object sender, RoutedEventArgs e) {
        if (listbox.SelectedIndex < listbox.Items.Count - 1) {
            listbox.SelectedIndex = listbox.SelectedIndex + 1;
        }

    }