jmdess
jmdess

Reputation: 73

Xamarin Forms Android in Visual Studio 2022 => ImageButton TapGestureRecognizer not working

My xaml is :

        <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="displayItemTemplate">
                <StackLayout>
                    <Frame  CornerRadius="3" HasShadow="False" Margin="10,2" Padding="5,0"
                            BorderColor="{Binding BindingContext.AppSettings[MPLVItemFrameBorderColor], Source={x:Reference this}}"
                            BackgroundColor="{Binding BindingContext.AppSettings[MPLVItemFrameBackgroundColor], Source={x:Reference this}}">
                        <StackLayout Orientation="Horizontal">
                            <BoxView BackgroundColor="Transparent" Color="{Binding Status, Converter={StaticResource NoteStatusToColorConverter}}" Margin="4" HeightRequest="32" WidthRequest="32" CornerRadius="5">
                                <BoxView.GestureRecognizers>
                                    <TapGestureRecognizer Command="{Binding Source={x:Reference this}, Path=BindingContext.NoteStatusSwitchCommand}" CommandParameter="{Binding .}"/>
                                </BoxView.GestureRecognizers>
                            </BoxView>
                            <Label BackgroundColor="Transparent" Text="{Binding Description}" TextDecorations="{Binding Status, Converter={StaticResource NoteStatusConverter}}" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Start">
                                <Label.GestureRecognizers>
                                    <TapGestureRecognizer NumberOfTapsRequired="2" Command="{Binding Source={x:Reference this}, Path=BindingContext.NoteEditCommand}" CommandParameter="{Binding .}"/>
                                </Label.GestureRecognizers>
                            </Label>
                            <ImageButton BackgroundColor="Transparent" Source="{Binding ItemImagePath, Converter={StaticResource ImagePathToImageSourceConverter}}" HeightRequest="32" WidthRequest="32"><!--Command="{Binding Source={x:Reference this}, Path=BindingContext.ItemImage1TapCommand}" CommandParameter="{Binding .}">-->
                                <ImageButton.GestureRecognizers>
                                    <TapGestureRecognizer NumberOfTapsRequired="1" Command="{Binding Source={x:Reference this}, Path=BindingContext.ItemImage1TapCommand}" CommandParameter="{Binding .}"/>
                                    <!--<TapGestureRecognizer NumberOfTapsRequired="2" Command="{Binding Source={x:Reference this}, Path=BindingContext.ItemImage2TapsCommand}" CommandParameter="{Binding .}"/>-->
                                </ImageButton.GestureRecognizers>
                            </ImageButton>
                            <ImageButton BackgroundColor="Transparent" Source="NoteDelete.png" HeightRequest="32" WidthRequest="32"
                                         Command="{Binding Source={x:Reference this}, Path=BindingContext.NoteDeleteCommand}" CommandParameter="{Binding .}"/>
                        </StackLayout>
                    </Frame>
                </StackLayout>
            </DataTemplate>
            <DataTemplate x:Key="emptyItemTemplate">
                <Grid/>
            </DataTemplate>

The TapGestureRecognizer Command in the ImageButton is never called.
If I delete the recognizer and put a normal Command property in the ImageButton then the command is called.
I am on this for 2 days, trying multiple tests and Internet browsing.
Can anyone help me, please?
Note: the Taps work well in :
    .BoxView
    .Label control
    .When I comment ImageButton.GestureRecognizers and put the Command directly as property of the ImageButton.

!!! Solution found !!!
I enclosed the ImageButton in a StackLayout with the Recognizers in this StackLayout and no more in the ImageButton.
Well, problem seems resolved.

Upvotes: 0

Views: 220

Answers (1)

Halo
Halo

Reputation: 2827

All you need to do is move the ImageButton into a StackLayout. You have to make sure that the Recognizers are inside this StackLayout you created instead of being inside the ImageButton

Upvotes: 1

Related Questions