DelegateCommand.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Resources;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace HandyControl.Interactivity.Commands
  8. {
  9. /// <summary>
  10. /// An <see cref="ICommand"/> whose delegates do not take any parameters for <see cref="Execute()"/> and <see cref="CanExecute()"/>.
  11. /// </summary>
  12. /// <see cref="DelegateCommandBase"/>
  13. /// <see cref="DelegateCommand{T}"/>
  14. public class DelegateCommand : DelegateCommandBase
  15. {
  16. Action _executeMethod;
  17. Func<bool> _canExecuteMethod;
  18. /// <summary>
  19. /// Creates a new instance of <see cref="DelegateCommand"/> with the <see cref="Action"/> to invoke on execution.
  20. /// </summary>
  21. /// <param name="executeMethod">The <see cref="Action"/> to invoke when <see cref="ICommand.Execute(object)"/> is called.</param>
  22. public DelegateCommand(Action executeMethod)
  23. : this(executeMethod, () => true)
  24. {
  25. }
  26. /// <summary>
  27. /// Creates a new instance of <see cref="DelegateCommand"/> with the <see cref="Action"/> to invoke on execution
  28. /// and a <see langword="Func" /> to query for determining if the command can execute.
  29. /// </summary>
  30. /// <param name="executeMethod">The <see cref="Action"/> to invoke when <see cref="ICommand.Execute"/> is called.</param>
  31. /// <param name="canExecuteMethod">The <see cref="Func{TResult}"/> to invoke when <see cref="ICommand.CanExecute"/> is called</param>
  32. public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod)
  33. : base()
  34. {
  35. if (executeMethod == null || canExecuteMethod == null)
  36. throw new ArgumentNullException(nameof(executeMethod), "Neither the executeMethod nor the canExecuteMethod delegates can be null.");
  37. _executeMethod = executeMethod;
  38. _canExecuteMethod = canExecuteMethod;
  39. }
  40. ///<summary>
  41. /// Executes the command.
  42. ///</summary>
  43. public void Execute()
  44. {
  45. try
  46. {
  47. _executeMethod();
  48. }
  49. catch (Exception ex)
  50. {
  51. if (!ExceptionHandler.CanHandle(ex))
  52. throw;
  53. ExceptionHandler.Handle(ex, null);
  54. }
  55. }
  56. /// <summary>
  57. /// Determines if the command can be executed.
  58. /// </summary>
  59. /// <returns>Returns <see langword="true"/> if the command can execute,otherwise returns <see langword="false"/>.</returns>
  60. public bool CanExecute()
  61. {
  62. try
  63. {
  64. return _canExecuteMethod();
  65. }
  66. catch (Exception ex)
  67. {
  68. if (!ExceptionHandler.CanHandle(ex))
  69. throw;
  70. ExceptionHandler.Handle(ex, null);
  71. return false;
  72. }
  73. }
  74. /// <summary>
  75. /// Handle the internal invocation of <see cref="ICommand.Execute(object)"/>
  76. /// </summary>
  77. /// <param name="parameter">Command Parameter</param>
  78. protected override void Execute(object? parameter)
  79. {
  80. Execute();
  81. }
  82. /// <summary>
  83. /// Handle the internal invocation of <see cref="ICommand.CanExecute(object)"/>
  84. /// </summary>
  85. /// <param name="parameter"></param>
  86. /// <returns><see langword="true"/> if the Command Can Execute, otherwise <see langword="false" /></returns>
  87. protected override bool CanExecute(object? parameter)
  88. {
  89. return CanExecute();
  90. }
  91. /// <summary>
  92. /// Observes a property that implements INotifyPropertyChanged, and automatically calls DelegateCommandBase.RaiseCanExecuteChanged on property changed notifications.
  93. /// </summary>
  94. /// <typeparam name="T">The object type containing the property specified in the expression.</typeparam>
  95. /// <param name="propertyExpression">The property expression. Example: ObservesProperty(() => PropertyName).</param>
  96. /// <returns>The current instance of DelegateCommand</returns>
  97. public DelegateCommand ObservesProperty<T>(Expression<Func<T>> propertyExpression)
  98. {
  99. ObservesPropertyInternal(propertyExpression);
  100. return this;
  101. }
  102. /// <summary>
  103. /// 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.
  104. /// </summary>
  105. /// <param name="canExecuteExpression">The property expression. Example: ObservesCanExecute(() => PropertyName).</param>
  106. /// <returns>The current instance of DelegateCommand</returns>
  107. public DelegateCommand ObservesCanExecute(Expression<Func<bool>> canExecuteExpression)
  108. {
  109. _canExecuteMethod = canExecuteExpression.Compile();
  110. ObservesPropertyInternal(canExecuteExpression);
  111. return this;
  112. }
  113. /// <summary>
  114. /// Registers an callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
  115. /// </summary>
  116. /// <param name="catch">The Callback</param>
  117. /// <returns>The current instance of <see cref="DelegateCommand"/></returns>
  118. public DelegateCommand Catch(Action<Exception> @catch)
  119. {
  120. ExceptionHandler.Register<Exception>(@catch);
  121. return this;
  122. }
  123. /// <summary>
  124. /// Registers an callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
  125. /// </summary>
  126. /// <param name="catch">The Callback</param>
  127. /// <returns>The current instance of <see cref="DelegateCommand"/></returns>
  128. public DelegateCommand Catch(Action<Exception, object> @catch)
  129. {
  130. ExceptionHandler.Register<Exception>(@catch);
  131. return this;
  132. }
  133. /// <summary>
  134. /// Registers an callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
  135. /// </summary>
  136. /// <typeparam name="TException">The Exception Type</typeparam>
  137. /// <param name="catch">The Callback</param>
  138. /// <returns>The current instance of <see cref="DelegateCommand"/></returns>
  139. public DelegateCommand Catch<TException>(Action<TException> @catch)
  140. where TException : Exception
  141. {
  142. ExceptionHandler.Register<TException>(@catch);
  143. return this;
  144. }
  145. /// <summary>
  146. /// Registers an callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
  147. /// </summary>
  148. /// <typeparam name="TException">The Exception Type</typeparam>
  149. /// <param name="catch">The Callback</param>
  150. /// <returns>The current instance of <see cref="DelegateCommand"/></returns>
  151. public DelegateCommand Catch<TException>(Action<TException, object> @catch)
  152. where TException : Exception
  153. {
  154. ExceptionHandler.Register<TException>(@catch);
  155. return this;
  156. }
  157. /// <summary>
  158. /// Registers an async callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
  159. /// </summary>
  160. /// <param name="catch">The Callback</param>
  161. /// <returns>The current instance of <see cref="DelegateCommand"/></returns>
  162. public DelegateCommand Catch(Func<Exception, Task> @catch)
  163. {
  164. ExceptionHandler.Register<Exception>(@catch);
  165. return this;
  166. }
  167. /// <summary>
  168. /// Registers an async callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
  169. /// </summary>
  170. /// <param name="catch">The Callback</param>
  171. /// <returns>The current instance of <see cref="DelegateCommand"/></returns>
  172. public DelegateCommand Catch(Func<Exception, object, Task> @catch)
  173. {
  174. ExceptionHandler.Register<Exception>(@catch);
  175. return this;
  176. }
  177. /// <summary>
  178. /// Registers an async callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
  179. /// </summary>
  180. /// <typeparam name="TException">The Exception Type</typeparam>
  181. /// <param name="catch">The Callback</param>
  182. /// <returns>The current instance of <see cref="DelegateCommand"/></returns>
  183. public DelegateCommand Catch<TException>(Func<TException, Task> @catch)
  184. where TException : Exception
  185. {
  186. ExceptionHandler.Register<TException>(@catch);
  187. return this;
  188. }
  189. /// <summary>
  190. /// Registers an async callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
  191. /// </summary>
  192. /// <typeparam name="TException">The Exception Type</typeparam>
  193. /// <param name="catch">The Callback</param>
  194. /// <returns>The current instance of <see cref="DelegateCommand"/></returns>
  195. public DelegateCommand Catch<TException>(Func<TException, object, Task> @catch)
  196. where TException : Exception
  197. {
  198. ExceptionHandler.Register<TException>(@catch);
  199. return this;
  200. }
  201. }
  202. }