2 using System.Reflection;
3 using System.Windows.Input;
5 namespace Tizen.NUI.Binding
7 internal sealed class Command<T> : Command
9 public Command(Action<T> execute)
12 if (IsValidParameter(o))
20 throw new ArgumentNullException(nameof(execute));
24 public Command(Action<T> execute, Func<T, bool> canExecute)
27 if (IsValidParameter(o))
31 }, o => IsValidParameter(o) && canExecute((T)o))
34 throw new ArgumentNullException(nameof(execute));
35 if (canExecute == null)
36 throw new ArgumentNullException(nameof(canExecute));
39 static bool IsValidParameter(object o)
43 // The parameter isn't null, so we don't have to worry whether null is a valid option
49 // The parameter is null. Is T Nullable?
50 if (Nullable.GetUnderlyingType(t) != null)
55 // Not a Nullable, if it's a value type then null is not valid
56 return !t.GetTypeInfo().IsValueType;
61 /// Defines an ICommand implementation that wraps a Action.
63 public class Command : ICommand
65 readonly Func<object, bool> _canExecute;
66 readonly Action<object> _execute;
69 /// Initializes a new instance of the Command class.
71 /// <param name="execute">An instance to execute when the Command is executed.</param>
72 public Command(Action<object> execute)
75 throw new ArgumentNullException(nameof(execute));
81 /// Initializes a new instance of the Command class.
83 /// <param name="execute">An Action to execute when the Command is executed.</param>
84 public Command(Action execute) : this(o => execute())
87 throw new ArgumentNullException(nameof(execute));
91 /// Initializes a new instance of the Command class.
93 /// <param name="execute">An Action to execute when the Command is executed.</param>
94 /// <param name="canExecute">A instance indicating if the Command can be executed.</param>
95 public Command(Action<object> execute, Func<object, bool> canExecute) : this(execute)
97 if (canExecute == null)
98 throw new ArgumentNullException(nameof(canExecute));
100 _canExecute = canExecute;
104 /// Initializes a new instance of the Command class.
106 /// <param name="execute">An Action to execute when the Command is executed.</param>
107 /// <param name="canExecute">A instance indicating if the Command can be executed.</param>
108 public Command(Action execute, Func<bool> canExecute) : this(o => execute(), o => canExecute())
111 throw new ArgumentNullException(nameof(execute));
112 if (canExecute == null)
113 throw new ArgumentNullException(nameof(canExecute));
117 /// Returns a Boolean indicating if the Command can be exectued with the given parameter.
119 /// <param name="parameter">An Object used as parameter to determine if the Command can be executed.</param>
120 /// <returns>true if the Command can be executed, false otherwise.</returns>
121 public bool CanExecute(object parameter)
123 if (_canExecute != null)
124 return _canExecute(parameter);
130 /// Occurs when the target of the Command should reevaluate whether or not the Command can be executed.
132 public event EventHandler CanExecuteChanged;
135 /// Invokes the execute Action.
137 /// <param name="parameter">An Object used as parameter for the execute Action.</param>
138 public void Execute(object parameter)
144 /// Send a CanExecuteChanged.
146 public void ChangeCanExecute()
148 EventHandler changed = CanExecuteChanged;
149 changed?.Invoke(this, EventArgs.Empty);