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 开发者_如何学Gomy 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.
You could try using WebClient client = new WebClient();
, then use client.DownloadStringAsync(new Uri("request_url"));
to make your request and subscribe to the client.DownloadStringCompleted
event to receive your data when the request is completed. After parsing the data, in the event handler you can then call NavigationService.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 the OnNavigatingFrom
instead (on the previous page ofc), cancel the navigation e.Cancel = true;
, do your thing as in make the request and stuff, then get the application frame and navigate to e.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.
精彩评论