c# - Android 上的 Xamarin.Forms TapRecognizer 取消 ItemTapped 事件

标签 c# xaml xamarin.forms xamarin.android

我在 Xamarin Forms 上创建了一个包含手势识别器的自定义控件。

ImageLabelControl.xaml

<ContentView.Content>
    <StackLayout x:Name="Container"
                 HorizontalOptions="FillAndExpand">
        <StackLayout.GestureRecognizers>
            <TapGestureRecognizer x:Name="Recognizer" Tapped="Recognizer_OnTapped" />
        </StackLayout.GestureRecognizers>
        <Image x:Name="ImageCell" />
        <Label x:Name="LabelCell" />
    </StackLayout>
</ContentView.Content>

ImageLabelControl.xaml.cs

 ...

 private void Recognizer_OnTapped(object sender, EventArgs e)
 {
       //ExecuteIfPossible just checks if the CanExecute of the ICommand returns true
       Command?.ExecuteIfPossible(CommandParameter);
 }

 ...

当我在 ListView 上使用上述控件时,在 Android 上运行时的 ItemTapped 事件永远不会被触发。在 UWP 上,事件按预期触发。

ListView测试用例:

 <ListView ItemsSource="{Binding DropdownOptionsCommands}">
            <ListView.Behaviors>
                <behaviors:EventToCommandBehavior
                    EventName="ItemTapped"
                    Command="{Binding ExecuteDropdownCommand}" />
            </ListView.Behaviors>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <controls:ImageLabelControl WidthRequest="200"
                                                    Orientation="Horizontal"
                                                    ImageSource="{Binding ImageUrl,Converter={StaticResource ImageConverter}}"
                                                    ImageHeightRequest="30"
                                                    ImageMargin="20,5,20,5"
                                                    ImageVerticalOptions="Center"
                                                    Text="{Binding Text}"
                                                    LabelMargin="20,5,20,5"
                                                    VerticalTextAlignment="Center"
                                                    LabelVerticalOptions="Center"/>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

github 上的 EventToCommandBehavior 实现

执行DropdownCommand

if (!(e is ItemTappedEventArgs ev)) return;

if (!(ev.Item is OptionCommands comm)) return;

if (comm.Command == null) return;

comm.Command.ExecuteIfPossible(comm.CommandParameter);

DropdownOptionsCommandsOptionCommandsObservableCollection

 public class OptionCommands
{
    public string ImageUrl { get; set; }

    public string Text { get; set; }

    public ICommand Command { get; set; }

    public object CommandParameter { get; set; }

    public OptionCommands()
    { }

    public OptionCommands(string text, ICommand command, object parameter = null)
    {
        Text = text;
        Command = command;
        CommandParameter = parameter;
    }

    public OptionCommands(string imageUrl, string text, 
    ICommand command, object parameter = null) : this(text, command, parameter)
    {
        ImageUrl = imageUrl;
    }

如果您需要更多信息,请发表评论。

谢谢。

最佳答案

我会尝试为 ListView 添加背景颜色!
听起来像类似的问题:
https://forums.xamarin.com/discussion/99978/different-tap-handling-in-android-and-uwp

刚刚将其作为答案发布,有一天它可以帮助某人。

关于c# - Android 上的 Xamarin.Forms TapRecognizer 取消 ItemTapped 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48062436/

相关文章:

c# - 如何在 Windows Phone 8.1 中设置支持的方向属性

ios - Xamarin 形式 : How to handle the notification click in ios

xamarin.forms - 如何为控件模板提供 BindingContext

c# - 在密码框的某些事件上显示密码字符

C# 接口(interface) - 隐式转换错误?

c# - 具有不同缩进大小的文件的 Visual Studio 2015 缩进行为

c# - 如何在 Xamarin 中创建和运行 PCL 代码测试?

c# - 如何绑定(bind)到 XAML 中实例化类的属性

Xamarin Forms 图像未从远程 URL 加载

c# - 在 C# 中使用 WMI 查询通过盘符获取硬盘序列号