问题
I have a WPF application and I'm using the WebBrowser control to display some content from a public website.
Sometimes in weird edge cases when the network connectivity is flakey, the web browser will show the "This program cannot display the webpage" error page. Is there some way to listen to the WebBrowser to detect when this occurs, so I can load a placeholder?
回答1:
You could try using the NavigationService from System.Windows.Controls.Frame as indicated in this MSDN forum post. The WebResponse will always be null for the WebBrowser control in WPF (as described in the post).
回答2:
In the Navigated event arguments, you can access the WebResponse, so you can access the HTTP response code.
来源:https://stackoverflow.com/questions/1473453/wpf-webbrowser-events-for-http-errors