Calling NavigationService.Navigate from Accelerometer.ReadingChanged throws a NotSupportedException

前端 未结 1 1052
悲哀的现实
悲哀的现实 2021-01-21 05:54

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

相关标签:
1条回答
  • 2021-01-21 06:44

    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));
            }); 
    
        } 
    }
    
    0 讨论(0)
提交回复
热议问题