// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // -------------------------------------------------------------------------------------------------------------------- namespace Microsoft.Kinect.Toolkit { using System; using System.Globalization; using System.Windows.Input; using Microsoft.Kinect.Toolkit.Properties; /// /// Command that executes a delegate that takes no parameters. /// public class RelayCommand : ICommand { /// /// Delegate to be executed /// private Action executeDelegate; /// /// Predicate determining whether this command can currently execute /// private Func canExecuteDelegate; private EventHandler canExecuteEventhandler; /// /// Initializes a new instance of the RelayCommand class with the provided delegate and predicate /// /// Delegate to be executed /// Predicate determining whether this command can currently execute public RelayCommand(Action executeDelegate, Func canExecuteDelegate) { if (null == executeDelegate) { throw new ArgumentNullException("executeDelegate"); } this.canExecuteDelegate = canExecuteDelegate; this.executeDelegate = executeDelegate; } /// /// Initializes a new instance of the RelayCommand class with the provided delegate /// /// Delegate to be executed public RelayCommand(Action executeDelegate) : this(executeDelegate, null) { } /// /// Event signaling that the possibility of this command executing has changed /// public event EventHandler CanExecuteChanged { add { this.canExecuteEventhandler += value; CommandManager.RequerySuggested += value; } remove { this.canExecuteEventhandler -= value; CommandManager.RequerySuggested -= value; } } /// /// Evaluates whether the command can currently execute /// /// ICommand required parameter that is ignored /// True if the command can currently execute, false otherwise public bool CanExecute(object parameter) { if (null == this.canExecuteDelegate) { return true; } return this.canExecuteDelegate.Invoke(); } /// /// Executes the associated delegate /// /// ICommand required parameter that is ignored public void Execute(object parameter) { this.executeDelegate.Invoke(); } /// /// Raises the CanExecuteChanged event to signal that the possibility of execution has changed /// public void InvokeCanExecuteChanged() { if (null != canExecuteDelegate) { EventHandler handler = this.canExecuteEventhandler; if (null != handler) { handler(this, EventArgs.Empty); } } } } public class RelayCommand : ICommand where T : class { /// /// Delegate to be executed /// private Action executeDelegate; /// /// Predicate determining whether this command can currently execute /// private Predicate canExecuteDelegate; private EventHandler canExecuteEventhandler; /// /// Initializes a new instance of the RelayCommand class with the provided delegate and predicate /// /// Delegate to be executed /// Predicate determining whether this command can currently execute public RelayCommand(Action executeDelegate, Predicate canExecuteDelegate) { if (null == executeDelegate) { throw new ArgumentNullException("executeDelegate"); } this.canExecuteDelegate = canExecuteDelegate; this.executeDelegate = executeDelegate; } /// /// Initializes a new instance of the RelayCommand class with the provided delegate /// /// Delegate to be executed public RelayCommand(Action executeDelegate) : this(executeDelegate, null) { } /// /// Event signaling that the possibility of this command executing has changed /// public event EventHandler CanExecuteChanged { add { this.canExecuteEventhandler += value; CommandManager.RequerySuggested += value; } remove { this.canExecuteEventhandler -= value; CommandManager.RequerySuggested -= value; } } /// /// Evaluates whether the command can currently execute /// /// Context of type T used for evaluating the current possibility of execution /// True if the command can currently execute, false otherwise public bool CanExecute(object parameter) { if (null == parameter) { throw new ArgumentNullException("parameter"); } if (null == this.canExecuteDelegate) { return true; } T castParameter = parameter as T; if (null == castParameter) { throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture, Resources.DelegateCommandCastException, parameter.GetType().FullName, typeof(T).FullName)); } return this.canExecuteDelegate.Invoke(castParameter); } /// /// Executes the associated delegate /// /// Parameter of type T passed to the associated delegate public void Execute(object parameter) { if (null == parameter) { throw new ArgumentNullException("parameter"); } T castParameter = parameter as T; if (null == castParameter) { throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture, Resources.DelegateCommandCastException, parameter.GetType().FullName, typeof(T).FullName)); } this.executeDelegate.Invoke(castParameter); } /// /// Raises the CanExecuteChanged event to signal that the possibility of execution has changed /// public void InvokeCanExecuteChanged() { if (null != canExecuteDelegate) { EventHandler handler = this.canExecuteEventhandler; if (null != handler) { handler(this, EventArgs.Empty); } } } } }