tmighty
tmighty

Reputation: 11399

Adding 2 TapGestureRecognizer not possible?

I have 2 TapGestureRecognizers on my custom control:

1) The first one is internal / exists only within the custom control.

2) The second one is attached on the page on which the custom control is instanciated.

I'm using the first TapGestureRecognizer to trigger an animation at a Tap internally / within the custom control, and the second TapGestureRecognizer is used to track Taps on the custom control on the Page so that I can react to taps.

It feels wrong to do the animation "outside" / on the Page as every instance of this control should animate, that's why I attached a TapGestureRecognizer within the custom control.

However, when I do this, only the "internal" TapGestureRecognizer works, the one on the outside doesn't.

Is that normal behaviour?

public class clsGridCell : ContentView
{

     var _Grid1ContainerForImageAndLabel = new Grid()
     {
     }

     var nTapRec = new TapGestureRecognizer();
     nTapRec.Tapped += OnItemSelected;
     _Grid1ContainerForImageAndLabel.GestureRecognizers.Add(nTapRec);

     this.Content = _Grid1ContainerForImageAndLabel;

     }

     private async void OnItemSelected(object sender, EventArgs e)
     {
         await Task.WhenAny<bool>
         (
            _image1.ScaleTo(0.9, 50, Easing.Linear)
         );

        //run some background color animation, too
    }

And "outside" / on the Page:

public class MainPage : ContentPage
{
     var nGridCell = new clsGridCell                            
     {
         ImageSource = nImgSrc,
         BackgroundColor = Color.Blue;
     };

     _BigGrid.Children.Add(nGridCell);

     var nTapRec = new TapGestureRecognizer();
     nTapRec.Tapped += OnItemSelected;
     nGridCell.GestureRecognizers.Add(nTapRec);

    private async void OnItemSelected(object sender, EventArgs e)
    {
         //Not fired! When I remove the "internal" TapGestureRecognizer, it does work

Upvotes: 0

Views: 76

Answers (1)

Luccas Clezar
Luccas Clezar

Reputation: 1074

Just create the internal TapGestureRecognizer as a public/internal property of the class and, instead of creating a new Gesture "outside", add a new Tapped action to the class' TapGestureRecognizer. Like this:

public class clsGridCell : ContentView     
{ 
    public TapGestureRecognizer TapGesture { get; set; }

    Action<clsGridCell> tap;
    public Action<clsGridCell> Tap
    {
        get => tap;
        set
        {
            tap = value;
            TapGesture.Tapped += (sender, e) => { value(this); };
        }
    }

    public clsGridCell()
    {
        var _Grid1ContainerForImageAndLabel = new Grid() { };

        TapGesture = new TapGestureRecognizer(); 
        TapGesture.Tapped += OnItemSelected; 
        _Grid1ContainerForImageAndLabel.GestureRecognizers.Add(TapGesture); 

        this.Content = _Grid1ContainerForImageAndLabel; 
    } 

    private async void OnItemSelected(object sender, EventArgs e) 
    { 
        await Task.WhenAny<bool> ( _image1.ScaleTo(0.9, 50, Easing.Linear) ); //run some background color animation, too
    }
}

And outside use myGrid.Tap = Method;

Upvotes: 1

Related Questions