Reputation: 93
I am trying to design a Game which generates Random Numbers on Shaking the phone. I am using Shake Gesture Library , Refer link below:
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
Upvotes: 0
Views: 163
Reputation: 6864
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);
});
Upvotes: 1