How to handle WPF WebBrowser control navigation exception

Let's say that WPF WebBrowser control shows some navigation errors and the page does not display.

So there is a WPF WebBrowser control exception.

I found several similar questions here , but that is not what I need.

Actually, I need some kind of method and an object that has an exception in order to get it somehow.

How can we handle this?

Thanks!

PS There is some approach for WinForm WebBrowser Control ... Can we do something similar for a WPF WebBrowser ?

 public Form13() { InitializeComponent(); this.webBrowser1.Navigate("http://blablablabla.bla"); SHDocVw.WebBrowser axBrowser = (SHDocVw.WebBrowser)this.webBrowser1.ActiveXInstance; axBrowser.NavigateError += new SHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(axBrowser_NavigateError); } void axBrowser_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel) { if (StatusCode.ToString() == "404") { MessageBox.Show("Page no found"); } } 

PS # 2 To place a WinForm WebBrowser control in a WPF application, I don't think so.

+8
c # wpf
source share
3 answers

I am struggling with a similar problem. When the computer loses its Internet connection, we want to deal with it.

In the absence of a better solution, I hooked up a WebBrowser navigation event and looked at the URL of the document. If this is res: //ieframe.dll, I am sure that an error has occurred.

Perhaps you can look at the document and see if the server returned 404.

 private void Navigated(object sender, NavigationEventArgs navigationEventArgs) { var browser = sender as WebBrowser; if(browser != null) { var doc = AssociatedObject.Document as HTMLDocument; if (doc != null) { if (doc.url.StartsWith("res://ieframe.dll")) { // Do stuff to handle error navigation } } } } 
+8
source share

This is on an old question, but since I just experienced it, I, although I can share. Firstly, I implemented the Markus solution, but wanted something a little better, as our firewall will block 403 message pages.

I found an answer here (among other places) that suggests using NavigationService since it has NavigationFailed .

In your XAML add:

 <Frame x:Name="frame"/> 

In your code-code 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")); 

Handlers can now deal with unsuccessful navigation or success:

 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: .Net 4.5 framework

+6
source share

You can also use the dynamic approach here.

 wb.Navigated += delegate(object sender, NavigationEventArgs args) { dynamic doc = ((WebBrowser)sender).Document; var url = doc.url as string; if (url != null && url.StartsWith("res://ieframe.dll")) { // Do stuff to handle error navigation } }; 
+1
source share

All Articles