WebBrowser控件-安装IE 11后页面呈现错误 [英] WebBrowser control - page rendering error after install IE 11

查看:188
本文介绍了WebBrowser控件-安装IE 11后页面呈现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

安装Internet Explorer 11预览版后,我在Winforms .NET类"WebBrowser" 上遇到问题.当我调用网页时,似乎禁用了javascipt.

I have problem with Winforms .NET Class "WebBrowser" after installing of Internet Explorer 11 Preview. It looks like disable javascipt when I call my web page.

推荐答案

如果基于WebBrowser的应用程序和网页仍可以在IE10上正常运行,则可能是以下原因造成的.

If your WebBrowser-based app and your web pages still work fine with IE10, the following is likely to be the reason for the problem.

在一个颇具争议的决定中,

In a rather controversial decision, Microsoft has changed the traditional layout of IE User Agent (UA) string in IE11.

这是IE11中的UI字符串:

This is what the UI string looks like in IE11:


 navigator.userAgent: Mozilla/5.0 (Windows NT 6.2; WOW64; Trident/7.0; rv:11.0) like Gecko
 document.documentMode: 11
 document.compatMode: CSS1Compat

这是它在IE10和更早版本中的外观:

This is what it used to look like in IE10 and older versions:


 navigator.userAgent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0;)
 document.documentMode: 10
 document.compatMode: CSS1Compat

虽然设计良好的网页不应依赖UA字符串来检测可用的HTML功能,但许多现有网页仍然可以使用,并且这种更改可能使它们感到困惑.

While a well-designed web page should not rely upon UA string to detect available HTML features, a lot of existing pages still do, and such change may have confused them.

如果您无法控制加载的网页,并且无法修复它们,则还原传统UA字符串的一种方法是使用

If you have no control over the web pages you load, and cannot fix them, one way to restore the traditional UA string is to force IE7 emulation with FEATURE_BROWSER_EMULATION for your WebBrowser-based app. Unfortunately, you'd have to go for as low as IE7. Specifying a higher version does't restore the old UA string layout.

另一种更灵活但更复杂的解决方法是通过UrlMkSetSessionOption/URLMON_OPTION_USERAGENT WinAPI设置自定义UA字符串.您应该使用UrlMkGetSessionOption检索当前的UI字符串,对其进行解析,添加缺少的部分,然后使用UrlMkSetSessionOption设置回去.在实例化WebBrowser对象之前,在Form类的static构造函数中执行此操作.

Another, more flexible but more complex way to fix this is to set up a custom UA string via UrlMkSetSessionOption/URLMON_OPTION_USERAGENT WinAPI. You should retrieve the current UI string with UrlMkGetSessionOption, parse it, add the missing parts and set it back with UrlMkSetSessionOption. Do it in the static constructor of your Form class, before WebBrowser object gets instantiated.

[UPDATE] 用于更改用户代理字符串的代码:

[UPDATE] The code for changing the user agent string:

static public void ChangeUserAgentForIE11()
{
    if (GetIEVersion() <= 10)
        return;

    var userAgent = new StringBuilder(256);
    int length = 0;
    Win32.UrlMkGetSessionOption(Win32.URLMON_OPTION_USERAGENT, userAgent, userAgent.Capacity-1, ref length, 0);

    // IE10: navigator.userAgent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0)
    // IE11: navigator.userAgent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; Media Center PC 6.0; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko

    var regex = new Regex(@"^\s*(Mozilla/\d+\.\d+\s+\()(Windows\s+.*)like Gecko\s*$", RegexOptions.IgnoreCase); //IE11 regex
    var match = regex.Match(userAgent.ToString());
    if (match.Success)
    {
        var newUserAgent = String.Concat(match.Groups[1], "compatible; MSIE 10.0; ", match.Groups[2]);
        Win32.UrlMkSetSessionOption(Win32.URLMON_OPTION_USERAGENT, newUserAgent, newUserAgent.Length, 0);
        var verifyUserAgent = new StringBuilder(256);
        length = 0;
        Win32.UrlMkGetSessionOption(Win32.URLMON_OPTION_USERAGENT, verifyUserAgent, verifyUserAgent.Capacity-1, ref length, 0);
        if (verifyUserAgent.ToString() != newUserAgent)
            throw new ApplicationException("Unable to change WebBrowser User Agent.");
    }
}

这篇关于WebBrowser控件-安装IE 11后页面呈现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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