如何添加带有参数的 tapGestureRecognizer?

Ala*_*an2 2 xamarin xamarin.forms

我的代码目前在后端 C# 中添加了这个

deselectGridLink.GestureRecognizers.Add(NewTapGestureForUpdateCategories(false));

    private TapGestureRecognizer NewTapGestureForUpdateCategories(bool val)
    {
        return new TapGestureRecognizer()
        {
            Command = new Command(() =>
            {
                App.DB.UpdateAllCategoryGroups(val);
                App.DB.UpdateAllCategories(val);
                GetPageData();
                RemoveTableViewClickSection();
                tableView.Root.Add(CreateTableSection());
                SetPageDetails();
            })
        };
    }
Run Code Online (Sandbox Code Playgroud)

如何在 XAML 中添加此参数,还包括 false 参数。此外,如果我以某种方式将其添加到 XAML,是否需要更改 C#f NewTapGestureForUpdateCategories 方法?:

<Grid x:Name="deselectGridLink" VerticalOptions="CenterAndExpand" Padding="20, 0">
   <Label TextColor="Blue" Style="{DynamicResource ListItemTextStyle}" x:Name="deselectLink" HorizontalOptions="StartAndExpand" VerticalOptions="Center" Text="Deselect All" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

nat*_*rip 11

<ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <Grid Padding="10">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="30"></RowDefinition>
                                            <RowDefinition Height="*"></RowDefinition>
                                            <RowDefinition Height="*"></RowDefinition>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                            <ColumnDefinition Width="50"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>

                                        <Label Grid.Row="0" Grid.Column="0" VerticalTextAlignment="Center" Text="{Binding UserName}" LineBreakMode="TailTruncation" TextColor="{StaticResource DarkTextColor}" FontSize="Medium"></Label>
                                        <Label Grid.Row="1" Grid.Column="0" VerticalTextAlignment="Center" Text="{Binding Mobile}" TextColor="{StaticResource DarkTextColor}" FontSize="Small" />
                                        <Label Grid.Row="2" Grid.Column="0" VerticalTextAlignment="Center" Text="{Binding Email}" TextColor="{StaticResource DarkTextColor}" FontSize="Small" />
                                        <Label Grid.Column="1" Grid.RowSpan="3" IsVisible="{Binding IsAdmin}" HorizontalOptions="Center" VerticalOptions="Center"
                                               TextColor="Red" FontSize="Large"
                                               Text="&#xf2ed;" FontFamily="Font Awesome 5 Free-Solid-900.otf#Font Awesome 5 Free Regular" >
                                            <Label.GestureRecognizers>
                                                <TapGestureRecognizer Tapped="Status_Clicked" CommandParameter="{Binding UserId}"/>
                                            </Label.GestureRecognizers>
                                        </Label>
                                    </Grid>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>




async private void Status_Clicked(object sender, EventArgs e)
        {
                    Label lblClicked = (Label)sender;
                    var item = (TapGestureRecognizer)lblClicked.GestureRecognizers[0];
                    var id=item.CommandParameter;
        }
Run Code Online (Sandbox Code Playgroud)


Col*_*SFT 8

XAML:

<Grid x:Name="deselectGridLink" VerticalOptions="CenterAndExpand" Padding="20, 0">
    <Label TextColor="Blue" 
           Style="{DynamicResource ListItemTextStyle}" 
           x:Name="deselectLink" 
           HorizontalOptions="StartAndExpand" 
           VerticalOptions="Center" 
           Text="Deselect All" >
     <Label.GestureRecognizers>
        //!!!!!!!!!!!!!!!!!!!!!!!!!set parameter here.
        <TapGestureRecognizer Command="{Binding TapCommand}" CommandParameter="false"/>
     </Label.GestureRecognizers>

    </Label>
</Grid>
Run Code Online (Sandbox Code Playgroud)

背后代码:

public partial class YourPage : ContentPage
{
    public Command TapCommand
    {
        get
        {
            return new Command(val => {
                DisplayAlert("Alert", val.ToString(), "OK");
            });
        }
    }

    public YourPage()
    {
        InitializeComponent();
        this.BindingContext = this;
    }
}
Run Code Online (Sandbox Code Playgroud)

当您在 Label.GestureRecognizers 中的CommandParameter上设置值时,后面代码中的 TapCommand 可以接收它。