WPF MediaElement 应用程序的播放列表
我正在通过构建 WMP 类型的应用程序来学习 C# 和 WPF。下面的代码运行良好,从列表框中选择一部电影在媒体元素中运行它。我遇到的问题是找到一种方法在电影结束后自动开始下一部电影。谢谢。
提供电影列表的 xml 文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
熊 c:\电影\Bear.wmv 蝴蝶 c:\电影\蝴蝶.wmv 湖 c:\电影\湖.wmv
xaml
<Window x:Class="WpfAppPlaylistTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="425">
<Window.Resources>
<XmlDataProvider x:Key="myMoviesXML"
Source="c:\Movies\media1.xml"
XPath="media"
/>
</Window.Resources>
<Grid DataContext="{Binding ElementName=movieList, Path=SelectedItem}">
<ListBox ItemsSource="{Binding Source={StaticResource myMoviesXML}, XPath=//media//movie}" IsSynchronizedWithCurrentItem="True"
Name="movieList" HorizontalAlignment="Right" Width="114" Margin="0,48,12,32">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=title}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<MediaElement Source="{Binding XPath=filename}" LoadedBehavior="Play" Name="mediaElement1" Margin="12,26,136,12" />
</Grid>
I'm learning C# and WPF by building a WMP-type app. The code below runs fine, selecting a movie from the listbox runs it in the media element. The problem I'm having is finding a way to automatically start the next movie after one ends. Thank You.
The xml file that provides a list of movies:
<?xml version="1.0" encoding="ISO-8859-1"?>
Bear
c:\movies\Bear.wmv
Butterfly
c:\movies\Butterfly.wmv
Lake
c:\movies\Lake.wmv
xaml
<Window x:Class="WpfAppPlaylistTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="425">
<Window.Resources>
<XmlDataProvider x:Key="myMoviesXML"
Source="c:\Movies\media1.xml"
XPath="media"
/>
</Window.Resources>
<Grid DataContext="{Binding ElementName=movieList, Path=SelectedItem}">
<ListBox ItemsSource="{Binding Source={StaticResource myMoviesXML}, XPath=//media//movie}" IsSynchronizedWithCurrentItem="True"
Name="movieList" HorizontalAlignment="Right" Width="114" Margin="0,48,12,32">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding XPath=title}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<MediaElement Source="{Binding XPath=filename}" LoadedBehavior="Play" Name="mediaElement1" Margin="12,26,136,12" />
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MediaElement 有一个 MediaEnded 事件,发生这种情况时应该触发该事件。然后,您可以通过编程选择列表中的下一个项目,并播放该文件。
MediaElement has a MediaEnded event that should fire when this happens. Then you can programmably select the next item in the list, and play that file.