我在 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);
DropdownOptionsCommands
是 OptionCommands
的 ObservableCollection
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/