代码改变世界

Windows phone 应用开发[5]-FrameworkDispatcher异常处理

2011-12-25 16:20  chenkai  阅读(1088)  评论(5编辑  收藏  举报

当你在Silverlight 或是Windows phone 应用程序中通过引用Microsoft.Xan.Framework尝试通过后台任务播放一段音频文件或是记录音频时. 通常会遇到如下关于FrameworkDispatcher异常信息:

2011-12-25_152651

一旦尝试在后台任务记录或播放一段音频文件.总是提示Application没有调用FrameworkDisplatcher.Update()方法.当你使用XNA对象在一个Silverlight或Windows phone 应用程序时这个异常很正常.在执行播放任务前.因首先检查在后台是否已经存在该资源任务的占用.如果已经占用.则以对话框的形式来提示用户是否停止.Check Code:

   1:  if (!Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl)
   2:   
   3:  //play Background Music File

在XnaFramework中 Xna框架的事件消息处理机制是放在一个XNA处理的消息队列中.在一个XNa Application 中实现GAme类. FrameworkDispatcher.Update()方法是自动调用Game.Update处理.

在一个没有实现GAme类的Windows phone或Silverlight应用程序中.则必须采用手动的方式调用FrameWorkDisplatcher.Update()通过XnaFrameWork消息队列自己发送消息.

Well 首先添加引用:

   1:  using System.Windows.Threading;
   2:  using Microsoft.Xna.Framework;

创建一个类实现IApplicationService接口:

   1:   public partial class NuanceFunctionDemo_Form : RecognizerListener, IApplicationService
   2:   {}

实现接口两个方法:

   1:   
   2:          public void StartService(ApplicationServiceContext context)
   3:          {
   4:              _frameworkDispatcherTimer.Start();
   5:          }
   6:   
   7:          public void StopService()
   8:          {
   9:              _frameworkDispatcherTimer.Stop();
  10:          }

而接口调用_frameworkDispatcherTimer则是DispatcherTimer对象的实例.完整处理Code:

   1:          private DispatcherTimer _frameworkDispatcherTimer;
   2:   
   3:          void NuanceFunctionDemo_Form_Loaded(object sender, RoutedEventArgs e)
   4:          {
   5:              this.VoiceType_LP.ItemsSource = voiceDefineTypeList;
   6:              this.VoiceType_LP.SelectedIndex = 0;
   7:   
   8:              //textBoxResult.Text = "test one. test two. test three. test four.";
   9:              textBoxServerIp.Text = AppInfo.SpeechKitServer;
  10:              textBoxServerPort.Text = AppInfo.SpeechKitPort.ToString();
  11:   
  12:   
  13:              _frameworkDispatcherTimer = new DispatcherTimer();
  14:              _frameworkDispatcherTimer.Tick += FrameworkDispatcherTimer_Tick;
  15:              _frameworkDispatcherTimer.Interval = new TimeSpan(0, 0, 3);
  16:              FrameworkDispatcher.Update();
  17:   
  18:              speechkitInitialize();
  19:              App.CancelSpeechKit += new CancelSpeechKitEventHandler(App_CancelSpeechKit);                
  20:          }
  21:   
  22:   
  23:          public void StartService(ApplicationServiceContext context)
  24:          {
  25:              _frameworkDispatcherTimer.Start();
  26:          }
  27:   
  28:          public void StopService()
  29:          {
  30:              _frameworkDispatcherTimer.Stop();
  31:          }
  32:   
  33:          void FrameworkDispatcherTimer_Tick(object sender, EventArgs e)
  34:          {
  35:              FrameworkDispatcher.Update();
  36:          }
  37:  }
这个异常在Silverlight和Windows phone 是常见的异常. 关于frameworkDispatcher处理方式 请参考MSDN用法.

参考资料:

Enable Xna Framework Events in Windows phone Application

无觅相关文章插件,快速提升流量