using System; namespace ArdupilotMega.Presenter { /// /// A command that executes delegates to determine whether the command can execute, and to execute the command. /// /// /// /// This command implementation is useful when the command simply needs to execute a method on a view model. The delegate for /// determining whether the command can execute is optional. If it is not provided, the command is considered always eligible /// to execute. /// /// public class DelegateCommand : ICommand { private readonly Predicate _canExecute; private readonly Action _execute; public DelegateCommand(Action execute) { _execute = execute; } /// /// Constructs an instance of DelegateCommand. /// /// /// The delegate to invoke when the command is executed. /// /// /// The delegate to invoke to determine whether the command can execute. /// public DelegateCommand(Action execute, Predicate canExecute) { _execute = execute; _canExecute = canExecute; } /// /// Determines whether this command can execute. /// public bool CanExecute(object parameter) { if (_canExecute == null) { return true; } return _canExecute(parameter); } /// /// Executes this command. /// public void Execute(object parameter) { _execute(parameter); } } }