今天用WPF的WebBrowser控件的时候,发现其竟然没有ScriptErrorsSuppressed属性,导致其到处乱弹脚本错误的对话框,在MSDN上找到了如下解决方案:
1 static void SuppressScriptErrors(WebBrowser webBrowser, bool hide) 2 { 3 webBrowser.Navigating += (s, e) => 4 { 5 var fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); 6 if (fiComWebBrowser == null) 7 return; 8 9 object objComWebBrowser = fiComWebBrowser.GetValue(webBrowser);10 if (objComWebBrowser == null)11 return;12 13 objComWebBrowser.GetType().InvokeMember("Silent", System.Reflection.BindingFlags.SetProperty, null, objComWebBrowser, new object[] { hide });14 };15 }