You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WIN UI程序调用ContentDialog时出现冻结问题求助

问题分析与修复方案

你的代码引发UI冻结的核心原因如下:

  • 重复调用ShowAsync():代码先无意义执行了一次await dialog.ShowAsync();,紧接着在while循环里再次调用该方法,导致对话框实例被重复触发显示,引发UI线程上下文冲突,最终造成冻结。
  • DefaultButton配置错误:将dialog.DefaultButton设为ContentDialogButton.Close不符合交互逻辑,同时可能干扰对话框的正常响应流程。

此外,代码中第一个ContentDialogResult result变量完全未被使用,属于冗余逻辑。

修复后的代码

public async Task<bool> enterPasswordAsync(modeCheckPassword checkPassword)
{
    var content = new PasswordBox { Height = 32 };
    ContentDialog dialog = new ContentDialog();
    dialog.XamlRoot = frame.XamlRoot;
    dialog.Content = content;
    dialog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
    dialog.Title = "Введите пароль";
    dialog.PrimaryButtonText = "Войти";
    dialog.SecondaryButtonText = "Отмена";
    // 修正默认按钮为Primary,匹配登录交互逻辑
    dialog.DefaultButton = ContentDialogButton.Primary;

    while (true)
    {
        ContentDialogResult result = await dialog.ShowAsync();
        if (result == ContentDialogResult.Primary)
        {
            // 注意:若checkPassword委托包含耗时操作,需改为异步执行,避免阻塞UI线程
            if (checkPassword.Invoke(content.Password))
                return true;
            else
            {
                // 密码验证失败时清空输入并提示用户
                content.Password = string.Empty;
                dialog.Title = "Пароль неверный, попробуйте снова";
            }
        }
        else
        {
            return false;
        }
    }
}

额外注意事项

  • 验证XamlRoot有效性:确保frame是当前应用活跃窗口的有效Frame实例,若frame为空或指向已销毁的UI元素,会导致对话框无法正确绑定UI上下文,引发冻结或无响应。
  • 处理委托异步性:如果checkPassword委托包含IO操作(如读取文件、调用API)或其他耗时逻辑,需将其改为异步委托(Func<string, Task<bool>>),并使用await checkPassword(content.Password)执行,避免阻塞UI线程。
  • 检查样式资源:确认DefaultContentDialogStyle资源存在且未破坏对话框默认布局逻辑,错误的样式可能导致对话框内部UI元素无法正常响应输入。

内容的提问来源于stack exchange,提问作者Maclay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 05:29:58