Visual Studio 2022 中的 Xamarin Forms Android => ImageButton TapGestureRecognizer 不工作
Xamarin Forms Android in Visual Studio 2022 => ImageButton TapGestureRecognizer not working
我的 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>
从未调用 ImageButton 中的 TapGestureRecognizer 命令。
如果我删除识别器并在 ImageButton 中放置一个普通命令 属性 然后调用该命令。
我用了 2 天,尝试了多个测试和互联网浏览。
有谁能帮帮我吗?
注意:水龙头在 中工作良好:
.BoxView
.标签控件
.当我评论ImageButton.GestureRecognizers并将Command直接作为ImageButton的属性时。
!!!已找到解决方案 !!!
我将 ImageButton 包含在 StackLayout 中,并在此 StackLayout 中包含识别器,而不再包含在 ImageButton 中。
好吧,问题似乎解决了。
您只需将 ImageButton
移动到 StackLayout
。您必须确保 Recognizers
在您创建的 StackLayout
中,而不是在 ImageButton
中
我的 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>
从未调用 ImageButton 中的 TapGestureRecognizer 命令。
如果我删除识别器并在 ImageButton 中放置一个普通命令 属性 然后调用该命令。
我用了 2 天,尝试了多个测试和互联网浏览。
有谁能帮帮我吗?
注意:水龙头在 中工作良好:
.BoxView
.标签控件
.当我评论ImageButton.GestureRecognizers并将Command直接作为ImageButton的属性时。
!!!已找到解决方案 !!!
我将 ImageButton 包含在 StackLayout 中,并在此 StackLayout 中包含识别器,而不再包含在 ImageButton 中。
好吧,问题似乎解决了。
您只需将 ImageButton
移动到 StackLayout
。您必须确保 Recognizers
在您创建的 StackLayout
中,而不是在 ImageButton