Calling NavigationService.Navigate from Accelerome

2020-04-11 18:27发布

In the following you can see the code I use to call a page if a shake event happens. However, the page pops up but in the same moment the app freezes and I can't do any further user input, for example clicking a button.

void accelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
    //double X, Y, Z;
    if (e.X > 1.5)
    {
        Dispatcher.BeginInvoke( () => { 
            NavigationService.Navigate(new Uri("/Bars/DetailBar.xaml", UriKind.Relative));
        } ); 
    } 
}

the debugger tells me, that the "NavigationFailed" and that there is an "System.NotSupportedException". What's going wrong?

1条回答
Animai°情兽
2楼-- · 2020-04-11 19:20

The readings are likely happening too quickly and you are causing multiple Navigations to occur. Try unsubscribing from the event:

void accelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
    //double X, Y, Z;
    if (e.X > 1.5)
    {
        accelerometer.ReadingChanged -= accelerometer_ReadingChanged;

        Dispatcher.BeginInvoke( () => {    
            NavigationService.Navigate(new Uri("/Bars/DetailBar.xaml", UriKind.Relative));
        }); 

    } 
}
查看更多
登录 后发表回答