[FIXED] How can I add a tapGestureRecognizer with a parameter?

Issue

My code currently adds this in the back end 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();
            })
        };
    }

How can I add this in XAML with the parameter of false also included. Also if I somehow add this to XAML do I need to change the C#f NewTapGestureForUpdateCategories method?:

<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>

Solution

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>

Code behind:

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

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

When you set value on CommandParameter in Label.GestureRecognizers , the TapCommand in code behind can receive it .

Answered By – ColeX

Answer Checked By – Mary Flores (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published