I am working on a Xamarin.Forms page displaying a list of items using MVVM pattern.
I can not get the Command property of the TapGestureRecognizer object of the list items working. It does not even fire, no matter what I try. This command property should reference a single ICommand property on the VM for all the listview elements. The CommandParameter property should select between the elements based on their Id.
The Tapped event seems to be working. I am not sure whether my databinding of the Command property to the VM is faulty or Xamarin.Forms has a bug here.
I have tried different settings for the data binding of TapGestureRecognizer's Command property to my ViewModel and I have also played with the InputTransparent properties on the nested View-elements. No change here: Command does not fire
View XAML header:
<ContentPage ...
x:Class="Ch9.MainPage2"
x:Name="page"
>
View has a ViewModel property which is the viewmodel:
public partial class MainPage2 : ContentPage
{
public MainPage2ViewModel ViewModel
{
get => BindingContext as MainPage2ViewModel;
set => BindingContext = value;
}
public MainPage2()
{
InitializeComponent();
ViewModel = new MainPage2ViewModel(
...injecting components...
);
}
}
the viewmodel has an ItemTappedCommand property:
public ICommand ItemTappedCommand { get; private set; }
...
ItemTappedCommand = new Command<int>(async id => await OnItemTappedCommand(id));
Inside the page I have a stacklayout as content-root and inside the stacklayout there is a listview. To keep the code small, I have omitted what I could, what I think is not relevant
<ListView
ItemsSource="{Binding SearchResults}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer
BindingContext="{x:Reference page}"
Command="{Binding Path=ViewModel.ItemTappedCommand}" CommandParameter="{Binding Id}" NumberOfTapsRequired="1"/>
</StackLayout.GestureRecognizers>
<Image InputTransparent="True">
<Image.Source>
<UriImageSource Uri="{Binding ImgSmSrc}"
CacheValidity="0"
CachingEnabled="False"
/>
</Image.Source>
</Image>
<StackLayout InputTransparent="True">
some content...
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>