2

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() in OnNavigatedTo()
  • Crashes If I call NavigationService.GoBack() as a result of WebException thrown in my HTTPWebRequest when Internet is not available [1]
  • Works fine if Internet is available and I call NavigationService.GoBack() when my HTTPWebRequest 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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Thomas Joulin
  • 6,590
  • 9
  • 53
  • 88
  • My first guess was that you have to make sure to execute NavigationService.GoBack() on the UI thread, but that's what you are apparently doing... Have you checked if the page that you are navigation back to is being loaded? Maybe that page if causing these exceptions? – Volker Voecking Oct 07 '11 at 07:53
  • You're right. If I do `NavigationService.GoBack()` instead of the `HTTPWebRequest` then GoBack() in a catch I have the same exceptions ! But I don't even get to my `OnNavigatedTo` ! If I have Internet, do the web request and call GoBack on the main thread once the request is successful, it work though. My guess is the GoBack fails if it happens too soon ? I'll update my question – Thomas Joulin Oct 07 '11 at 08:06

1 Answers1

0

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.

Marius Bughiu
  • 997
  • 14
  • 32