我在Windows Phone应用中为背景音乐创建了 MediaElement ,它可以完美播放 现在当我尝试为按钮添加声音时出现问题按下
当我调试时,背景音乐播放,但当我按按钮时,导航我到下一页没有播放我创建的声音,当我从导航页面按 BackButton 时,播放它,为什么?
代码:
private void play_Click(object sender, RoutedEventArgs e)
{
MediaElement Click = new MediaElement();
Click.Source = new Uri ("/Assets/Sounds/press.mp3",UriKind.Relative);
Click.Position = TimeSpan.Zero;
Click.Volume = 1;
LayoutRoot.Children.Add(Click);
Click.Play();
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
}
答案 0 :(得分:0)
private void play_Click(object sender, RoutedEventArgs e)
{
MediaElement Click = new MediaElement();
Click.Source = new Uri ("/Assets/Sounds/press.mp3",UriKind.Relative);
Click.Position = TimeSpan.Zero;
Click.Volume = 1;
LayoutRoot.Children.Add(Click);
Click.Play();
}
你应该处理这个外部按钮点击,
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
为点击添加事件处理程序并单独调用
Click.MediaEnded += Click_Completed;
void Click_Completed(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
}
答案 1 :(得分:0)
或者您可以制作mediaElement的MediaEnded事件,然后您可以调用导航
像这样:
private void play_Click(object sender, RoutedEventArgs e)
{
MediaElement Click = new MediaElement();
Click.Source = new Uri ("/Assets/Sounds/press.mp3",UriKind.Relative);
Click.Position = TimeSpan.Zero;
Click.Volume = 1;
LayoutRoot.Children.Add(Click);
Click.MediaEnded += Click_MediaEnded;
Click.Play();
}
void Click_MediaEnded(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
}