GestureRecognizers.Clear(); doesn't work for iOs

AIoanAIoan Member ✭✭

I am trying to add and then remove a tap gesture from a layout as I need an action to be only happening once. I Add a TapGestureRecognizer to my layout with a Command and a Tapped command. When I later on remove it using myLayout.GestureRecognizers.Clear(); the tap gesture keeps happening every time I tap inside myLayout even though the GesureRecognizers is empty. This works fine on Android.
I've created a sample project and attached the code snippet of xaml and xaml.cs below demonstrating the issue.
Not sure if this is a bug or I am just doing this wrong.

**GestureRecognizerTest.xaml
**<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="GestureRecognizerTest.MainPage" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:GestureRecognizerTest"> <StackLayout x:Name="TestLayout" HorizontalOptions="Center" VerticalOptions="Center"> <Label x:Name="TestLabel" Text="Click me to Add 'A1' ONCE:" /> </StackLayout> </ContentPage>

**GestureRecognizerTest.xaml.cs
**`
namespace GestureRecognizerTest
{
public partial class MainPage : ContentPage
{
public ICommand OnDoSomething { get; set; }

    public MainPage()
    {
        InitializeComponent();
        OnDoSomething = new Command(() => TestLabel.Text += "A");

        AddGesture();
    }
    public void AddGesture()
    {
        TapGestureRecognizer testTapGesture;

        testTapGesture = new TapGestureRecognizer();

        testTapGesture.Tapped += ((o2, e2) =>
        {
            TestLabel.Text += "1";
            RemoveGesture();

        });
        testTapGesture.Command = OnDoSomething;

        TestLayout.GestureRecognizers.Add(testTapGesture);
    }

    public void RemoveGesture()
    {
        TestLayout.GestureRecognizers.Clear();

    }
}

}
`

Sign In or Register to comment.