當你在Silverlight 或是Windows phone 應用程式中通過引用Microsoft.Xan.Framework嘗試通過背景任務播放一段音頻檔案或是記錄音頻時. 通常會遇到如下關于FrameworkDispatcher異常資訊:
<a target="_blank" href="http://blog.51cto.com/attachment/201201/141408285.jpg"></a>
一旦嘗試在背景任務記錄或播放一段音頻檔案.總是提示Application沒有調用FrameworkDisplatcher.Update()方法.當你使用XNA對象在一個Silverlight或Windows phone 應用程式時這個異常很正常.在執行播放任務前.因首先檢查在背景是否已經存在該資源任務的占用.如果已經占用.則以對話框的形式來提示使用者是否停止.Check Code:
if (!Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl)
//play Background Music File
在XnaFramework中 Xna架構的事件消息處理機制是放在一個XNA處理的消息隊列中.在一個XNa Application 中實作GAme類. FrameworkDispatcher.Update()方法是自動調用Game.Update處理.
在一個沒有實作GAme類的Windows phone或Silverlight應用程式中.則必須采用手動的方式調用FrameWorkDisplatcher.Update()通過XnaFrameWork消息隊列自己發送消息.
Well 首先添加引用:
using System.Windows.Threading;
using Microsoft.Xna.Framework;
建立一個類實作IApplicationService接口:
public partial class NuanceFunctionDemo_Form : RecognizerListener, IApplicationService
{}
實作接口兩個方法:
public void StartService(ApplicationServiceContext context)
{
_frameworkDispatcherTimer.Start();
}
public void StopService()
_frameworkDispatcherTimer.Stop();
}
而接口調用_frameworkDispatcherTimer則是DispatcherTimer對象的執行個體.完整處理Code:
private DispatcherTimer _frameworkDispatcherTimer;
void NuanceFunctionDemo_Form_Loaded(object sender, RoutedEventArgs e)
this.VoiceType_LP.ItemsSource = voiceDefineTypeList;
this.VoiceType_LP.SelectedIndex = 0;
//textBoxResult.Text = "test one. test two. test three. test four.";
textBoxServerIp.Text = AppInfo.SpeechKitServer;
textBoxServerPort.Text = AppInfo.SpeechKitPort.ToString();
_frameworkDispatcherTimer = new DispatcherTimer();
_frameworkDispatcherTimer.Tick += FrameworkDispatcherTimer_Tick;
_frameworkDispatcherTimer.Interval = new TimeSpan(0, 0, 3);
FrameworkDispatcher.Update();
speechkitInitialize();
App.CancelSpeechKit += new CancelSpeechKitEventHandler(App_CancelSpeechKit);
}
public void StartService(ApplicationServiceContext context)
{
_frameworkDispatcherTimer.Start();
public void StopService()
_frameworkDispatcherTimer.Stop();
void FrameworkDispatcherTimer_Tick(object sender, EventArgs e)
這個異常在Silverlight和Windows phone 是常見的異常. 關于frameworkDispatcher處理方式 請參考MSDN用法.
參考資料:
<a href="http://msdn.microsoft.com/library/ff842408.aspx" target="_blank">Enable Xna Framework Events in Windows phone Application</a>
本文轉自chenkaiunion 51CTO部落格,原文連結:
http://blog.51cto.com/chenkai/763293