BaseCommand.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Input;
  7. namespace OpenSEMI.ClientBase.Command
  8. {
  9. public class BaseCommand<T> : ICommand
  10. {
  11. #region Fields
  12. readonly Action<T> _execute = null;
  13. readonly Predicate<T> _canExecute = null;
  14. #endregion // Fields
  15. #region Constructors
  16. public BaseCommand(Action<T> execute)
  17. : this(execute, null)
  18. {
  19. }
  20. /// <summary>
  21. /// Creates a new command.
  22. /// </summary>
  23. /// <param name="execute">The execution logic.</param>
  24. /// <param name="canExecute">The execution status logic.</param>
  25. public BaseCommand(Action<T> execute, Predicate<T> canExecute)
  26. {
  27. if (execute == null)
  28. throw new ArgumentNullException("execute");
  29. _execute = execute;
  30. _canExecute = canExecute;
  31. }
  32. #endregion // Constructors
  33. #region ICommand Members
  34. public bool CanExecute(object parameter)
  35. {
  36. return _canExecute == null ? true : _canExecute((T)parameter);
  37. }
  38. public event EventHandler CanExecuteChanged
  39. {
  40. add
  41. {
  42. if (_canExecute != null)
  43. CommandManager.RequerySuggested += value;
  44. }
  45. remove
  46. {
  47. if (_canExecute != null)
  48. CommandManager.RequerySuggested -= value;
  49. }
  50. }
  51. public void Execute(object parameter)
  52. {
  53. _execute((T)parameter);
  54. }
  55. #endregion // ICommand Members
  56. }
  57. public class BaseCommand : ICommand
  58. {
  59. #region Fields
  60. readonly Action _execute;
  61. readonly Func<bool> _canExecute;
  62. #endregion // Fields
  63. #region Constructors
  64. /// <summary>
  65. /// Creates a new command that can always execute.
  66. /// </summary>
  67. /// <param name="execute">The execution logic.</param>
  68. public BaseCommand(Action execute)
  69. : this(execute, null)
  70. {
  71. }
  72. /// <summary>
  73. /// Creates a new command.
  74. /// </summary>
  75. /// <param name="execute">The execution logic.</param>
  76. /// <param name="canExecute">The execution status logic.</param>
  77. public BaseCommand(Action execute, Func<bool> canExecute)
  78. {
  79. if (execute == null)
  80. throw new ArgumentNullException("execute");
  81. _execute = execute;
  82. _canExecute = canExecute;
  83. }
  84. #endregion // Constructors
  85. #region ICommand Members
  86. public bool CanExecute(object parameter)
  87. {
  88. return _canExecute == null ? true : _canExecute();
  89. }
  90. public event EventHandler CanExecuteChanged
  91. {
  92. add
  93. {
  94. if (_canExecute != null)
  95. CommandManager.RequerySuggested += value;
  96. }
  97. remove
  98. {
  99. if (_canExecute != null)
  100. CommandManager.RequerySuggested -= value;
  101. }
  102. }
  103. public void Execute(object parameter)
  104. {
  105. _execute();
  106. }
  107. #endregion
  108. }
  109. }