首页 >后端开发 >C++ >如何在AJAX调用期间修复反应迟钝的C#WebBrowser控件?

如何在AJAX调用期间修复反应迟钝的C#WebBrowser控件?

DDD
DDD原创
2025-01-30 20:36:09652浏览

How to Fix Unresponsive C# WebBrowser Control During Ajax Calls?

C# WebBrowser控件与Ajax调用

问题描述

在C# WPF .NET 4应用程序中使用WebBrowser控件时,点击按钮后浏览器无响应。页面显示“您的请求正在处理中”消息,阻止页面继续处理。使用完整的IE浏览器则不会出现此问题。

问题分析

WPF和WinForms中的WebBrowser控件与完整的IE浏览器在行为上存在显着差异。这些差异可能导致脚本兼容性问题。

解决方案

通过实现功能控制来使WebBrowser控件的行为与IE浏览器保持一致。这涉及设置特定的功能,例如FEATURE_BROWSER_EMULATION,无需管理员权限即可实现。

代码示例

<code class="language-csharp">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);
    }
}</code>

使用方法示例

<code class="language-csharp">private void SetBrowserFeatureControl()
{
    // 获取进程特定设置
    var fileName = System.IO.Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);

    // 确保控件不在Visual Studio设计器中运行
    if (String.Compare(fileName, "devenv.exe", true) == 0 || String.Compare(fileName, "XDesProc.exe", true) == 0)
        return;

    SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", fileName, GetBrowserEmulationMode()); // IE10 标准模式,适用于具有基于标准的 !DOCTYPE 指令的页面。
    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!");
        }
        int.TryParse(version.ToString().Split('.')[0], out browserVersion);
    }

    UInt32 mode = 11000; // IE11 标准模式
    switch (browserVersion)
    {
        case 7:
            mode = 7000; // IE7 标准模式
            break;
        case 8:
            mode = 8000; // IE8 模式
            break;
        case 9:
            mode = 9000; // IE9 模式
            break;
        case 10:
            mode = 10000; // IE10 模式
            break;
        default:
            // 默认使用 IE11 模式
            break;
    }

    return mode;
}</code>

部署

为了确保此更改生效,应在WebBrowser初始化之前注册必要的功能,通常在主窗体的构造函数中。

<code class="language-csharp">public MainWindow()
{
    SetBrowserFeatureControl();

    InitializeComponent();
//...
}</code>

更全面的功能设置

有关更全面的功能列表,请参考相关链接中提供的更新答案。

This revised output maintains the original image and uses more natural language while paraphrasing the content. The code remains unchanged as it's not appropriate to paraphrase code.

以上是如何在AJAX调用期间修复反应迟钝的C#WebBrowser控件?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn