I am working on a Xamarin.Forms project with a ListView.
The XAML for the ListView is
I prefer to use the SelectedItem property to handle simple clicks. This works platform independent and everything can be done in the view model code.
The trick is to set the property null again to deselect the item immediately after evaluating it (in hte sample I use it for initialising another view model).
XAML:
ViewModel.cs:
public Command ViewDetailsCommand;
public ViewModel()
{
ViewDetailsCommand = new Command(async s => await ViewDetails(s));
}
public IProduct SelectedProduct
{
get { return _selectedProduct; }
set
{
if (value != _selectedProduct)
{
SetProperty(ref _selectedProduct, value);
if (value != null)
{
ViewDetailsCommand.Execute(value);
}
}
}
}
private async Task ViewDetails(IProduct product)
{
var viewModel = AppContainer.Container.Resolve();
viewModel.Initialise(this, product as ShoppingListItemViewModel);
SelectedProduct = null;
await _pageNavigator.PushModalAsync(viewModel);
}