单击按钮动态发送 id 以从 firebase xamarin 表单中删除

Send id dynamically on buttonImg click to delete from fire base xamarin form

我正在尝试通过单击按钮 Img 从 firebase 数据库中删除文档,但我不知道如何将项目的 ID 发送到删除服务

这是我的视图文件:

 <ContentPage.Behaviors>
        <xct:EventToCommandBehavior Command="{Binding Appearing}" EventName="Appearing" />
    </ContentPage.Behaviors>
    <StackLayout>
        <Grid HorizontalOptions="CenterAndExpand" >
            <Image Source="https://bcckids.org/wp-content/uploads/2016/10/Photo_Main_Slider2.png"                         
                   Scale="1.0" 
                Aspect="Fill"
                   
                />
        </Grid>
        <ListView
            HasUnevenRows="True"
            ItemsSource="{Binding Patients}"
            SelectedItem="{Binding SelectedPatient, Mode=TwoWay}"
            SelectionMode="Single">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid RowDefinitions="Auto,Auto,Auto">
                            <Frame  Style="{StaticResource FrameStyle}"
                                        Margin="15,0,30,0"
                                        >
                                <StackLayout Orientation="Horizontal"  Margin="10,0,20,0" VerticalOptions="Center" HorizontalOptions="Center">
                                    <Image Source="https://www.freeiconspng.com/thumbs/profile-icon-png/profile-icon-9.png" WidthRequest="70" HeightRequest="70"/>
                                    <BoxView 
                                    Color="Maroon"
                                    WidthRequest="1"
                                    Margin="5, 0, 10, 0" />
                                    <Label Grid.Row="0" Text="{Binding PatientName}"  VerticalOptions="Center" HorizontalOptions="Start" Style="{StaticResource labelView}"/>
                                    <ImageButton Style="{StaticResource DeleteButton}"/>
                                </StackLayout>

                            </Frame>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>

        </ListView>
        <Button Style="{StaticResource BlusButton}" Clicked="Button_Clicked_1" />

    </StackLayout>

以及如何在 viewmodel 中编写它并调用 thw 服务? 这是我的删除服务:

public async Task DeletePatient(string ID)
{
    await firebaseClient
   .Child($"Specalists/406707265/Patients/{ID}")
   .DeleteAsync();
}

代码可能如下所示:

// Your item model.
public class Patient
{
    public string Id { get; set; }
    public string PatientName { get; set; }
}

// ... your viewmodel class
{
    public ObservableCollection<Patient> Patients { get; set; } = new ObservableCollection<Patient();
}

// YourView.xaml
...
    <ListView.ItemTemplate>
    ...
        <ImageButton Style="{StaticResource DeleteButton}" Clicked="ItemImageButton_Clicked" CommandParameter="{Binding .}"/>

// your view's code behind (.xaml.cs):
public partial class YourView ...
{
    private async void ItemImageButton_Clicked(object sender, EventArgs e)
    {
        var patient = (Patient)((ImageButton)sender).CommandParameter;
        await DeletePatient(patient.Id);
    }

.._Clicked方法中,sender是被点击的ImageButton
在 xaml 中,CommandParameter="{Binding .}" 将模型项 (Patient) 传递给该方法。

或者如果 DeletePatient 在 YourViewModel 中,即 YourView 的 BindingContext:

// your view's code behind (.xaml.cs):
...
    private async void ItemImageButton_Clicked(object sender, EventArgs e)
    {
        var patient = (Patient)((ImageButton)sender).CommandParameter;
        var vm = (YourViewModel)BindingContext;
        await vm.DeletePatient(patient.Id);
    }