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

发布于 2024-12-01 13:56:33 字数 91 浏览 1 评论 0原文

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

I have Music folder in my solution explorer..then i want to add that songs to the list box control after that i want to play the selected songs from listbox in the media element using wpf?
Please Help me.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最终幸福 2024-12-08 13:56:33

要使按钮单击上的播放行为变得明确,请参阅:

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 ();
}

To make play behaviour eexplici on a button click , refer this:

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>

Code Behind :-

private void Button_Click (object sender, RoutedEventArgs e) {
       media.Play ();
}
或十年 2024-12-08 13:56:33
  • 您应该实现业务逻辑来浏览您的目标目录。准备一系列物品。将它们绑定到列表框
  • 要播放歌曲,请将所选项目绑定到 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);
    }

}
  • You should implement business logic to browse the directory you are targetting. Prepare a collection of Items. Bind these to Listbox
  • For playing the song, bind selected item to MediaElement.

I will try to compile some simple solution and update if you still need further help.

Updating simple solution:

Xaml:

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

Code Behind:

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);
    }

}
十二 2024-12-08 13:56:33
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;
        }`
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;
        }`
久隐师 2024-12-08 13:56:33
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;
        }

    }
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;
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文