嗨,我想要一個偉大的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