TapGestureRecognizer in Xamarin.Forms for Android
Just a very quick and dirty post for other people who might be looking for this. TL;DR the TapGestureRecognizer doesn’t work for more than two taps in Xamarin.Forms.
The problem with TapGestureRecognizer #
Yesterday I spent a too big amount of time looking for the cause of my TapGestureRecognizer not working for Android. Observe the code underneath:
I wanted to create an image which triggered a Command when it is tapped ten times. OK, given, there are probably not many use cases which require this kind of behavior. But on the other hand even Google themselves use it to enable developer-mode these days.
Anyways, this code works just fine on iOS, but on Android it wouldn’t trigger my Command code. Instantly I started debugging, checked for any layout elements overlapping my image, applying InputTransparent everywhere just to rule it out. I even removed everything else, just keeping the image and still it wouldn’t work. I was about to give up, when I did one final attempt: does it work when I set it to require just one tap.. And guess what, it worked!
So, ten taps seems to be too much for Android. Or, at least, for Xamarin.Forms on Android. I’m still not sure why. I haven’t been able to find much on this on Google, part of the reason why I decided to do a small post on this. After I determined that you cannot do more than two taps on Android I decided to investigate some more. As it turns out there is already a bug for this on Bugzilla.
The ‘solution’ #
The solution I decided to do for the time being is to just set the NumberOfTapsRequired property to one and in the Command behind it I keep a simple integer counter which adds up until the number I want it to reach, and only then trigger the logic I want, i.e. enable the developer mode.
Maybe, just maybe, if I happen to find some spare time I might look into the Xamarin.Forms code and see what is going on. Would be awesome to contribute (even a little bit) to Forms at some point..
I hope that this information finds someone that needs it and saves some precious time! If you want to see more about GestureRecognizers, check out this episode of Gur.Codes();.