using System; using System.Collections.Generic; using System.Text; using System.Windows.Input; namespace Sarand.Net.Standard.Base.Core { //======================================================================= /// /// Code contract for commands - menu options, button actions, keyboard accelerators etc. /// /// public class SacsCommand : ICommand { //------------------------------------------------------------------- // Definitions. public delegate bool CanExecuteFn (object commandParam); public delegate void ExecuteFn (object commandParam); //------------------------------------------------------------------- // Data members. private readonly ExecuteFn executeFn; private readonly CanExecuteFn canExecuteFn; //------------------------------------------------------------------- // Construction. public SacsCommand (ExecuteFn executeFn, CanExecuteFn canExecuteFn = null) { this.executeFn = executeFn; this.canExecuteFn = canExecuteFn; } //------------------------------------------------------------------- // Properties. public String CommandId { get; set; } public String Caption { get; set; } //------------------------------------------------------------------- /// /// CanExecuteChanged event handler. /// Unclear when this fires as Command won't know when state changes. /// public event EventHandler CanExecuteChanged; protected void FireCanExecuteChanged () { CanExecuteChanged?.Invoke(this, EventArgs.Empty); } //------------------------------------------------------------------- /// Determine whether this command can be executed. public virtual bool CanExecute (object commandParam) { return canExecuteFn?.Invoke(commandParam) ?? false; } //------------------------------------------------------------------- /// /// Execute command. /// Only invokes command method if comand can be executed. /// public virtual void Execute (object commandParam) { if (!CanExecute(commandParam)) return; executeFn?.Invoke(commandParam); } //------------------------------------------------------------------- } //======================================================================= }