10

假设WPF WebBrowser control显示一些导航错误并且页面未显示。

所以有一个例外WPF WebBrowser control

我在这里发现了一些类似的问题,但这不是我需要的。

事实上,我需要一些有异常的方法和对象才能以某种方式获取它。

我们该如何处理呢?

谢谢!

PS WinForm WebBrowser Control有一些方法......我们可以做一些类似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 在 WPF App 下托管 WinForm WebBrowser 控件不是我认为的答案。

4

4 回答 4

10

我正在努力解决类似的问题。当计算机失去互联网连接时,我们希望以一种很好的方式处理它。

在没有更好的解决方案的情况下,我连接了 WebBrowser 的 Navigated 事件并查看文档的 URL。如果是 res://ieframe.dll,我很确定发生了一些错误。

也许可以查看文档并查看服务器是否返回 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
            }
        }
    }
}
于 2013-02-07T10:17:09.450 回答
7

这是一个老问题,但由于我刚刚经历过这个,我想我不妨分享一下。首先,我实现了 Markus 的解决方案,但想要更好一些,因为我们的防火墙会重新映射 403 消息页面。

我在这里(在其他地方)找到了一个答案,建议使用NavigationService它有一个NavigationFailed事件。

在您的 XAML 中,添加:

<Frame x:Name="frame"/>

在代码隐藏的构造函数中,添加:

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

处理程序现在可以处理失败的导航或成功的导航:

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

顺便说一句:这是在 .Net 4.5 框架上

于 2015-05-01T00:20:46.577 回答
2

也可以在dynamic这里使用方法。

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
            }
        };
于 2017-09-09T15:56:53.320 回答
1

我一直在为这个问题苦苦挣扎一段时间。我发现了一种比接受的答案更干净的方法来处理这个问题。检查res://ieframe.dll并不总是适合我。有时发生导航错误时,文档 url 为空。

将以下引用添加到您的项目中:

  1. 微软.mshtml
  2. Microsoft.VisualStudio.OLE.Interop
  3. SHDocVw(在 COM 下称为“Microsoft Internet Controls”)

创建以下帮助程序类:

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Windows.Controls;
using System.Windows.Navigation;

/// <summary>
/// Adds event handlers to a webbrowser control
/// </summary>
internal class WebBrowserHelper
{
    [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore", Justification = "consistent naming")]
    private static readonly Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");

    internal WebBrowserHelper(WebBrowser browser)
    {
        // Add event handlers
        browser.Navigated += this.OnNavigated;

        // Navigate to about:blank to setup the browser event handlers in first call to OnNavigated
        browser.Source = null;
    }

    internal delegate void NavigateErrorEvent(string url, int statusCode);

    internal event NavigateErrorEvent NavigateError;

    private void OnNavigated(object sender, NavigationEventArgs e)
    {
        // Grab the browser and document instance
        var browser = sender as WebBrowser;
        var doc = browser?.Document;

        // Check if this is a nav to about:blank
        var aboutBlank = new Uri("about:blank");
        if (aboutBlank.IsBaseOf(e.Uri))
        {
            Guid serviceGuid = SID_SWebBrowserApp;
            Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;

            IntPtr obj = IntPtr.Zero;
            var serviceProvider = doc as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
            if (serviceProvider?.QueryService(ref serviceGuid, ref iid, out obj) == 0)
            {
                // Set up event handlers
                var webBrowser2 = Marshal.GetObjectForIUnknown(obj) as SHDocVw.IWebBrowser2;
                var webBrowserEvents2 = webBrowser2 as SHDocVw.DWebBrowserEvents2_Event;
                if (webBrowserEvents2 != null)
                {
                    // Add event handler for navigation error
                    webBrowserEvents2.NavigateError -= this.OnNavigateError;
                    webBrowserEvents2.NavigateError += this.OnNavigateError;
                }
            }
        }
    }

    /// <summary>
    /// Invoked when navigation fails
    /// </summary>
    [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "consistent naming")]
    [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1306:FieldNamesMustBeginWithLowerCaseLetter", Justification = "consistent naming")]
    private void OnNavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
    {
        this.NavigateError.Invoke(URL as string, (int)StatusCode);
    }
}

然后在你的窗口类中:

// Init the UI
this.InitializeComponent();
this.WebBrowserHelper = new WebBrowserHelper(this.MyBrowserPane);

// Handle nav error
this.WebBrowserHelper.NavigateError += this.OnNavigateError;
于 2020-03-26T22:35:46.497 回答