Answers for "Async RelayCommand"

0

Async RelayCommand

public class RelayCommandAsync : ICommand
    {
        private readonly Func executedMethod;
        private readonly Func canExecuteMethod;

        public event EventHandler CanExecuteChanged;
        public RelayCommandAsync(Func execute) : this(execute, null) { }

        public RelayCommandAsync(Func execute, Func 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);
        public void OnCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
    }
Posted by: Guest on August-26-2021

Browse Popular Code Answers by Language