Dživo Jelić
Dživo Jelić

Reputation: 2115

Is it possible to navigate from webbrowser NavigationFailed event?

private void webBrowser_NavigationFailed(object sender, NavigationFailedEventArgs e)
    {
        Debug.WriteLine("Navigation Failed");

        if (!Utils.IsNetworkAvailable())
        {
            MessageBoxResult result = MessageBox.Show("Please go to Settings and enable your network connection.",
                                                 "No network connection", MessageBoxButton.OK);

            if (result == MessageBoxResult.OK)
            {                    
                Dispatcher.BeginInvoke(() => 
                    NavigationService.Navigate(new Uri("/TutorialPage.xaml", UriKind.Relative))); //TODO: Doesnt work
            }
        }
}

Is this posible? i want go to previous xaml page not webpage.

Thank you in advance.

Upvotes: 2

Views: 433

Answers (1)

Romasz
Romasz

Reputation: 29792

I've tried your code and it runs ok. Here is my simple example. You can Navigate in NavigationFailedEvent - the problem is that you never get there.

As I've tried the problem mostly concerns Emulator - probably due to how internet connection is realized. For example:

web.Navigate(new Uri(@"http://nosite.azd", UriKind.Absolute));

this Navigation hadn't failed on my Emulator (I was redirected somewhere), but as I've tested it on the Device - it failed.

So try to test your App on device. But IMO it will be much better to check for the internet connection before Navigating (Loading Webbrowser) rather that waiting Navigation to Fail (it can be an additional check up).

Also you don't need to Navigate via Dispatcher as your code runs on main thread.

Upvotes: 1

Related Questions