i前缀 wpf_WPF ICommand 用法

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Windows.Input;6

7 namespaceWpfExample8 {9 public classRelayCommand : ICommand10 {11 #region Fields

12

13 ///

14 ///Encapsulated the execute action15 ///

16 private Actionexecute;17

18 ///

19 ///Encapsulated the representation for the validation of the execute method20 ///

21 private PredicatecanExecute;22

23 #endregion //Fields

24

25 #region Constructors

26

27 ///

28 ///Initializes a new instance of the RelayCommand class29 ///Creates a new command that can always execute.30 ///

31 /// The execution logic.

32 public RelayCommand(Actionexecute)33 : this(execute, DefaultCanExecute)34 {35 }36

37 ///

38 ///Initializes a new instance of the RelayCommand class39 ///Creates a new command.40 ///

41 /// The execution logic.

42 /// The execution status logic.

43 public RelayCommand(Action execute, PredicatecanExecute)44 {45 if (execute == null)46 {47 throw new ArgumentNullException("execute");48 }49

50 if (canExecute == null)51 {52 throw new ArgumentNullException("canExecute");53 }54

55 this.execute =execute;56 this.canExecute =canExecute;57 }58

59 #endregion //Constructors

60

61 #region ICommand Members

62

63 ///

64 ///An event to raise when the CanExecute value is changed65 ///

66 ///

67 ///Any subscription to this event will automatically subscribe to both68 ///the local OnCanExecuteChanged method AND69 ///the CommandManager RequerySuggested event70 ///

71 public eventEventHandler CanExecuteChanged72 {73 add74 {75 CommandManager.RequerySuggested +=value;76 this.CanExecuteChangedInternal +=value;77 }78

79 remove80 {81 CommandManager.RequerySuggested -=value;82 this.CanExecuteChangedInternal -=value;83 }84 }85

86 ///

87 ///An event to allow the CanExecuteChanged event to be raised manually88 ///

89 private eventEventHandler CanExecuteChangedInternal;90

91 ///

92 ///Defines if command can be executed93 ///

94 /// the parameter that represents the validation method

95 /// true if the command can be executed

96 public bool CanExecute(objectparameter)97 {98 return this.canExecute != null && this.canExecute(parameter);99 }100

101 ///

102 ///Execute the encapsulated command103 ///

104 /// the parameter that represents the execution method

105 public void Execute(objectparameter)106 {107 this.execute(parameter);108 }109

110 #endregion //ICommand Members

111

112 ///

113 ///Raises the can execute changed.114 ///

115 public voidOnCanExecuteChanged()116 {117 EventHandler handler = this.CanExecuteChangedInternal;118 if (handler != null)119 {120 //DispatcherHelper.BeginInvokeOnUIThread(() => handler.Invoke(this, EventArgs.Empty));

121 handler.Invoke(this, EventArgs.Empty);122 }123 }124

125 ///

126 ///Destroys this instance.127 ///

128 public voidDestroy()129 {130 this.canExecute = _ => false;131 this.execute = _ => { return; };132 }133

134 ///

135 ///Defines if command can be executed (default behaviour)136 ///

137 /// The parameter.

138 /// Always true

139 private static bool DefaultCanExecute(objectparameter)140 {141 return true;142 }143 }144 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值