WPF WebBrowser中的脚本警告

时间:2012-04-05 14:14:09

标签: c# wpf

您好,我想在WPF中使用一个很棒的HTML编辑器。所以我这样做是因为我在WPF窗口中添加了一个WebBrowser,并使用TinyMCE html编辑器将其导航到html页面。但是,当我运行应用程序时,浏览器控件会显示脚本警告:(请参阅此MSDN主题以获取图像http://social.msdn.microsoft.com/Forums/en/wpf/thread/cbc3eae6-dbc4-4074-befc-902d990fbaae

我在这里尝试了Simon Mourier在StackOverflow上发布的COM代码(http://stackoverflow.com/questions/6138199/wpf-webbrowser-control-how-to-supress-script-errors)

现在我的代码看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Reflection;

namespace TinyMceWpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            webBrowser1.Navigated += new NavigatedEventHandler(webBrowser1_Navigated);
            webBrowser1.Navigate(new
Uri(@"C:\Users\MAHESH\Desktop\TechNode\WPF\MytTechDos\TinyMceWpf\TinyMceWpf\TinyMceWpf\edit.html"));
        }


        private void btnGetHtml_Click(object sender, RoutedEventArgs e)
        {
            string editHtml = this.webBrowser1.InvokeScript("getContent").ToString();
            MessageBox.Show(editHtml);
        }

    public static void SetSilent(WebBrowser browser, bool silent)
    {
        if (browser == null)
            throw new ArgumentNullException("browser");

        // get an IWebBrowser2 from the document
        IOleServiceProvider sp = browser.Document as IOleServiceProvider;
        if (sp != null)
        {
            Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
            Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E");

            object webBrowser;
            sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out webBrowser);
            if (webBrowser != null)
            {
                webBrowser.GetType().InvokeMember("Silent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.PutDispProperty, null, webBrowser, new object[] { silent });
            }
        }
    }


    [ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    private interface IOleServiceProvider
    {
        [PreserveSig]
        int QueryService([In] ref Guid guidService, [In] ref Guid riid, [MarshalAs(UnmanagedType.IDispatch)] out object ppvObject);
    }

    private void webBrowser1_Navigated(object sender, NavigationEventArgs e)
    {
        SetSilent(webBrowser1, true);
    }
}
}

但直到现在它还没有运作。我该怎么办?请帮忙。

1 个答案:

答案 0 :(得分:3)

在SetSilent方法中使用此代码。

FieldInfo webBrowserInfo = browser.GetType().GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
object comWebBrowser = null;

if (webBrowserInfo != null)
   comWebBrowser = webBrowserInfo.GetValue(browser);

if (comWebBrowser != null)
   comWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, comWebBrowser, new object[] { silent });