在 mvvm xamarin 中单击图像按钮

ImgButton click in mvvvm xamarin

我在 xaml.cs 文件中写了一段代码,但我想在 viewmodel 文件中执行它,但我不知道我该怎么做,我尝试执行命令,但没有成功'没用,我只是个初学者,所以对这种模式和 commadns 没有太多想法

这是我的代码:

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());
        }

有什么帮助吗?

您可以为 ImageButton 绑定命令。下面是一个简单的代码供您参考。

Xaml:

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

视图模型:

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

页面绑定:

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

   
}