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); //执行完毕后,取消该事件,以免重复注册
}
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); //执行完毕后,取消该事件,以免重复注册
}
相关文章推荐
- WP7“Navigation is not allowed when the task is not in the foreground.”解决方案
- PRB: "Requested Registry Access Is Not Allowed" Error Message When ASP.NET Application Tries to Write New EventSource in the Eve
- ant批量运行Jmeter脚本遇到 Content is not allowed in prolog.问题及解决方案
- xxx is not in the sudoers file. This incident will be reported的错误解决方案
- is not in the sudoers file 解决方案
- phpnow:open_basedir restriction in effect;file is not within the allowed path的解决办法
- tomcat启动报错“The processing instruction target matching "[xX][mM][lL]" is not allowed.”解决方案
- 当执行sudo时提示“is not in the sudoers file”的解决方案:
- TIP: when you get a message in job log user [Dr. Who] is not authorized to view the log
- xx is not in the sudoers file 问题的解决方案
- 14、 iOS "The sandbox is not in sync with the Podfile.lock"解决方案
- 对Warning: Attempt to present on whose view is not in the window hierarchy!的解决方案
- Only one expression can be specified in the select list when the subquery is not introduced with EXI
- 对Warning: Attempt to present on whose view is not in the window hierarchy!的解决方案
- Only one expression can be specified in the select list when the subquery is not introduced with EXI
- UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
- Claims系列 - ID3206错误 (A signin response may only redirect within the current web application: (url) is not allowed)
- 对Warning: Attempt to present XXX on XXX whose view is not in the window hierarchy!的解决方案
- 对Warning: Attempt to present on whose view is not in the window hierarchy!的解决方案
- 由于空格引起的xml错误之The processing instruction target matching "[xX][mM][lL]" is not allowed以及the markup in the document prec