Skip to content

Instantly share code, notes, and snippets.

@Plus1XP
Created October 14, 2019 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Plus1XP/4e687b08db75a9f05c16569f5abdbfbf to your computer and use it in GitHub Desktop.
Save Plus1XP/4e687b08db75a9f05c16569f5abdbfbf to your computer and use it in GitHub Desktop.
RelayCommand.cs
public class RelayCommandAsync<T> : ICommand
{
private readonly Func<T, Task> executedMethod;
private readonly Func<T, bool> canExecuteMethod;
public event EventHandler CanExecuteChanged;
public RelayCommandAsync(Func<T, Task> execute) : this(execute, null) { }
public RelayCommandAsync(Func<T, Task> execute, Func<T, bool> canExecute)
{
this.executedMethod = execute ?? throw new ArgumentNullException("execute");
this.canExecuteMethod = canExecute;
}
public bool CanExecute(object parameter) => this.canExecuteMethod == null || this.canExecuteMethod((T)parameter);
public async void Execute(object parameter) => await this.executedMethod((T)parameter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment