1

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

Screen Shot

Then it shows incorrect page Screen Shot

web browser 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.

web browser page Screen Shot

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>

Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59
Dilshod K
  • 2,924
  • 1
  • 13
  • 46

1 Answers1

3

The web browser control in wpf uses a pretty old IE web browser engine, so thats no wonder it cannot display modern websites. You can navigate to http://html5test.com to get an exact IE version it is using. I suggest using more modern web browser engine and a wrapper to embed it. Take a look at https://cefsharp.github.io

Hirasawa Yui
  • 1,138
  • 11
  • 29