WebBrowser 未正确显示页面
WebBrowser isn't showing page correctly
我正在尝试在 WPF
中使用 WebBrowser
。我只是像这样使用 Navigate
方法:
public MainWindow()
{
InitializeComponent();
browser.Navigate("https://whosebug.com/");
}
首先,它显示了很多与脚本相关的消息:
脚本错误屏幕截图
然后显示不正确的页面截图
我找到了这个 WPF WebBrowser control - how to suppress script errors? 并使用了这个代码:
public MainWindow()
{
InitializeComponent();
browser.Navigated += new NavigatedEventHandler(Browser_Navigated);
browser.Navigate("https://whosebug.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);
}
此后没有错误信息,但页面仍然不正确。
有人知道如何解决这个问题吗?
这是 xaml
代码:
<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>
wpf 中的网页浏览器控件使用了相当古老的 IE 网页浏览器引擎,所以难怪它不能显示现代网站。您可以导航至 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
我正在尝试在 WPF
中使用 WebBrowser
。我只是像这样使用 Navigate
方法:
public MainWindow()
{
InitializeComponent();
browser.Navigate("https://whosebug.com/");
}
首先,它显示了很多与脚本相关的消息:
脚本错误屏幕截图
然后显示不正确的页面截图
我找到了这个 WPF WebBrowser control - how to suppress script errors? 并使用了这个代码:
public MainWindow()
{
InitializeComponent();
browser.Navigated += new NavigatedEventHandler(Browser_Navigated);
browser.Navigate("https://whosebug.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);
}
此后没有错误信息,但页面仍然不正确。
有人知道如何解决这个问题吗?
这是 xaml
代码:
<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>
wpf 中的网页浏览器控件使用了相当古老的 IE 网页浏览器引擎,所以难怪它不能显示现代网站。您可以导航至 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