mvvmmauiicommand

Why is the ICommand not triggered in my MAUI app?


At the XAML I got a button using a command:

<Button Text="Month" Command="{Binding SetMonthCommand}"/>

At the ViewModel class I implement the Command:

public ICommand SetMonthCommand()
{
    IsMontHActive = true;
    IsListActive = !IsMontHActive;
    return null;
}

But this method is never called (by clicking the button). What is missing?

My XAML implements the ViewModels by namespace.

xmlns:viewModels="clr-namespace:MyProject.ViewModels"

My ViewModel is bound to the XAML in .cs file:

this.BindingContext = _calendarPageViewModel;

I tried using the ICommand as public property, but when I click the button, the setter is not reached.

private ICommand _setMonatCommand; 
public ICommand SetMonatCommand 
{ 
    get { return _setMonatCommand; } 
    set { 
        _setMonatCommand = value; 
        IsMonatActive = true; 
        IsListeActive = !IsMonatActive; 
    }
}

Solution

  • create a public property

    public ICommand MyCommand { get; set; }
    

    then in your constructor set it

    MyCommand = new ICommand( () => {
      IsMontHActive = true;
      IsListActive = !IsMontHActive;
    });