In the WPF MVVM world, it is fairly common for projects to write their
own simple command implementations, sometimes called DelegateCommand,
RelayCommand or simply Command. Since ICommand uses object as the
parameter type, it is also common for developers using CommandParameter
often to implement a generic Command to avoid casting boilerplate.
For the same reason, this adds a strongly typed AsyncCommand that
simply wraps an AsyncCommand, relaying CanExecutedChanged and
PropertyChanged events as well as the Execute call, casting the
parameter.
using Nito.Mvvm;
using System.IO;
using System.Threading.Tasks;
namespace Example
{
public enum Fruit { Apple }
public class MainWindowViewModel
{
public MainWindowViewModel()
{
UpdateLastPickedCommand = new AsyncCommand<Fruit>(UpdateLastPicked);
}
public IAsyncCommand<Fruit> UpdateLastPickedCommand { get; }
private Task UpdateLastPicked(Fruit fruitClicked)
{
return File.WriteAllTextAsync("LastFruitPicked.txt", fruitClicked.ToString());
}
}
}
This basically allows:
// Current way
UpdateLastPickedCommand = new AsyncCommand((fruit) => UpdateLastPicked((Fruit)fruit));
// With strongly typed AsyncCommand
UpdateLastPickedCommand = new AsyncCommand<Fruit>(UpdateLastPicked);
Description
In the WPF MVVM world, it is fairly common for projects to write their own simple command implementations, sometimes called DelegateCommand, RelayCommand or simply Command. Since ICommand uses object as the parameter type, it is also common for developers using CommandParameter often to implement a generic Command to avoid casting boilerplate.
For the same reason, this adds a strongly typed AsyncCommand that
simply wraps an AsyncCommand, relaying CanExecutedChanged and
PropertyChanged events as well as the Execute call, casting the
parameter.
Example usage
XAML:
c#:
This basically allows: