简体   繁体   中英

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:

Link: Shaking-up-your-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.

I shake my phone, but it is showing me error after going into first statement of PlayButton_Click() :

An exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll but was not handled in user code

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. Dispatcher.BeginInvoke is used to put the request on the UI thread. You need to use Dispatcher.BeginInvoke to call your PlayButton_Click

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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