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

.NET 9 MAUI Toolkit升级后弹窗关闭传参为空问题求助

.NET 9弹窗命令参数为null的修复方案

1. 调整XAML的CommandParameter绑定逻辑

原绑定取的是Popup的BindingContext,但你实际需要传递弹窗页面实例,且.NET 9中RelativeSource的祖先查找行为有变化导致该绑定返回null。修改按钮的CommandParameter:

<Button Command="{Binding SettingsDoneCommand}" 
        CommandParameter="{Binding Source={RelativeSource AncestorType={x:Type toolkit:Popup}}}" 
        Text="Done" />

移除Path=BindingContext,直接将Popup实例作为命令参数传递。

2. 优化ViewModel的空值安全处理

避免强制空值解包引发异常,增加类型判断:

SettingsDoneCommand = new Command<object>(async (object o) => 
{
    Preferences.Set("USER", CardNo);
    Preferences.Set("PASS", Pin);
    var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
    
    if (o is PrefPopupPage p)
    {
        await p.CloseAsync(token: cts.Token);
        await Shell.Current.GoToAsync("//home");
        RefreshCanExecutes();
    }
});

排查补充

若问题仍存在,确认:

  • MAUI Toolkit的命名空间引用正确(通常为xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit")
  • PrefPopupPage确实继承自toolkit:Popup类型

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 05:52:39