如何让 WebBrowser 控件显示现代内容? [英] How can I get the WebBrowser control to show modern contents?

查看:27
本文介绍了如何让 WebBrowser 控件显示现代内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个使用 WebBrowser 控件的 Winforms 应用程序;我动态分配它的 Uri.它工作了一段时间很好,但现在我收到了这个消息:

您似乎在使用不受支持的浏览器.较旧的浏览器可能会危及您的安全,速度较慢且无法与较新的 Google 地图功能配合使用.要访问 Google 地图,您需要更新到现代浏览器.

最后两个词是一个链接,在那个链接之后,我看到:

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

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

我的机器设置为使用 Chrome 作为浏览器;也许控件应该使用您当前的浏览器?我不知道这是否可能/可行.

更新

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

键、字符串值、二进制值

我认为字符串值是.exe"字符串,二进制值是dword"值,但键"值应该是什么?

解决方案

WebBrowser Control

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

为 Windows 窗体使用 WebViewCompatible 控件

您还可以使用新的 Windows 窗体的 WebViewCompatible 控件.您可以在此处查看使用的简单步骤:将 WebBrowser 控件替换为适用于 Windows 窗体的新 WebView 兼容控件.

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

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

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

  • 注意:WebView2 是替代品用于 WebVeiwWebViewCompatible.

设置 X-UA 兼容元标记

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

使用其他浏览器控件

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

I've created a Winforms app that uses a WebBrowser control; I dynamically assign its Uri. It worked fine for awhile, but now I'm getting this msg:

You seem to be using an unsupported browser. Older browsers can put your security at risk, are slow and don't work with newer Google Maps features. To access Google Maps, you'll need to update to a modern browser.

The last two words are a link, and following that link, I see:

You are currently using... IE 11

So, okay, the WebBrowser component uses IE 11; how can I change that?

My machine is set to use Chrome as its browser; perhaps the control should use whatever your current browser is? I don't know if that's possible/feasible.

UPDATE

Okay, I'm willing to give Reza's suggestion a try. But when I navigate to the specified spot in regedit, and right-click in the right pane to add a New entry, it has three options:

Key, String Value, Binary Value

I reckon the string values are the ".exe" strings, and the Binary values are the "dword" vals, but what should the "Key" values be?

解决方案

WebBrowser Control

The WebBrowser control uses the same Internet Explorer version which is installed on your OS but it doesn't use the latest document mode by default and shows content in compatibility mode.

Symptom - As a symptom, the site works properly in Internet Explorer or other browsers, but WebBrowser control doesn't show the site well and for some sites it shows script errors.

Solution - You can tell the WebBrowser control to use the latest document mode without compatibility mode in WebBrowser control. You can follow instructions here to disable the setting using registry. [Reference: Browser Emulation]

Apply Browser Emulation setting using code

If you want to apply the settings using code, run the following code once:

using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
    @"SoftwareMicrosoftInternet ExplorerMainFeatureControlFEATURE_BROWSER_EMULATION",
    true))
{
    var app = System.IO.Path.GetFileName(Application.ExecutablePath);
    key.SetValue(app, 11001, Microsoft.Win32.RegistryValueKind.DWord);
    key.Close();
}

In above code, I've used 11001 which means IE11 Edge mode.

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.

Apply the Browser Emulation setting manually

Open Registry editor and browse HKEY_CURRENT_USER, go to the following key:

SoftwareMicrosoftInternet ExplorerMainFeatureControlFEATURE_BROWSER_EMULATION

Add the following values:

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

(In older versions of Visual Studio you needed to add vshost.exe value as well, when you run your program in Visual Studio.)

To create entries right click on an empty area of the right pane, then in the window which appears after selecting dword value, choose hexadecimal and enter 2af9:

In above steps, I've used 11001 which means IE11 Edge mode.

Use WebViewCompatible Control for Windows Forms

You can also use the new 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 uses one of two rendering engines to support a broader set of Windows clients:

  • On Windows 10 devices, the newer Microsoft Edge rendering engine is used to embed a view that renders richly formatted HTML content from a remote web server, dynamically generated code, or content files.

  • On devices running older versions of Windows, the System.Windows.Controls.WebBrowser is used, which provides Internet Explorer engine-based rendering.

  • Note: WebView2 is a replacement for WebVeiw and WebViewCompatible.

Set X-UA-Compatibile meta tag

In case that you have access to the html content of the page and you can change the content (for example it's a local html file, or the site belong to yourself) then you can set X-UA-Compatibile meta tag in the head like: <meta http-equiv="X-UA-Compatible" content="IE=Edge" />.

Use other Browser Controls

You can rely on other browser controls like CefSharp.

这篇关于如何让 WebBrowser 控件显示现代内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆