Reputation: 907
I have created a custom control on Xamarin Forms that contains a gesture recognizer.
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);
}
...
when i use the above control on a ListView the ItemTapped
Event when running on Android never gets fired. On UWP the event gets fired as expected.
ListView Test Case:
<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>
Implementation of EventToCommandBehavior on github
ExecuteDropdownCommand
if (!(e is ItemTappedEventArgs ev)) return;
if (!(ev.Item is OptionCommands comm)) return;
if (comm.Command == null) return;
comm.Command.ExecuteIfPossible(comm.CommandParameter);
DropdownOptionsCommands
is an ObservableCollection
of 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;
}
If you need more info please leave a comment.
Thank you.
Upvotes: 2
Views: 230
Reputation: 2718
I would try to put a background colour to the ListView!
Sounds like a similar problem:
https://forums.xamarin.com/discussion/99978/different-tap-handling-in-android-and-uwp
Just posted it as an answer, it could help someone someday.
Upvotes: 2