开发者

Calling NavigationService.Navigate from Accelerometer.ReadingChanged throws a NotSupportedException

开发者 https://www.devze.com 2023-03-08 11:40 出处:网络
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 exampl

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.NotSupporte开发者_运维百科dException". What's going wrong?


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

精彩评论

暂无评论...
验证码 换一张
取 消