我在listbox上显示歌曲列表我有绑定媒体元素但是我无法在songs.cs文件中获取媒体元素名称实例而无法播放歌曲
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<MediaElement Name="Player" Source="{Binding SongUrl}" AutoPlay="False"/>
<Button Name="Click" Click="Play_Click" Content="Button"/>
<StackPanel Width="150" Height="50">
<TextBlock Text="{Binding SongName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" TextAlignment="Center" Foreground="Red" FontSize="16"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
示例在Songs.cs中:
private void Play_Click(object sender, RoutedEventArgs e)
{
Player.play(); (unable to get Media Element name)
}
答案 0 :(得分:2)
媒体播放器不应该在ItemTemplate中,因为我一次只会播放一个声音(为eaach项目设置媒体元素会很昂贵)。所以你应该把MEdiaaPlayer移到外面并按下按钮点击:
private void Play_Click(object sender, RoutedEventArgs e)
{
Button button=sender as Button;
Player.Source=((Item)button.DataContext).SongUrl
Player.play(); (unable to get Media Element name)
}