Hi
I am unable to get ItemTapped working. I am using VS 2019/XAML/C#.
When I click on a selected item, nothing happens.
I have following xaml code in my ItemsPage.xaml:
The ItemsPage. c has following code:
The ItemsViewModel.cs codes is as follows:
When a displayed Item is clicked, nothing happens. I am expecting the following method to be triggered :
Any suggestions to resolve problem would be greatly appreciated.
Thanks
I am unable to get ItemTapped working. I am using VS 2019/XAML/C#.
When I click on a selected item, nothing happens.
I have following xaml code in my ItemsPage.xaml:
C#:
<StackLayout.GestureRecognizers>
<TapGestureRecognizer
NumberOfTapsRequired="1"
Command="{Binding Source={RelativeSource AncestorType={x:Type local:ItemsViewModel}}, Path=ItemTapped}"
CommandParameter="{Binding .}">
</TapGestureRecognizer>
</StackLayout.GestureRecognizers>
</StackLayout>
The ItemsPage. c has following code:
C#:
namespace IRKL.Views
{
public partial class ItemsPage : ContentPage
{
private readonly ItemsViewModel _viewModel;
readonly ObservableCollection<Item> items = new ObservableCollection<Item>();
public ObservableCollection<Item> Item { get { return items; } }
public ItemsPage()
{
InitializeComponent();
BindingContext = _viewModel = new ItemsViewModel();
items = new ObservableCollection<Item>()
{
new Item { Id = Guid.NewGuid().ToString(), Text = "TEST", Description = "TEST" },
};
BindingContext = this;
}
protected override void OnAppearing()
{
base.OnAppearing();
_viewModel.OnAppearing();
//build data for ItemsPage
}
}
}
The ItemsViewModel.cs codes is as follows:
C#:
namespace IRKL.ViewModels
{
public class ItemsViewModel : BaseViewModel
{
private Item _selectedItem;
public ObservableCollection<Item> Items { get; }
public Command LoadItemsCommand { get; }
public Command AddItemCommand { get; }
public Command<Item> ItemTapped { get; }
public ItemsViewModel()
{
//Title = "Browse";
Items = new ObservableCollection<Item>();
//LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());
ItemTapped = new Command<Item>(OnItemSelected);
AddItemCommand = new Command(OnAddItem);
}
async Task ExecuteLoadItemsCommand()
{
IsBusy = true;
try
{
Items.Clear();
var items = await DataStore.GetItemsAsync(true);
foreach (var item in items)
{
Items.Add(item);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
public void OnAppearing()
{
IsBusy = true;
SelectedItem = null;
}
public Item SelectedItem
{
get => _selectedItem;
set
{
SetProperty(ref _selectedItem, value);
OnItemSelected(value);
}
}
private async void OnAddItem(object obj)
{
await Shell.Current.GoToAsync(nameof(NewItemPage));
}
async void OnItemSelected(Item item)
{
if (item == null)
return;
await Shell.Current.GoToAsync($"{nameof(ItemDetailPage)}?{nameof(ItemDetailViewModel.ItemId)}={item.Id}");
}
protected bool SetProperty<T>(ref T field, T newValue, [CallerMemberName] string propertyName = null)
{
if (!Equals(field, newValue))
{
field = newValue;
return true;
}
return false;
}
private System.Collections.IEnumerable item1;
public System.Collections.IEnumerable Item { get => item1; set => SetProperty(ref item1, value); }
}
}
When a displayed Item is clicked, nothing happens. I am expecting the following method to be triggered :
C#:
async void OnItemSelected(Item item)
{
if (item == null)
return;
await Shell.Current.GoToAsync($"{nameof(ItemDetailPage)}?{nameof(ItemDetailViewModel.ItemId)}={item.Id}");
}
Any suggestions to resolve problem would be greatly appreciated.
Thanks