using System; using System.Windows.Input; namespace vCloud.GeneratePackages.Tool.ViewModels { public class Command : ViewModel, ICommand { private readonly Action _executeAction; private readonly Func _canExecuteFunc; public event EventHandler CanExecuteChanged; public Command(string name, Action executeAction, Func canExecuteFunc = null) { Description = name; if (executeAction == null) { throw new NullReferenceException("Execute Action can not be null"); } _executeAction = executeAction; _canExecuteFunc = canExecuteFunc; } /// /// If this command can executed /// /// /// public bool CanExecute(object parameter) { if (!IsEnabled) { return false; } if (_canExecuteFunc != null) { return _canExecuteFunc(parameter); } return true; } /// /// Execute the command /// /// public virtual void Execute(object parameter) { if (CanExecute(parameter)) { //Logger.WriteLineInfo($"Execute Command with description {Description}"); _executeAction(parameter); } } public virtual void OnCanExecuteChanged() { CanExecuteChanged?.Invoke(this, EventArgs.Empty); } protected override void OnIsEnabledChanged() { OnCanExecuteChanged(); base.OnIsEnabledChanged(); } internal void UpdateDescription() { OnPropertyChanged(() => Description); } } }