【问题标题】:C# WebBrowser Ajax callC# WebBrowser Ajax 调用
【发布时间】:2013-08-22 09:50:49
【问题描述】:

我正在使用嵌入在 C# WPF .NET4 应用程序中的 WebBrowser 控件。每当我手动按下表单中的按钮时,浏览器都会挂在“正在处理您的请求”消息上,并且没有任何反应。如果我在完整的 IE 浏览器中执行相同操作,则页面会正常处理并产生结果。 我错过了什么?

按钮背后的代码:

<a onclick="startSearch();" href="javascript:void(-1);" name="btnNext" class="btn floatLe noClear btnSubmit btnRight"> 
<span>Continue</span> 
</a>

【问题讨论】:

    标签: c# webbrowser-control


    【解决方案1】:

    WebBrowser 控件(WPF 和 WinForms 版本)在许多方面与完整的 IE 不同。您可能希望实现Feature Control 以使其行为尽可能接近IE(特别是FEATURE_BROWSER_EMULATION),这通常可以解决脚本兼容性问题。这是一些代码,请注意它不需要管理员权限即可运行:

    private void SetBrowserFeatureControlKey(string feature, string appName, uint value)
    {
        using (var key = Registry.CurrentUser.CreateSubKey(
            String.Concat(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\", feature), 
            RegistryKeyPermissionCheck.ReadWriteSubTree))
        {
            key.SetValue(appName, (UInt32)value, RegistryValueKind.DWord);
        }
    }
    

    例如:

    private void SetBrowserFeatureControl()
    {
        // http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx
    
        // FeatureControl settings are per-process
        var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
    
        // make the control is not running inside Visual Studio Designer
        if (String.Compare(fileName, "devenv.exe", true) == 0 || String.Compare(fileName, "XDesProc.exe", true) == 0) 
            return;
    
        SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, GetBrowserEmulationMode()); // Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
        SetBrowserFeatureControlKey("FEATURE_AJAX_CONNECTIONEVENTS", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_DOMSTORAGE ", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_GPU_RENDERING ", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI  ", fileName, 0);
        SetBrowserFeatureControlKey("FEATURE_DISABLE_LEGACY_COMPRESSION", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_LOCALMACHINE_LOCKDOWN", fileName, 0);
        SetBrowserFeatureControlKey("FEATURE_BLOCK_LMZ_OBJECT", fileName, 0);
        SetBrowserFeatureControlKey("FEATURE_BLOCK_LMZ_SCRIPT", fileName, 0);
        SetBrowserFeatureControlKey("FEATURE_DISABLE_NAVIGATION_SOUNDS", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_SCRIPTURL_MITIGATION", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_SPELLCHECKING", fileName, 0);
        SetBrowserFeatureControlKey("FEATURE_STATUS_BAR_THROTTLING", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_TABBED_BROWSING", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_VALIDATE_NAVIGATE_URL", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_WEBOC_DOCUMENT_ZOOM", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_WEBOC_POPUPMANAGEMENT", fileName, 0);
        SetBrowserFeatureControlKey("FEATURE_WEBOC_MOVESIZECHILD", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_ADDON_MANAGEMENT", fileName, 0);
        SetBrowserFeatureControlKey("FEATURE_WEBSOCKET", fileName, 1);
        SetBrowserFeatureControlKey("FEATURE_WINDOW_RESTRICTIONS ", fileName, 0);
        SetBrowserFeatureControlKey("FEATURE_XMLHTTP", fileName, 1);
    }
    
    private UInt32 GetBrowserEmulationMode()
    {
        int browserVersion = 7;
        using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer",
            RegistryKeyPermissionCheck.ReadSubTree,
            System.Security.AccessControl.RegistryRights.QueryValues))
        {
            var version = ieKey.GetValue("svcVersion");
            if (null == version)
            {
                version = ieKey.GetValue("Version");
                if (null == version)
                    throw new ApplicationException("Microsoft Internet Explorer is required!");
            }
            int.TryParse(version.ToString().Split('.')[0], out browserVersion);
        }
    
        UInt32 mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode. Default value for Internet Explorer 11.
        switch (browserVersion)
        {
            case 7:
                mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control.
                break;
            case 8:
                mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8
                break;
            case 9:
                mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.
                break;
            case 10:
                mode = 10000; // Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 mode. Default value for Internet Explorer 10.
                break;
            default:
                // use IE11 mode by default
                break;
        }
    
        return mode;
    }
    

    您应该想出自己的set of features 并在 WebBrowser 初始化之前注册它们,例如,在主表单构造函数中:

    public MainWindow()
    {
        SetBrowserFeatureControl();
    
        InitializeComponent();
    //...
    }
    

    更新,我目前使用并推荐一组功能,可以在here找到。

    【讨论】:

    • @Jim,我添加了对“XDesProc.exe”(VS XAML Designer 进程)的检查,您可能也想更新您的代码。
    • 嗯,很难说是什么原因造成的,但我会再次查看all available options 的列表并使用那些看起来相关的,尝试 1 和 0 值。例如,FEATURE_AJAX_CONNECTIONEVENTS、FEATURE_WEBOC_POPUPMANAGEMENT、FEATURE_WINDOW_RESTRICTIONS、FEATURE_TABBED_BROWSING 等。如果您找到它,请告诉我们 :)
    • @Noseratio:很棒的代码。谢谢你。仅供参考:SetBrowserFeatureControlKey("FEATURE_NINPUT_LEGACYMODE", fileName, 0) 阻止通过在 WebBrowser 控件导航网页的文本框中单击鼠标来设置鼠标光标。 Windows 8. IE10.0.9200.16750,更新:10.0.12
    • @ShamilS,谢谢,好点,我会从答案中删除它。
    • 这真是太棒了。谢谢!
    【解决方案2】:

    这对我有用:yourWebBrowser.ScriptErrorsSuppressed = true;

    【讨论】:

      猜你喜欢
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      • 2010-11-29
      • 1970-01-01
      相关资源
      最近更新 更多