Xamarin.forms: GestureRecognizers does not work with button/ImageButton

Created on 7 Nov 2018  Â·  4Comments  Â·  Source: xamarin/Xamarin.Forms

Description

I'm unable to use GestureRecognizers on a button. Putting GestureRecognizers on StackLayout, ContentView etc is working fine.

Steps to Reproduce

  1. Declare some GestureRecognizers on button.

Expected Behavior

GestureRecognizers like Tapped and Swiped should work as expected.

Actual Behavior

None of those GestureRecognizers gets called

Basic Information

  • Version with issue:
  • Last known good version: ?
  • IDE: VS 15.8.5
  • Platform Target Frameworks:

    • Android: 8.1

  • Nuget Packages: XF 3.3.0.967583
  • Affected Devices: I'm using Genymotion emulator with android 8

    Screenshots

N/A

Reproduction Link

https://github.com/ysmoradi/XFButtonGestureRecognizersIssue/blob/master/src/App/App.xaml

<?xml version="1.0" encoding="utf-8" ?>
<Application
    x:Class="XFButtonGestureRecognizersIssue.App"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <Application.MainPage>
        <ContentPage x:Name="Page">
            <StackLayout CompressedLayout.IsHeadless="True">

                <Button Text="Test 1">
                    <View.GestureRecognizers>
                        <ClickGestureRecognizer Clicked="TapGestureRecognizer_Tapped" />
                        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
                    </View.GestureRecognizers>
                </Button>

                <Label Text="Test 2">
                    <View.GestureRecognizers>
                        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
                    </View.GestureRecognizers>
                </Label>

            </StackLayout>
        </ContentPage>
    </Application.MainPage>
</Application>
public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }

        void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
            ((VisualElement)sender).BackgroundColor = Color.LightBlue;
        }
    }
gestures 🖖 imagebutton 6 Android proposal-open enhancement ➕

Most helpful comment

I'm facing the same issue with ImageButton too (XF 3.4)

All 4 comments

Hi, this never worked in XF, so is not really a bug, more a enhancement. Normally user's use the click event.
Marking this to be look further as an enhancement.
Can you please provide a scenario you need this also to help us out.

Thanks.

It's working with images, content views etc.
It has a problem with button only. I'm willing to have double tap.
TapGestureRecognizer has a property for this. (NumberOfRequiredTap). It's also useful for right click on UWP.

I'm facing the same issue with ImageButton too (XF 3.4)

I'm facing this same issue in Image control. It's working fine in phones but not working in iPad.

<ContentView AbsoluteLayout.LayoutBounds="0.99,5,25,25" AbsoluteLayout.LayoutFlags="XProportional">
        <Image Source="uploadcamera" Aspect="AspectFit" />
        <ContentView.GestureRecognizers>
              <TapGestureRecognizer Command="{Binding ChangePhotoCommand}" NumberOfTapsRequired="1"/>
        </ContentView.GestureRecognizers>
</ContentView>

Work around:
I have fix this issue by replacing Absolute Layout with Grid Layout

Was this page helpful?
0 / 5 - 0 ratings