嗨,我想要一个伟大的HTML编辑器在WPF中。所以为此我所做的是,我添加了一个WebBrowser到WPF窗口,并使用其中的TinyMCE html编辑器将其导航到html页面。但是当我运行该应用程序时,浏览器控件显示脚本警告:(请参阅此MSDN线程的图像http://social.msdn.microsoft.com/Forums/en/wpf/thread/cbc3eae6-dbc4-4074-befc-902d990fbaae)WPF中的脚本警告WebBrowser
我尝试了由Simon Mourier在StackOverflow上发布的COM代码(http://stackoverflow.com /问题/ 6138199/WPF的网页浏览器 - 控制 - 如何对剿 - 脚本错误)
现在我的代码如下所示:
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);
}
}
}
但它仍然没有工作至今。我该怎么办?请帮忙。
当您启用脚本时,错误消失。问题具体是什么? – Paparazzi 2012-04-05 16:47:56
是的,当我启用脚本时,错误消失了。但是当我运行应用程序时,我不希望脚本错误发生,并且WebBrowser控件导航到包含tinyMce的html页面。 – 2012-04-05 16:53:14
抱歉。实际上,我用它来快速完成回应,然后实施并实验。所以我觉得我忘了标记他们为答案。但我很困惑,我没有把他们当作答案。我的错。我现在标记了它们。是的,谢谢指出这一点。 – 2012-04-05 17:04:30