#pragma warning disable SA1402 namespace FluentTest.Commanding { using System; using System.Diagnostics; using System.Windows.Input; public class RelayCommand : IRelayCommand { private readonly Action action; private readonly Func canExecute; protected RelayCommand() { } public RelayCommand(Action execute) { this.action = execute; } public RelayCommand(Action execute, Func canExecute) : this(execute) { this.canExecute = canExecute; } #region IRelayCommand Members [DebuggerStepThrough] public virtual bool CanExecute(object parameter) { return this.canExecute is null || this.canExecute(); } /// public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public event EventHandler Executed; public event EventHandler Executing; protected void OnExecuted() { this.Executed?.Invoke(this, null); } protected void OnExecuting() { this.Executing?.Invoke(this, null); } public void Execute(object parameter) { this.OnExecuting(); this.InvokeAction(parameter); this.OnExecuted(); } protected virtual void InvokeAction(object parameter) { this.action(); } #endregion // IRelayCommand Members } /// /// A command whose sole purpose is to /// relay its functionality to other /// objects by invoking delegates. The /// default return value for the CanExecute /// method is 'true'. /// public class RelayCommand : RelayCommand { private readonly Action action; private readonly Func canExecute; protected RelayCommand() { } /// /// Initializes a new instance of the class. /// /// The execution logic. public RelayCommand(Action action) : this(action, null) { } /// /// Initializes a new instance of the class. /// /// The execution logic. /// The execution status logic. public RelayCommand(Action action, Func canExecute) { this.action = action; this.canExecute = canExecute; } /// /// Defines the method that determines whether the command can execute in its current state. /// /// /// true if this command can be executed; otherwise, false. /// /// Data used by the command. If the command does not require data to be passed, this object can be set to null. public override bool CanExecute(object parameter) { return this.canExecute is null || this.canExecute((T)parameter); } protected override void InvokeAction(object parameter) { this.action((T)parameter); } } }