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

.NET 9升级后CommunityToolkit.Maui.Markup命令绑定异常咨询

问题原因及解决方案

原因分析

你遇到的TypeLoadException是因为旧版CommunityToolkit.Maui.Markup与.NET 9不兼容。.NET 9对MAUI的绑定系统进行了更新,新增了ApplyToResolvedSource方法的接口要求,但你当前使用的Markup库版本(1.0.0.0)并未实现这个方法,导致运行时类型加载失败。

而原生SetBinding方法能正常工作,是因为MAUI原生绑定系统已经适配了.NET 9的改动,不受第三方库版本问题影响。

解决方案

  1. 升级CommunityToolkit.Maui.Markup到兼容.NET 9的版本
    至少升级到v4.0.0及以上版本(该版本开始正式支持.NET 9),可以通过NuGet包管理器或命令行更新:
    Install-Package CommunityToolkit.Maui.Markup -Version 4.0.0
    
  2. 同步升级CommunityToolkit.Maui
    确保CommunityToolkit.Maui也升级到与Markup版本匹配的兼容版(建议v8.0.0及以上,适配.NET 9),避免跨版本依赖冲突。
  3. 重新编译并测试
    清理项目后重新编译,验证Bind方法是否正常执行,命令绑定是否生效。

代码对比验证

正常工作的原生绑定代码

var bt4 = new Button()
.Text("Confirm All")
.Center();
bt4.SetBinding(Button.CommandProperty, new Binding(nameof(vm.ConfirmAllAlarmsCommand), 
mode: BindingMode.OneTime));

触发异常的Markup绑定代码(升级库后可正常运行)

var bt4 = new Button()
.Text("Confirm All")
.Center()
.Bind(Button.CommandProperty, getter: static (ActiveAlarmSummaryViewModel vm) => 
vm.ConfirmAllAlarmsCommand, mode: BindingMode.OneWay);

异常信息

System.TypeLoadException: 'Method 'ApplyToResolvedSource' in type 
'CommunityToolkit.Maui.Markup.TypedBinding`2' from assembly 
'CommunityToolkit.Maui.Markup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 
does not have an implementation.'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 04:15:59