Home > Software design >  ImgButton click in mvvvm xamarin
ImgButton click in mvvvm xamarin

Time:04-27

I have a code written in xaml.cs file, But i want to do it in viewmodel file, but i don't know how can i do it, i tried to do command but this is didn't worked and i'm just a beginner so don't have many ideas about this pattern and commadns

this is my code :

private async void ItemImageButton_Clicked(object sender, EventArgs e)
        {
            var appoitment = (Appoitment)((ImageButton)sender).CommandParameter;
            AppintmentService appintmentService = new AppintmentService();
            await appintmentService.DeleteFollowup(appoitment.PatientID, appoitment.ID);
            await DisplayAlert("Delted", "The patient Deleteed", "Ok");
            await Navigation.PushAsync(new PatientProfileFollowUpPage());
        }

Any help please ?

CodePudding user response:

You could binding Command for the ImageButton. A simple code below for your reference.

Xaml:

 <ImageButton Command="{Binding ImgButtonCommand}"></ImageButton>

ViewModel:

public class Page1ViewModel
{
    public Command ImgButtonCommand { get; set; }
    public Page1ViewModel()
    {
        ImgButtonCommand = new Command(() =>
        {
            //something you want to do 
        });
    }
}

Binding for the page:

public partial class Page1 : ContentPage
{
    public Page1()
    {
        InitializeComponent();
        this.BindingContext=new Page1ViewModel();
    }

   
}
  • Related