【问题标题】:Xamarin.Forms TapRecognizer on android cancels ItemTapped Eventandroid 上的 Xamarin.Forms TapRecognizer 取消 ItemTapped 事件
【发布时间】:2018-01-02 14:07:26
【问题描述】:

我在 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);

DropdownOptionsCommandsObservableCollectionOptionCommands

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

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

谢谢。

【问题讨论】:

标签: c# xaml xamarin.forms xamarin.android


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    相关资源
    最近更新 更多