How to handle WPF WebBrowser control navigation exception

前端 未结 4 1847
慢半拍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:58

    I'm struggling with a similar problem. When the computer loses internet connection we want to handle that in a nice way.

    In the lack of a better solution, I hooked up the Navigated event of the WebBrowser and look at the URL for the document. If it is res://ieframe.dll I'm pretty confident that some error has occurred.

    Maybe it is possible to look at the document and see if a 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
                }
            }
        }
    }
    

提交回复
热议问题