I'm trying to use WebBrowser
in WPF
. I just use Navigate
method like this:
public MainWindow()
{
InitializeComponent();
browser.Navigate("https://stackoverflow.com/");
}
First, it showed many messages related with scripts:
Script Errors Screen Shot
Then it shows incorrect page Screen Shot
I found this WPF WebBrowser control - how to suppress script errors? and used this code :
public MainWindow()
{
InitializeComponent();
browser.Navigated += new NavigatedEventHandler(Browser_Navigated);
browser.Navigate("https://stackoverflow.com/");
}
private void Browser_Navigated(object sender, NavigationEventArgs e)
{
SetSilent(browser, true);
}
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);
}
After that there are no error messages, but page still incorrect.
Does anybody know how to fix this issue?
Here is xaml
code :
<Window x:Class="MyWebBrowser.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyWebBrowser"
mc:Ignorable="d"
Height="300" Width="450"
Title="MainWindow">
<Grid>
<WebBrowser Name="browser"></WebBrowser>
</Grid>