After hours of searching and trying I couldn't get the following to work.
Expected behavior: Tap on a item in a list once, navigate to a page. Tap 2-3 times (or more) on the same item quickly, still open ONE page and not more.
I have managed to do this, but when using a generic DelegateCommand it just won't work for some reason. I'm using Xamarin.Forms (version 3.1.0.637273) and Prism (version 7.1.0.172-pre).
In my XAML I have a ListView with the following behavior and bindings:
<ListView
CachingStrategy="RecycleElement"
ItemsSource="{Binding MyCollection}">
<ListView.Behaviors>
<b:EventToCommandBehavior
Command="{Binding MyCommand}"
EventName="ItemTapped"
EventArgsParameterPath="Item" />
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
...
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
In my ViewModel I have:
private ObservableCollection<SomeModel> _myCollection;
public ObservableCollection<SomeModel> MyCollection
{
get { return _myCollection; }
set { SetProperty(ref _myCollection, value); }
}
private bool _canNavigate;
public bool CanNavigate
{
get { return _canNavigate; }
set { SetProperty(ref _canNavigate, value); }
}
private DelegateCommand<SomeModel> _myCommand;
public DelegateCommand<SomeModel> MyCommand =>
_someModel ?? (_myCommand = new DelegateCommand<SomeModel>(ExecuteMethod, (parameter) => CanNavigate))
.ObservesProperty(() => CanNavigate);
private async void ExecuteMethod(SomeModel parameter)
{
CanNavigate = false;
NavigationParameters parameters = new NavigationParameters($"?id={parameter.Id}");
await NavigationService.NavigateAsync("SomeCoolPage", parameters);
CanNavigate = true;
}
I have also tried other varients with like this for example with no luck:
private async void ExecuteMethod(SomeModel parameter)
{
if(CanNavigate)
{
CanNavigate = false;
...
...
CanNavigate = true;
}
}
This code works when I'm not using the generic version of the DelegateCommand.
If anyone can point me to what is it I'm doing wrong that would be awesome. Thanks.