.netmauimaui-community-toolkit.net-9.0

Binding Command using Community toolkit Markup problem


Hi recently i updated to .net 9 and suddenly i have problem with command binding which i did not have before.

Could someone explain me why this is working

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

but this runs into error

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.'

I have quite standard view that have viewmodel injected in the constructor, and the command is a [RelayCommand] from toolkit


Solution

  • First of all, I can reproduce your problem with the nuget package CommunityToolkit.Maui.Markup version 4.2.0. And when I downgrade the version to version 4.1.0, the Bind() can work successfully.

    Here is my code:

            public MainPage(MyVM myVM)
            {
                InitializeComponent();
                BindingContext = myVM;
                var button = new Button()
                    .Text("Test")
                    .Center()
                    .Bind(Button.CommandProperty,getter:static(MyVM vm)=>vm.TestCommand,mode:BindingMode.OneWay);
                layout.Children.Insert(0, button);
                // there is <VerticalLayout x:Name="layout".../> in the xaml
            }