.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
相关产品推荐
相关产品推荐

