« Posts tagged WebBrowser

WPF WebBrowser script errors

The WPF WebBrowser control doesn’t expose all the features of the IWebBrowser2 interface. This can be a real pain, because the control doesn’t even expose the underlying activex interface to get around its short comings. Recently I needed to disable script errors from being reported. This can easily be done with the WinForms version of the control. I can across some simple reflection based code to get around the issue. This technique could be used to access other features of the IWebBrowser2 interface.

C#
<code>
public partial class Text : UserControl {
        public Text()
        {                               
            InitializeComponent();            
            browser.Navigated += new NavigatedEventHandler(browser_Navigated);
        }
 
        void browser_Navigated(object sender, NavigationEventArgs e)
        {
            HideScriptErrors(browser, true);
        }
 
 
        public void HideScriptErrors(WebBrowser wb, bool Hide)
        {
            FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
            if (fiComWebBrowser == null) 
                return;
 
            object objComWebBrowser = fiComWebBrowser.GetValue(wb);
 
            if (objComWebBrowser == null) 
                return;
 
            objComWebBrowser.GetType().InvokeMember( "Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide });
 
        }
}
 
</code>