'How do i know when a button is clicked through the viewmodel in Xamarin?

I'm very bad at Xamarin and need help, the title explains itself, I really need to detect a button click in the ViewModel from the view, how do I do this? My code is non-existent so... yeah. Please help!



Solution 1:[1]

Bind a Command To the button.

 <Button Text="Button"
                VerticalOptions="CenterAndExpand"
                HorizontalOptions="Center"
                Command="{Binding ButtonCommand}" />

In Your ViewModel


public Command ButtonCommand { set; get; } 

public ConstructorViewModel()
{
    
    ButtonCommand = new Command(ShowAlert);
}


private async void ShowAlert(object obj)
{
    // Do whatever functionality you want
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1