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

一旦尝试在后台任务记录或播放一段音频文件.总是提示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
{} 实现接口两个方法:
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用法。
参考资料:
Enable Xna Framework Events in Windows phone Application
原文转自:http://www.cnblogs.com/chenkai/archive/2011/12/25/2301063.html
上一篇:Windows Phone 7中实现ListBox的分页加载
下一篇:Windows Phone应用PhoneGap之处理屏幕旋转
Feedback