首頁 >後端開發 >C++ >如何在AJAX調用期間修復反應遲鈍的C#WebBrowser控件?

如何在AJAX調用期間修復反應遲鈍的C#WebBrowser控件?

DDD
DDD原創
2025-01-30 20:36:09624瀏覽

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