替换WinForms webbrowser错误消息

时间:2016-09-07 20:32:50

标签: c# winforms webbrowser-control

我正在使用WinForms网页浏览器,我想在页面无效时显示自定义错误html,但我无法弄清楚如何做到这一点。我有这个小片段用于显示它:

        if (wb_Main.Document != null)
        { wb_Main.Document.Write(string.Empty); }
        wb_Main.DocumentText = html;

我在SO上发现了一些包含WebBrowser的代码,以便进入似乎工作正常的导航错误事件。但是每当我尝试在错误事件中设置html时,我都会遇到异常:

 System.Runtime.InteropServices.COMException: The requested resource is in use. (Exception from HRESULT: 0x800700AA)

使用堆栈跟踪:

   at System.Windows.Forms.UnsafeNativeMethods.IWebBrowser2.Navigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
   at System.Windows.Forms.WebBrowser.PerformNavigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
   at System.Windows.Forms.WebBrowser.set_Url(Uri value)
   at System.Windows.Forms.WebBrowser.set_DocumentStream(Stream value)
   at System.Windows.Forms.WebBrowser.set_DocumentText(String value)
   at RDES.CMI.Revit.CADDBrowser.UC_Browser.wb_Main_NavigateError(Object sender, WebBrowserNavigateErrorEventArgs e) in c:\RD\Projects\16-003 CADD Browser\Code\CADDBrowser\UC_Browser.xaml.cs:line 84
   at RDES.CMI.Revit.CADDBrowser.WebBrowser2.OnNavigateError(WebBrowserNavigateErrorEventArgs e) in c:\RD\Projects\16-003 CADD Browser\Code\CADDBrowser\WebBrowser2.cs:line 49
   at RDES.CMI.Revit.CADDBrowser.WebBrowser2.WebBrowser2EventHelper.NavigateError(Object pDisp, Object& url, Object& frame, Object& statusCode, Boolean& cancel) in c:\RD\Projects\16-003 CADD Browser\Code\CADDBrowser\WebBrowser2.cs:line 69

我甚至尝试在导航错误处理程序中设置错误文本变量,然后等待文档已完成事件重新导航到错误文本,但文档已完成从不触发。导航错误后我触发的唯一事件是'status text changed'事件,当我尝试显示错误时会抛出相同的错误。

我最初使用的WPF浏览器控件做得非常好,但不幸的是我插入的程序似乎在WPF浏览器中存在一个主要问题(它在某些相对常见的情况下崩溃了程序)。

有办法做到这一点吗?我感觉有点卡住......

以下是我在此处找到的包装类的代码,以防需要:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace RDES.CMI.Revit.CADDBrowser
{
    public class WebBrowser2 : WebBrowser
    {
        AxHost.ConnectionPointCookie cookie;
        WebBrowser2EventHelper helper;

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
        protected override void CreateSink()
        {
            base.CreateSink();

            // Create an instance of the client that will handle the event
            // and associate it with the underlying ActiveX control.
            helper = new WebBrowser2EventHelper(this);
            cookie = new AxHost.ConnectionPointCookie(
                this.ActiveXInstance, helper, typeof(DWebBrowserEvents2));
        }

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
        protected override void DetachSink()
        {
            // Disconnect the client that handles the event
            // from the underlying ActiveX control.
            if (cookie != null)
            {
                cookie.Disconnect();
                cookie = null;
            }
            base.DetachSink();
        }

        public event WebBrowserNavigateErrorEventHandler NavigateError;

        // Raises the NavigateError event.
        protected virtual void OnNavigateError(
            WebBrowserNavigateErrorEventArgs e)
        {
            if (this.NavigateError != null)
            {
                this.NavigateError(this, e);
            }
        }

        // Handles the NavigateError event from the underlying ActiveX 
        // control by raising the NavigateError event defined in this class.
        private class WebBrowser2EventHelper :
            StandardOleMarshalObject, DWebBrowserEvents2
        {
            private WebBrowser2 parent;

            public WebBrowser2EventHelper(WebBrowser2 parent)
            {
                this.parent = parent;
            }

            public void NavigateError(object pDisp, ref object url,
                ref object frame, ref object statusCode, ref bool cancel)
            {
                // Raise the NavigateError event.
                this.parent.OnNavigateError(
                    new WebBrowserNavigateErrorEventArgs(
                    (String)url, (String)frame, (Int32)statusCode, cancel));
            }
        }
    }

    // Represents the method that will handle the WebBrowser2.NavigateError event.
    public delegate void WebBrowserNavigateErrorEventHandler(object sender,
        WebBrowserNavigateErrorEventArgs e);

    // Provides data for the WebBrowser2.NavigateError event.
    public class WebBrowserNavigateErrorEventArgs : EventArgs
    {
        private String urlValue;
        private String frameValue;
        private Int32 statusCodeValue;
        private Boolean cancelValue;

        public WebBrowserNavigateErrorEventArgs(
            String url, String frame, Int32 statusCode, Boolean cancel)
        {
            urlValue = url;
            frameValue = frame;
            statusCodeValue = statusCode;
            cancelValue = cancel;
        }

        public String Url
        {
            get { return urlValue; }
            set { urlValue = value; }
        }

        public String Frame
        {
            get { return frameValue; }
            set { frameValue = value; }
        }

        public Int32 StatusCode
        {
            get { return statusCodeValue; }
            set { statusCodeValue = value; }
        }

        public Boolean Cancel
        {
            get { return cancelValue; }
            set { cancelValue = value; }
        }
    }

    // Imports the NavigateError method from the OLE DWebBrowserEvents2 
    // interface. 
    [ComImport, Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
    TypeLibType(TypeLibTypeFlags.FHidden)]
    public interface DWebBrowserEvents2
    {
        [DispId(271)]
        void NavigateError(
            [In, MarshalAs(UnmanagedType.IDispatch)] object pDisp,
            [In] ref object URL, [In] ref object frame,
            [In] ref object statusCode, [In, Out] ref bool cancel);
    }
}

0 个答案:

没有答案