简体   繁体   English

WP8中的摇动手势库

[英]Shake Gesture Library in WP8

I am trying to design a Game which generates Random Numbers on Shaking the phone . 我正在尝试设计一个在摇动手机时生成随机数的游戏。 I am using Shake Gesture Library , Refer link below: 我正在使用Shake Gesture Library ,请参考下面的链接:

Link: Shaking-up-your-WP7 链接:摇晃您的WP7

I change the default sample code from: 我将默认示例代码从:

    private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
    _lastUpdateTime.Dispatcher.BeginInvoke(
        () =>
        {
            _lastUpdateTime.Text = DateTime.Now.ToString();
            CurrentShakeType = e.ShakeType;
        });
}

TO: 至:

  private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
              PlayButton_Click(null, null); 

}

PlayButton_Click() is my method which contains rest statements to generate random numbers. PlayButton_Click()是我的方法,其中包含rest语句以生成随机数。

I shake my phone, but it is showing me error after going into first statement of PlayButton_Click() : 我摇了摇手机,但进入PlayButton_Click()的第一条语句后却显示错误:

An exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll but was not handled in user code System.Windows.ni.dll中发生类型为'System.UnauthorizedAccessException'的异常,但未在用户代码中处理

The issue is that PlayButton_Click is accessing UI components, the call to Instance_ShakeGesture comes through on a non-UI thread, you can't access UI components on any thread other than the main UI thread. 问题在于PlayButton_Click正在访问UI组件,对Instance_ShakeGesture的调用是通过非UI线程进行的,您不能访问除主UI线程以外的任何线程上的UI组件。 Dispatcher.BeginInvoke is used to put the request on the UI thread. Dispatcher.BeginInvoke用于将请求放在UI线程上。 You need to use Dispatcher.BeginInvoke to call your PlayButton_Click 您需要使用Dispatcher.BeginInvoke来调用您的PlayButton_Click

        Deployment.Current.Dispatcher.BeginInvoke(
        () =>
        {
            PlayButton_Click(null, null); 
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM