简体   繁体   English

Visual Studio 2022 中的 Xamarin Forms Android => ImageButton TapGestureRecognizer 不工作

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

My xaml is:我的 xaml 是:

        <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.永远不会调用 ImageButton 中的 TapGestureRecognizer 命令。
If I delete the recognizer and put a normal Command property in the ImageButton then the command is called.如果我删除识别器并在 ImageButton 中放置一个普通的 Command 属性,则会调用该命令。
I am on this for 2 days, trying multiple tests and Inte.net browsing.我在这上面待了 2 天,尝试了多项测试和 Inte.net 浏览。
Can anyone help me, please?任何人都可以帮助我吗?
Note: the Taps work well in :注意:水龙头适用于
.BoxView .BoxView
.Label control .Label 控制
.When I comment ImageButton.GestureRecognizers and put the Command directly as property of the ImageButton. .当我评论 ImageButton.GestureRecognizers 并将命令直接作为 ImageButton 的属性时。

!!! !!! Solution found !!!找到解决方案!!!
I enclosed the ImageButton in a StackLayout with the Recognizers in this StackLayout and no more in the ImageButton.我将 ImageButton 封装在 StackLayout 中,识别器在此 StackLayout 中,不再包含在 ImageButton 中。
Well, problem seems resolved.好吧,问题似乎解决了。

All you need to do is move the ImageButton into a StackLayout .您需要做的就是将ImageButton移到StackLayout中。 You have to make sure that the Recognizers are inside this StackLayout you created instead of being inside the ImageButton您必须确保Recognizers器位于您创建的StackLayout内,而不是位于ImageButton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM