Android上的Xamarin.Forms TapRecognizer取消了ItemTapped事件

时间:2018-01-02 14:07:26

标签: 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

ExecuteDropdownCommand

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

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

if (comm.Command == null) return;

comm.Command.ExecuteIfPossible(comm.CommandParameter);

DropdownOptionsCommandsObservableCollection

OptionCommands
 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;
    }

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

谢谢。

1 个答案:

答案 0 :(得分:1)

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

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