using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace Tango.Core.Commands { public class RelayCommand : ICommand { protected Action _action; protected Func _canExecute; public event EventHandler Executed; public RelayCommand(Action action, Func canExecute) { _action = action; _canExecute = canExecute; } public RelayCommand(Action action, Func canExecute) : this(action, new Func((x) => canExecute())) { } public RelayCommand(Action action) : this(action, new Func((x) => true)) { } public RelayCommand(Action action, Func canExecute) : this((x) => action(), canExecute) { } public RelayCommand(Action action) : this((x) => action(), new Func((x) => true)) { } public RelayCommand(Action action, Func canExecute) : this((x) => action(), canExecute) { } public virtual bool CanExecute(object parameter) { try { return _canExecute != null ? _canExecute(parameter) : true; } catch (Exception ex) { Debug.WriteLine("Error on CanExecute RelayCommand\n" + ex); return false; } } public virtual void Execute(object parameter) { _action?.Invoke(parameter); Executed?.Invoke(this, parameter); } public void RaiseCanExecuteChanged() { CanExecuteChanged?.Invoke(this, new EventArgs()); } public event EventHandler CanExecuteChanged; } public class RelayCommand : RelayCommand { public new event EventHandler Executed; public RelayCommand(Action action, Func canExecute) : base((x) => action((T)x), (x) => canExecute((T)x)) { } public RelayCommand(Action action, Func canExecute) : this(action, new Func((x) => canExecute())) { } public RelayCommand(Action action) : base((x) => action((T)x)) { } public override void Execute(object parameter) { _action?.Invoke(parameter); Executed?.Invoke(this, parameter != null ? (T)parameter : default(T)); } } public class DelayedRelayCommand : RelayCommand { public TimeSpan Delay { get; set; } public DelayedRelayCommand(Action action) : base(action) { } public DelayedRelayCommand(Action action) : base(action) { } public DelayedRelayCommand(Action action, Func canExecute) : base(action, canExecute) { } public DelayedRelayCommand(Action action, Func canExecute) : base(action, canExecute) { } public DelayedRelayCommand(Action action, Func canExecute) : base(action, canExecute) { } public DelayedRelayCommand(Action action, Func canExecuteChange) : base(action, canExecuteChange) { } public override async void Execute(object parameter) { await Task.Delay(Delay); base.Execute(parameter); } } ///// ///// RelayCommand is a very easy-to-use implementation of ICommand. You can use a RelayCommand to expose viewmodel functionality as a command, and ///// supply the condition that determines the command's availability. A control in the view bound to a command can execute an available command will ///// update its enabled state in response to the availability of the command. ///// //public sealed class RelayCommand : ICommand //{ // #region fields // readonly Predicate canExecute; // readonly Action execute; // #endregion fields // #region constructors // public RelayCommand(Action execute) : this(execute, null) { } // public RelayCommand(Action execute, Predicate canExecute) // { // this.execute = execute; // this.canExecute = canExecute; // } // public RelayCommand(Action execute) // : this((x) => execute()) // { // } // public RelayCommand(Action execute, Func canExecute) : this((x) => execute(), (x) => canExecute()) // { // } // public RelayCommand(Action execute, Predicate canExecute) // : this((x) => execute(), canExecute) // { // } // #endregion constructors // #region methods // public void RaiseCanExecuteChanged() // { // if (this.CanExecuteChanged != null) // { // this.CanExecuteChanged(this, EventArgs.Empty); // } // } // #endregion methods // #region ICommand events // public event EventHandler CanExecuteChanged; // #endregion ICommand events // #region ICommand methods // public bool CanExecute(object parameter) // { // return this.canExecute != null ? this.canExecute(parameter) : true; // } // public void Execute(object parameter) // { // if (this.execute != null) // { // this.execute(parameter); // } // } // #endregion ICommand methods //} //public sealed class RelayCommand : ICommand //{ // #region Events // public event EventHandler Executed; // #endregion // #region fields // readonly Predicate canExecute; // readonly Action execute; // #endregion fields // #region constructors // public RelayCommand(Action execute) : this(execute, null) { } // public RelayCommand(Action execute, Predicate canExecute) // { // this.execute = execute; // this.canExecute = canExecute; // } // public RelayCommand(Action execute) // : this((x) => execute()) // { // } // #endregion constructors // #region methods // public void RaiseCanExecuteChanged() // { // if (this.CanExecuteChanged != null) // { // this.CanExecuteChanged(this, EventArgs.Empty); // } // } // #endregion methods // #region ICommand events // public event EventHandler CanExecuteChanged; // #endregion ICommand events // #region ICommand methods // public bool CanExecute(object parameter) // { // return this.canExecute != null ? this.canExecute(parameter) : true; // } // public void Execute(object parameter) // { // if (this.execute != null) // { // this.execute((T)parameter); // Executed?.Invoke(this, (T)parameter); // } // } // #endregion ICommand methods //} }