如何处理WPF WebBrowser控件导航例外 [英] How to handle WPF WebBrowser control navigation exception

查看:376
本文介绍了如何处理WPF WebBrowser控件导航例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设 WPF WebBrowser控件显示了一些导航错误,该页面无法显示。

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

因此​​,有一个例外 WPF WebBrowser控件的。

So there is an exception of WPF WebBrowser control.

我发现了一些类似的问题<一href="http://stackoverflow.com/questions/1206540/webbrowser-control-detect-navigation-failure">here但它不是我所需要的。

I found some similar question here but it is not what I need.

其实我需要一些方法和对象,有一个例外得到它一些如何。

In fact I need some method and object that has an exception to get it some how.

我们怎么能处理呢?

感谢您!

P.S。没有用于WinForm的WebBrowser控件的一些做法......我们可以做类似的事情了 WPF web浏览器控制?

P.S. There is some approach for WinForm WebBrowser Control... Can we do something similar for WPF WebBrowser control?

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");
     }
}

P.S。 #2要举办的WinForm WebBrowser控件在WPF应用程序是不是一个答案,我想。

P.S. #2 To host WinForm WebBrowser control under WPF App is not an answer I think.

推荐答案

我挣扎着类似的问题。当电脑失去互联网连接,我们要处理,在一个很好的方式。

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

在没有更好的解决办法,我迷上了web浏览器的导航的活动,并期待在URL中的文件。如果是RES://ieframe.dll我pretty的信心,一些错误occcured

In 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 occcured.

也许可以看看文件,看看服务器返回404。

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
            }
        }
    }
}

这篇关于如何处理WPF WebBrowser控件导航例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆