CommandBase.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 athosRT.tool
  8. {
  9. public class CommandBase : ICommand
  10. {
  11. private readonly Action<object> _commandpara;
  12. private readonly Action _command;
  13. private readonly Func<bool> _canExecute;
  14. public CommandBase(Action command, Func<bool> canExecute = null)
  15. {
  16. if (command == null)
  17. {
  18. throw new ArgumentNullException();
  19. }
  20. _canExecute = canExecute;
  21. _command = command;
  22. }
  23. public CommandBase(Action<object> commandpara, Func<bool> canExecute = null)
  24. {
  25. if (commandpara == null)
  26. {
  27. throw new ArgumentNullException();
  28. }
  29. _canExecute = canExecute;
  30. _commandpara = commandpara;
  31. }
  32. public bool CanExecute(object parameter)
  33. {
  34. return _canExecute == null || _canExecute();
  35. }
  36. public event EventHandler CanExecuteChanged
  37. {
  38. add { CommandManager.RequerySuggested += value; }
  39. remove { CommandManager.RequerySuggested -= value; }
  40. }
  41. public void Execute(object parameter)
  42. {
  43. if (parameter != null)
  44. {
  45. _commandpara(parameter);
  46. }
  47. else
  48. {
  49. if (_command != null)
  50. {
  51. _command();
  52. }
  53. else if (_commandpara != null)
  54. {
  55. _commandpara(null);
  56. }
  57. }
  58. }
  59. }
  60. public class DelegateCommand : ICommand
  61. {
  62. // 定义一个名为SimpleEventHandler的委托,两个参数,一个object类,一个是自定义的DelegateCommandEventArgs类
  63. public delegate void SimpleEventHandler(object sender, DelegateCommandEventArgs e);
  64. // handler是方法,别忘了,委托是用于定义方法的类
  65. private SimpleEventHandler handler;
  66. private bool isEnabled = true;
  67. public DelegateCommand(SimpleEventHandler handler)
  68. {
  69. this.handler = handler;
  70. }
  71. public void Execute(object parameter)
  72. {
  73. this.handler(this, new DelegateCommandEventArgs(parameter));
  74. }
  75. public bool CanExecute(object parameter)
  76. {
  77. return this.isEnabled;
  78. }
  79. public event EventHandler CanExecuteChanged;
  80. public bool IsEnabled
  81. {
  82. get { return this.isEnabled; }
  83. set
  84. {
  85. this.isEnabled = value;
  86. this.OnCanExecuteChanged();
  87. }
  88. }
  89. private void OnCanExecuteChanged()
  90. {
  91. if (this.CanExecuteChanged != null)
  92. this.CanExecuteChanged(this, EventArgs.Empty);
  93. }
  94. }
  95. public class DelegateCommandEventArgs : EventArgs
  96. {
  97. private object parameter;
  98. public DelegateCommandEventArgs(object parameter)
  99. {
  100. this.parameter = parameter;
  101. }
  102. public object Parameter
  103. {
  104. get { return this.parameter; }
  105. }
  106. }
  107. }