How to handle WPF WebBrowser control navigation exception

前端 未结 4 1844
慢半拍i
慢半拍i 2020-12-11 16:13

Let\'s say that WPF WebBrowser control shows some navigation errors and the page is not showing.

So there is an exception of WPF WebBrowser contro

4条回答
  •  借酒劲吻你
    2020-12-11 16:53

    It's an old question but since I have just suffered through this, I thought I may as well share. First, I implemented Markus' solution but wanted something a bit better as our Firewall remaps 403 message pages.

    I found an answer here (amongst other places) that suggests using NavigationService as it has a NavigationFailed event.

    In your XAML, add:

    
    

    In your code-behind's constructor, add:

    frame.Navigated += new System.Windows.Navigation.NavigatedEventHandler(frame_Navigated);
    frame.NavigationFailed += frame_NavigationFailed;
    frame.LoadCompleted += frame_LoadCompleted;
    
    frame.NavigationService.Navigate(new Uri("http://theage.com.au"));
    

    The handlers can now deal with either a failed navigation or a successful one:

    void frame_NavigationFailed(object sender, System.Windows.Navigation.NavigationFailedEventArgs e)
    {
      e.Handled = true;
      // TODO: Goto an error page.
    }
    
    private void frame_Navigated(object sender,  System.Windows.Navigation.NavigationEventArgs e)
    {
      System.Diagnostics.Trace.WriteLine(e.WebResponse.Headers);
    }
    

    BTW: This is on the .Net 4.5 framework

提交回复
热议问题