xamarin 如何添加带参数的tapGestureRecognizer?

ct3nt3jp  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(140)

我的代码目前在后端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();
            })
        };
    }

我如何在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>
9fkzdhlc

9fkzdhlc2#

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>

后面的代码:

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

    public YourPage()
    {
        InitializeComponent();
        this.BindingContext = this;
    }
}

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

相关问题