當你在Silverlight 或是Windows phone 應用程序中經過引用Microsoft.Xan.Framework嘗試經過後臺任務播放一段音頻文件或是記錄音頻時. 一般會遇到以下關於FrameworkDispatcher異常信息:sql
一旦嘗試在後臺任務記錄或播放一段音頻文件.老是提示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處理.ide
在一個沒有實現GAme類的Windows phone或Silverlight應用程序中.則必須採用手動的方式調用FrameWorkDisplatcher.Update()經過XnaFrameWork消息隊列本身發送消息.this
Well 首先添加引用:spa
- using System.Windows.Threading;
- using Microsoft.Xna.Framework;
建立一個類實現IApplicationService接口:3d
- public partial class NuanceFunctionDemo_Form : RecognizerListener, IApplicationService
- {}
實現接口兩個方法:orm
- 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)
- {
- FrameworkDispatcher.Update();
- }
- }
這個異常在Silverlight和Windows phone 是常見的異常. 關於frameworkDispatcher處理方式 請參考MSDN用法.blog
參考資料:接口