如何让 WebBrowser 控件显示现代内容?

How can I get the WebBrowser control to show modern contents?

我创建了一个使用 WebBrowser 控件的 Winforms 应用;我动态分配它的 Uri。有一段时间它工作正常,但现在我收到了这条消息:

您似乎使用了不受支持的浏览器。较旧的浏览器可能会使您的安全受到威胁,速度很慢,并且无法使用较新的 Google 地图功能。要访问 Google 地图,您需要更新到现代浏览器。

最后两个字是link,后面是link,我看到:

您目前正在使用... IE 11

所以,好的,WebBrowser 组件使用 IE 11;我该如何更改?

我的机器设置为使用 Chrome 作为浏览器;也许控件应该使用您当前的浏览器是什么?我不知道那是不是 possible/feasible.

更新

好的,我愿意尝试 Reza 的建议。但是当我导航到regedit中的指定位置,并在右窗格中右键单击以添加一个新条目时,它有三个选项:

键、字符串值、二进制值

我认为字符串值是“.exe”字符串,二进制值是 "dword" 值,但是 "Key" 值应该是多少?

C# WebBrowser class 基本上是一个 IE 包装器,因此无法更改。

参见this link:

The WebBrowser control is a managed wrapper around a component installed with Internet Explorer.

您可以查看替代方案

WebKit.NET

GeckoFX

Note: The post is about WebBrowser control, however, for all the new .NET projects the main solution is using WebView2. To learn more, take a look at this post:

  • .

Web 浏览器控件

WebBrowser 控件使用与您 OS 上安装的相同的 Internet Explorer 版本,但默认情况下它不使用最新的文档模式,而是以兼容模式显示内容。

症状 - 作为一个症状,站点在 Internet Explorer 或其他浏览器中正常工作,但 WebBrowser 控件不能很好地显示站点并且对于某些站点它显示脚本错误。

解决方法- 可以让WebBrowser控件使用最新的文档模式,而不用在WebBrowser控件中使用兼容模式。您可以按照说明 here 使用注册表禁用该设置。 [参考:Browser Emulation]

使用代码应用浏览器仿真设置

如果您想使用代码应用设置,运行 一次以下代码:

using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
    @"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
    true))
{
    var app = System.IO.Path.GetFileName(Application.ExecutablePath);
    key.SetValue(app, 11001, Microsoft.Win32.RegistryValueKind.DWord);
    key.Close();
}

在上面的代码中,我使用了 11001 这意味着 IE11 Edge 模式。

Internet Explorer 11. Webpages are displayed in IE11 edge mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive causes the page to load in Quirks.

手动应用浏览器仿真设置

打开注册表编辑器并浏览 HKEY_CURRENT_USER,转到以下项:

Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION

添加以下值:

"YourApplicationFileName.exe"=dword:00002af9
"YourApplicationFileName.vshost.exe"=dword:00002af9

(在 Visual Studio 的旧版本中,当您 运行 您的程序在 Visual Studio.)

要创建条目,请右键单击右侧窗格的空白区域,然后在选择 dword 值后出现的 window 中,选择十六进制并输入 2af9:

在上面的步骤中,我使用了 11001,这意味着 IE11 Edge 模式。

为 Windows 表单使用 WebViewCompatible 控件

您也可以使用新的WebViewCompatible control for Windows Forms. You can see simple steps to use here: Replace WebBrowser control by new WebView Compatible control for Windows Forms

WebViewCompatible 使用两个渲染引擎之一来支持更广泛的 Windows 客户端:

  • 在 Windows 10 台设备上,较新的 Microsoft Edge 呈现引擎用于嵌入一个视图,该视图呈现来自远程 Web 服务器的格式丰富的 HTML 内容,动态生成的代码, 或内容文件。

  • 在 运行 旧版本 Windows 设备上,使用 System.Windows.Controls.WebBrowser,它提供基于 Internet Explorer 引擎的渲染。

  • 注意: WebVeiwWebViewCompatible 的替代。

设置 X-UA-Compatible 元标记

如果您有权访问页面的 html 内容并且可以更改内容(例如它是本地 html 文件,或者该站点属于您自己),那么您可以在 head 中设置 X-UA-Compatibile 元标记,例如:<meta http-equiv="X-UA-Compatible" content="IE=Edge" />.

使用其他浏览器控件

您可以依赖其他浏览器控件,例如 CefSharp

在我的应用程序嵌入式自定义协议的情况下,我将只允许浏览应用程序提供的页面,而不允许来自外部的内容,所以我想跳过保存到 Windows 注册表的步骤。当我按照 Reza Aghaei 的回答进行测试时,发现您可以从内容页面中更改兼容模式。这将跳过配置注册表项的需要,但您必须将其添加到每个页面。

要更改页面的兼容模式,您必须添加元标记以供呈现引擎应用:

<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    </head>
    <body>
    ...
    </body>
</html>

以下过程将添加正确的密钥并再次将其删除。 在加载 Web 浏览器所在的表单时调用 CreateBrowserKey。 然后在关闭表单时,调用 RemoveBrowserKey

Private Sub CreateBrowserKey(Optional ByVal IgnoreIDocDirective As Boolean = False)
    '      Dim basekey As String = Microsoft.Win32.Registry.CurrentUser.ToString
    Dim value As Int32
    '       Dim thisAppsName As String = My.Application.Info.AssemblyName & ".exe"

    ' Value reference: http://msdn.microsoft.com/en-us/library/ee330730%28v=VS.85%29.aspx
    ' IDOC Reference:  http://msdn.microsoft.com/en-us/library/ms535242%28v=vs.85%29.aspx
    Select Case (New WebBrowser).Version.Major
        Case 8
            If IgnoreIDocDirective Then
                value = 8888
            Else
                value = 8000
            End If
        Case 9
            If IgnoreIDocDirective Then
                value = 9999
            Else
                value = 9000
            End If
        Case 10
            If IgnoreIDocDirective Then
                value = 10001
            Else
                value = 10000
            End If
        Case 11
            If IgnoreIDocDirective Then
                value = 11001
            Else
                value = 11000
            End If

        Case Else
            Exit Sub
    End Select
    Microsoft.Win32.Registry.SetValue(Microsoft.Win32.Registry.CurrentUser.ToString & BrowserKeyPath, _
                                              Process.GetCurrentProcess.ProcessName & ".exe", _
                                              value, _
                                              Microsoft.Win32.RegistryValueKind.DWord)
End Sub

Private Sub RemoveBrowserKey()
    Dim key As Microsoft.Win32.RegistryKey
    key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(BrowserKeyPath.Substring(1), True)
    key.DeleteValue(Process.GetCurrentProcess.ProcessName & ".exe", False)
End Sub