[英]Shake Gesture Library in WP8
我正在尝试设计一个在摇动手机时生成随机数的游戏。 我正在使用Shake Gesture Library ,请参考下面的链接:
我将默认示例代码从:
private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
_lastUpdateTime.Dispatcher.BeginInvoke(
() =>
{
_lastUpdateTime.Text = DateTime.Now.ToString();
CurrentShakeType = e.ShakeType;
});
}
至:
private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
PlayButton_Click(null, null);
}
PlayButton_Click()是我的方法,其中包含rest语句以生成随机数。
我摇了摇手机,但进入PlayButton_Click()的第一条语句后却显示错误:
System.Windows.ni.dll中发生类型为'System.UnauthorizedAccessException'的异常,但未在用户代码中处理
问题在于PlayButton_Click正在访问UI组件,对Instance_ShakeGesture的调用是通过非UI线程进行的,您不能访问除主UI线程以外的任何线程上的UI组件。 Dispatcher.BeginInvoke用于将请求放在UI线程上。 您需要使用Dispatcher.BeginInvoke来调用您的PlayButton_Click
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
PlayButton_Click(null, null);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.