以 xamarin 形式从 listview 和数据库 fire base 中删除单元格

delete cell from listview and database fire base in xamarin forms

我有一个列表视图,其中包含这样的单元格:

<StackLayout Orientation="Horizontal"  Margin="30,0,20,0" VerticalOptions="Center">
 <Label Grid.Row="0" Text="{Binding AppointmentPatientName}" VerticalOptions="Center" HorizontalOptions="StartAndExpand"   Style="{StaticResource labelView}"/>
  <Label Grid.Row="0" Grid.Column="2" Text="{Binding AppointmentDate.Date}" Font="Small" HorizontalOptions="CenterAndExpand"  Style="{StaticResource labelView}" />
  <Label x:Name="Delete Button" Text="X"></Label>
</StackLayout>

我怎样才能激活这个标签,让它从 firebase 数据库中删除,它是这样的:

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

但我希望它用于标签而不是 ImgButton,并希望在视图模型页面中激活它

i want it for label not ImgButton and want activate it in viewmodel page

是的,您可以使用 TapGestureRecognizer 和 MVVM 来实现。

根据你的代码,我做了一个简单的demo.You可以参考下面的代码:

<ContentPage.BindingContext>
    <tapapp1:MyViewModel></tapapp1:MyViewModel>
</ContentPage.BindingContext>

<StackLayout Orientation="Horizontal"  Margin="30,0,20,0" VerticalOptions="Center">
        <Label  Text="{Binding AppointmentPatientName}" VerticalOptions="Center" HorizontalOptions="StartAndExpand"   />
        <Label  Grid.Column="2" Text="{Binding AppointmentDate}" Font="Small" HorizontalOptions="CenterAndExpand"   />
        <Label x:Name="Delete" Text="X"  >

          <Label.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding TapCommand}"
                      />
          </Label.GestureRecognizers>  

    </Label>
 </StackLayout>

MyViewModel.cs

public class MyViewModel
{
    public string AppointmentPatientName { get; set; }

    public string AppointmentDate { get; set; }


    public ICommand TapCommand => new Command(RemoveItem);

    private void RemoveItem()
    {
       // add your code here

    }

    public MyViewModel()
    {
        AppointmentPatientName = "Test1";
        AppointmentDate = "2022-4-28";
    }
}

注意:您可以根据需要修改以上代码。