您的位置:首页 > 其它

WP7“Navigation is not allowed when the task is not in the foreground.”解决方案

2013-02-01 00:08 477 查看
今天在做一个项目的时候,遇到了“Navigation is not allowed when the task is not in the foreground.”这个错误,原因是我在main页面中调用了PhotoChooserTask选择器,应用程序失去激活状态,控制权由照片选择器接管,但是我在Completed事件处理函数中,想直接跳转页面,并把选择的照片传给新页面,代码如下:

        void photoTask_Completed(object sender, PhotoResult e)
        {
            if(e.TaskResult == TaskResult.OK )
            {
                 NavigationService.Navigate(new Uri("/PuzzlePage.xaml?type="+e.ChosenPhoto, UriKind.Relative));
            }

        }

这时候,就报题目中所示错误了,本能反应是开启一个新的线程,或把跳转代码封装一个方法,再用DispatcherTimer控制方法的执行时机,但是,感觉执行效率都太差。

上网查了一下,最终成功解决,其最终解决方案就是:注册Navigated事件,也就是将导航的时间向后推移,这样,当我们导航时,我们的应用程序已从照片选择器中成功接管了控制权。代码如下:

        void photoTask_Completed(object sender, PhotoResult e)

        {

            if(e.TaskResult == TaskResult.OK )

            {

                NavigationService.Navigated += new System.Windows.Navigation.NavigatedEventHandler(NavigationService_Navigated);

            }

        }

        void NavigationService_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)

        {

            NavigationService.Navigate(new Uri("/PuzzlePage.xaml?“, UriKind.Relative));

            NavigationService.Navigated -= new System.Windows.Navigation.NavigatedEventHandler(NavigationService_Navigated);  //执行完毕后,取消该事件,以免重复注册

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐