问题描述
我在这里发现了一个类似的问题:
I found a similar question here:
如何抑制脚本使用 WPF WebBrowser 控件时出错?
但是这些解决方案都不适合我.我需要阻止弹出窗口出现,因为我正在使用 WebBrowser 在网站上自动执行管理任务.
But non of those solutions work for me. I need to stop the popups from appearing as i am using the WebBrowser to automate admin tasks on a website.
SuppressScriptErrors
似乎不是我的 WebControl 上的可用属性 :(
SuppressScriptErrors
does not appear to be an available attribute on my WebControl :(
推荐答案
这是一个 C# 例程,它能够将 WPF 的 WebBrowser
置于静默模式.你不能在 WebBrowser 初始化时调用它,因为它太早了,而是在导航发生之后.这是一个带有 wbMain WebBrowser 组件的 WPF 示例应用:
Here is a C# routine that is capable of putting WPF's WebBrowser
in silent mode. You can't call it at WebBrowser initialization as it 's too early, but instead after navigation occured. Here is a WPF sample app with a wbMain WebBrowser component:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
wbMain.Navigated += new NavigatedEventHandler(wbMain_Navigated);
}
void wbMain_Navigated(object sender, NavigationEventArgs e)
{
SetSilent(wbMain, true); // make it silent
}
private void button1_Click(object sender, RoutedEventArgs e)
{
wbMain.Navigate(new Uri("... some url..."));
}
}
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);
}
这篇关于WPF WebBrowser 控件 - 如何抑制脚本错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!