1

您好,我想要一个很棒的 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);
    }
}
}

但直到现在它仍然没有工作。我应该怎么办?请帮忙。

4

1 回答 1

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 });
于 2012-04-06T11:47:37.807 回答