2012-11-15 59 views
1

我已經在Visual Studio 2012最終開發了Windows 8應用程序,但是我遇到了MediaElement的MediaOpened和MediaEnded事件未被觸發的問題。Windows 8商店應用程序MediaElement事件沒有觸發

下面是我的代碼:

public static async void PlaySound(string AudioFile, RoutedEventHandler PlayerEnded) 
    { 
     Uri audioUri = new Uri("ms-appx:///Assets/" + AudioFile); 

     StorageFile audioStorage = await StorageFile.GetFileFromApplicationUriAsync(audioUri); 

     if (audioStorage != null) 
     { 
      var stream = await audioStorage.OpenAsync(Windows.Storage.FileAccessMode.Read); 

      MediaElement player = new MediaElement(); 

      player.AudioCategory = AudioCategory.GameEffects; 

      player.AutoPlay = false; 

      player.MediaEnded += PlayerEnded; 

      player.MediaOpened += new RoutedEventHandler(delegate(Object sender, RoutedEventArgs e) 
       { 
        player.Play(); 
       }); 

      player.SetSource(stream, audioStorage.ContentType); 
     } 
    } 

基本上這將傳遞一個點擊按鈕的聲音文件,播完後將調用this.Frame.Navigate方法切換到另一頁。但是這並不奏效,因爲沒有任何事件被解僱。

如果我設置AutoPlay = true,那麼它表現良好,但事件仍然不會被解僱。

任何人都可以看到什麼是錯的?

感謝您的幫助

回答

相關問題