Calling NavigationService.Navigate from Accelerometer.ReadingChanged throws a NotSupportedException

Next, you can see the code that I use to invoke the page if a shake event occurs. However, the page appears, but at the same moment the application freezes, and I can not do any further user input, for example, by pressing 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 "NavigationFailed" and that there is a "System.NotSupportedException". What's wrong?

+3
source share
1 answer

The readings are likely to occur too quickly, and you invoke several navigations. 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));
        }); 

    } 
}
+7
source

All Articles