using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Resources; using System.Text; using System.Threading.Tasks; namespace HandyControl.Interactivity.Commands { /// /// An whose delegates do not take any parameters for and . /// /// /// public class DelegateCommand : DelegateCommandBase { Action _executeMethod; Func _canExecuteMethod; /// /// Creates a new instance of with the to invoke on execution. /// /// The to invoke when is called. public DelegateCommand(Action executeMethod) : this(executeMethod, () => true) { } /// /// Creates a new instance of with the to invoke on execution /// and a to query for determining if the command can execute. /// /// The to invoke when is called. /// The to invoke when is called public DelegateCommand(Action executeMethod, Func canExecuteMethod) : base() { if (executeMethod == null || canExecuteMethod == null) throw new ArgumentNullException(nameof(executeMethod), "Neither the executeMethod nor the canExecuteMethod delegates can be null."); _executeMethod = executeMethod; _canExecuteMethod = canExecuteMethod; } /// /// Executes the command. /// public void Execute() { try { _executeMethod(); } catch (Exception ex) { if (!ExceptionHandler.CanHandle(ex)) throw; ExceptionHandler.Handle(ex, null); } } /// /// Determines if the command can be executed. /// /// Returns if the command can execute,otherwise returns . public bool CanExecute() { try { return _canExecuteMethod(); } catch (Exception ex) { if (!ExceptionHandler.CanHandle(ex)) throw; ExceptionHandler.Handle(ex, null); return false; } } /// /// Handle the internal invocation of /// /// Command Parameter protected override void Execute(object? parameter) { Execute(); } /// /// Handle the internal invocation of /// /// /// if the Command Can Execute, otherwise protected override bool CanExecute(object? parameter) { return CanExecute(); } /// /// Observes a property that implements INotifyPropertyChanged, and automatically calls DelegateCommandBase.RaiseCanExecuteChanged on property changed notifications. /// /// The object type containing the property specified in the expression. /// The property expression. Example: ObservesProperty(() => PropertyName). /// The current instance of DelegateCommand public DelegateCommand ObservesProperty(Expression> propertyExpression) { ObservesPropertyInternal(propertyExpression); return this; } /// /// Observes a property that is used to determine if this command can execute, and if it implements INotifyPropertyChanged it will automatically call DelegateCommandBase.RaiseCanExecuteChanged on property changed notifications. /// /// The property expression. Example: ObservesCanExecute(() => PropertyName). /// The current instance of DelegateCommand public DelegateCommand ObservesCanExecute(Expression> canExecuteExpression) { _canExecuteMethod = canExecuteExpression.Compile(); ObservesPropertyInternal(canExecuteExpression); return this; } /// /// Registers an callback if an exception is encountered while executing the /// /// The Callback /// The current instance of public DelegateCommand Catch(Action @catch) { ExceptionHandler.Register(@catch); return this; } /// /// Registers an callback if an exception is encountered while executing the /// /// The Callback /// The current instance of public DelegateCommand Catch(Action @catch) { ExceptionHandler.Register(@catch); return this; } /// /// Registers an callback if an exception is encountered while executing the /// /// The Exception Type /// The Callback /// The current instance of public DelegateCommand Catch(Action @catch) where TException : Exception { ExceptionHandler.Register(@catch); return this; } /// /// Registers an callback if an exception is encountered while executing the /// /// The Exception Type /// The Callback /// The current instance of public DelegateCommand Catch(Action @catch) where TException : Exception { ExceptionHandler.Register(@catch); return this; } /// /// Registers an async callback if an exception is encountered while executing the /// /// The Callback /// The current instance of public DelegateCommand Catch(Func @catch) { ExceptionHandler.Register(@catch); return this; } /// /// Registers an async callback if an exception is encountered while executing the /// /// The Callback /// The current instance of public DelegateCommand Catch(Func @catch) { ExceptionHandler.Register(@catch); return this; } /// /// Registers an async callback if an exception is encountered while executing the /// /// The Exception Type /// The Callback /// The current instance of public DelegateCommand Catch(Func @catch) where TException : Exception { ExceptionHandler.Register(@catch); return this; } /// /// Registers an async callback if an exception is encountered while executing the /// /// The Exception Type /// The Callback /// The current instance of public DelegateCommand Catch(Func @catch) where TException : Exception { ExceptionHandler.Register(@catch); return this; } } }