过早使用 NavigationService.GoBack() 时 Windows Phone 应用程序崩溃
即使 NavigationService.CanGoBack
返回 True
,NavigationService.GoBack()
也会引发这些异常:
A first chance exception of type 'System.ArgumentException' occurred in System.Windows.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in
这会在两种情况下系统地发生,而第三个工作正常:
- 如果我调用
NavigationService.GoBack()
就会崩溃 在OnNavieratedTo()
- 如果我调用就会崩溃
NavigationService.GoBack()
由于 Internet 不可用时在我的HTTPWebRequest
中抛出WebException
[1] - 如果互联网可用,并且当我的
HTTPWebRequest
获取结果、解析并显示结果时,我会调用NavigationService.GoBack()
,则工作正常。
我的理论是,从一个页面导航到另一个页面后,我不能太快调用 GoBack()
...我的问题:当 < code>HTTPWebRequest 无法加载?
编辑:我决定采用另一种方式,但我认为我的问题可能是由于导航动画和 Windows Phone C# 工具包(我使用 2011 年 2 月版) )
[1] 详情我的案例 2 代码:
我有一个简单的 HTTPWebRequest
。我的回调执行此操作,并且我的应用程序在飞行模式下崩溃。尽管 NavigationService.CanGoBack
返回 true
,但 NavigationService.GoBack()
行负责。
try
{
response = request.EndGetResponse(result);
}
catch (WebException)
{
Dispatcher.BeginInvoke(() =>
{
NavigationService.GoBack();
});
}
我也尝试使用 Deployment.Current.Dispatcher.BeginInvoke() 。
Even though NavigationService.CanGoBack
returns True
, NavigationService.GoBack()
throws me these exceptions :
A first chance exception of type 'System.ArgumentException' occurred in System.Windows.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in
This happens systematically on two case, while the third works fine :
- Crashes if I call
NavigationService.GoBack()
inOnNavigatedTo()
- Crashes If I call
NavigationService.GoBack()
as a result ofWebException
thrown in myHTTPWebRequest
when Internet is not available [1] - Works fine if Internet is available and I call
NavigationService.GoBack()
when myHTTPWebRequest
got results, parsed them, and displayed them.
My theory is that I can't call GoBack()
too soon after navigating from a page to another... My question : How can I programatically go back up the navigation stack when an HTTPWebRequest
fails to load ?
Edit : I've decided to do it another way, but I think my problems might be due to navigation animations and the Windows Phone C# Toolkit (I use Feb 2011 edition)
[1] Details of my code on case 2 :
I have a simple HTTPWebRequest
. My callback does this, and my app crashes when in Airplane Mode. The line NavigationService.GoBack()
is responsible, even though NavigationService.CanGoBack
returns true
.
try
{
response = request.EndGetResponse(result);
}
catch (WebException)
{
Dispatcher.BeginInvoke(() =>
{
NavigationService.GoBack();
});
}
I tried using Deployment.Current.Dispatcher.BeginInvoke()
also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用
WebClient client = new WebClient();
,然后使用client.DownloadStringAsync(new Uri("request_url"));
发出请求并订阅client.DownloadStringCompleted
事件,用于在请求完成时接收您的数据。解析数据后,您可以在事件处理程序中调用NavigationService.GoBack();
或转到您想要的任何页面。另外,如果您尝试在
OnNavigedTo
事件中执行某些操作并遇到麻烦,您可以尝试使用OnNavigatingFrom
(在 c 的上一页),取消导航e.Cancel = true;
,按照发出请求等操作,然后获取应用程序框架并导航到e.Uri
(基本上继续您之前取消的导航) 。尽管第二个也可能代表一个解决方案,但我认为第一个更好,因为它异步完成所有工作,因此不会阻塞您的 UI 线程。这是我通常在我的应用程序中使用的。希望有帮助。
You could try using
WebClient client = new WebClient();
, then useclient.DownloadStringAsync(new Uri("request_url"));
to make your request and subscribe to theclient.DownloadStringCompleted
event to receive your data when the request is completed. After parsing the data, in the event handler you can then callNavigationService.GoBack();
or go to whichever page you want.Also, if you try to do something in the
OnNavigatedTo
event and run into trouble you could try using theOnNavigatingFrom
instead (on the previous page ofc), cancel the navigatione.Cancel = true;
, do your thing as in make the request and stuff, then get the application frame and navigate toe.Uri
(basically continuing the navigation you previously cancelled).Altho this second might represent a solution as well, I think the first one is better as it does all the work async thus not blocking your UI thread. This is what I generally use in my apps. Hope it helps.