0

我在列表框中显示歌曲列表我有绑定媒体元素但我无法在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) 
    }
4

1 回答 1

2

媒体播放器不应该在 ItemTemplate 中,因为我相信您一次只能播放一种声音(为每个项目设置一个媒体元素会很昂贵)。所以你应该把 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) 
}
于 2013-09-17T19:02:21.093 回答