1 | // --------------------------------------------------------------------------------------------------------------------
|
---|
2 | // <copyright file="RelayCommand.cs" company="Microsoft">
|
---|
3 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
---|
4 | // </copyright>
|
---|
5 | // --------------------------------------------------------------------------------------------------------------------
|
---|
6 |
|
---|
7 | namespace Microsoft.Kinect.Toolkit
|
---|
8 | {
|
---|
9 | using System;
|
---|
10 | using System.Globalization;
|
---|
11 | using System.Windows.Input;
|
---|
12 | using Microsoft.Kinect.Toolkit.Properties;
|
---|
13 |
|
---|
14 | /// <summary>
|
---|
15 | /// Command that executes a delegate that takes no parameters.
|
---|
16 | /// </summary>
|
---|
17 | public class RelayCommand : ICommand
|
---|
18 | {
|
---|
19 | /// <summary>
|
---|
20 | /// Delegate to be executed
|
---|
21 | /// </summary>
|
---|
22 | private Action executeDelegate;
|
---|
23 |
|
---|
24 | /// <summary>
|
---|
25 | /// Predicate determining whether this command can currently execute
|
---|
26 | /// </summary>
|
---|
27 | private Func<bool> canExecuteDelegate;
|
---|
28 |
|
---|
29 | private EventHandler canExecuteEventhandler;
|
---|
30 |
|
---|
31 | /// <summary>
|
---|
32 | /// Initializes a new instance of the RelayCommand class with the provided delegate and predicate
|
---|
33 | /// </summary>
|
---|
34 | /// <param name="executeDelegate">Delegate to be executed</param>
|
---|
35 | /// <param name="canExecuteDelegate">Predicate determining whether this command can currently execute</param>
|
---|
36 | public RelayCommand(Action executeDelegate, Func<bool> canExecuteDelegate)
|
---|
37 | {
|
---|
38 | if (null == executeDelegate)
|
---|
39 | {
|
---|
40 | throw new ArgumentNullException("executeDelegate");
|
---|
41 | }
|
---|
42 |
|
---|
43 | this.canExecuteDelegate = canExecuteDelegate;
|
---|
44 | this.executeDelegate = executeDelegate;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /// <summary>
|
---|
48 | /// Initializes a new instance of the RelayCommand class with the provided delegate
|
---|
49 | /// </summary>
|
---|
50 | /// <param name="executeDelegate">Delegate to be executed</param>
|
---|
51 | public RelayCommand(Action executeDelegate)
|
---|
52 | : this(executeDelegate, null)
|
---|
53 | {
|
---|
54 | }
|
---|
55 |
|
---|
56 | /// <summary>
|
---|
57 | /// Event signaling that the possibility of this command executing has changed
|
---|
58 | /// </summary>
|
---|
59 | public event EventHandler CanExecuteChanged
|
---|
60 | {
|
---|
61 | add
|
---|
62 | {
|
---|
63 | this.canExecuteEventhandler += value;
|
---|
64 | CommandManager.RequerySuggested += value;
|
---|
65 | }
|
---|
66 |
|
---|
67 | remove
|
---|
68 | {
|
---|
69 | this.canExecuteEventhandler -= value;
|
---|
70 | CommandManager.RequerySuggested -= value;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | /// <summary>
|
---|
75 | /// Evaluates whether the command can currently execute
|
---|
76 | /// </summary>
|
---|
77 | /// <param name="parameter">ICommand required parameter that is ignored</param>
|
---|
78 | /// <returns>True if the command can currently execute, false otherwise</returns>
|
---|
79 | public bool CanExecute(object parameter)
|
---|
80 | {
|
---|
81 | if (null == this.canExecuteDelegate)
|
---|
82 | {
|
---|
83 | return true;
|
---|
84 | }
|
---|
85 |
|
---|
86 | return this.canExecuteDelegate.Invoke();
|
---|
87 | }
|
---|
88 |
|
---|
89 | /// <summary>
|
---|
90 | /// Executes the associated delegate
|
---|
91 | /// </summary>
|
---|
92 | /// <param name="parameter">ICommand required parameter that is ignored</param>
|
---|
93 | public void Execute(object parameter)
|
---|
94 | {
|
---|
95 | this.executeDelegate.Invoke();
|
---|
96 | }
|
---|
97 |
|
---|
98 | /// <summary>
|
---|
99 | /// Raises the CanExecuteChanged event to signal that the possibility of execution has changed
|
---|
100 | /// </summary>
|
---|
101 | public void InvokeCanExecuteChanged()
|
---|
102 | {
|
---|
103 | if (null != canExecuteDelegate)
|
---|
104 | {
|
---|
105 | EventHandler handler = this.canExecuteEventhandler;
|
---|
106 | if (null != handler)
|
---|
107 | {
|
---|
108 | handler(this, EventArgs.Empty);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public class RelayCommand<T> : ICommand where T : class
|
---|
115 | {
|
---|
116 | /// <summary>
|
---|
117 | /// Delegate to be executed
|
---|
118 | /// </summary>
|
---|
119 | private Action<T> executeDelegate;
|
---|
120 |
|
---|
121 | /// <summary>
|
---|
122 | /// Predicate determining whether this command can currently execute
|
---|
123 | /// </summary>
|
---|
124 | private Predicate<T> canExecuteDelegate;
|
---|
125 |
|
---|
126 | private EventHandler canExecuteEventhandler;
|
---|
127 |
|
---|
128 | /// <summary>
|
---|
129 | /// Initializes a new instance of the RelayCommand class with the provided delegate and predicate
|
---|
130 | /// </summary>
|
---|
131 | /// <param name="executeDelegate">Delegate to be executed</param>
|
---|
132 | /// <param name="canExecuteDelegate">Predicate determining whether this command can currently execute</param>
|
---|
133 | public RelayCommand(Action<T> executeDelegate, Predicate<T> canExecuteDelegate)
|
---|
134 | {
|
---|
135 | if (null == executeDelegate)
|
---|
136 | {
|
---|
137 | throw new ArgumentNullException("executeDelegate");
|
---|
138 | }
|
---|
139 |
|
---|
140 | this.canExecuteDelegate = canExecuteDelegate;
|
---|
141 | this.executeDelegate = executeDelegate;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /// <summary>
|
---|
145 | /// Initializes a new instance of the RelayCommand class with the provided delegate
|
---|
146 | /// </summary>
|
---|
147 | /// <param name="executeDelegate">Delegate to be executed</param>
|
---|
148 | public RelayCommand(Action<T> executeDelegate)
|
---|
149 | : this(executeDelegate, null)
|
---|
150 | {
|
---|
151 | }
|
---|
152 |
|
---|
153 | /// <summary>
|
---|
154 | /// Event signaling that the possibility of this command executing has changed
|
---|
155 | /// </summary>
|
---|
156 | public event EventHandler CanExecuteChanged
|
---|
157 | {
|
---|
158 | add
|
---|
159 | {
|
---|
160 | this.canExecuteEventhandler += value;
|
---|
161 | CommandManager.RequerySuggested += value;
|
---|
162 | }
|
---|
163 |
|
---|
164 | remove
|
---|
165 | {
|
---|
166 | this.canExecuteEventhandler -= value;
|
---|
167 | CommandManager.RequerySuggested -= value;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | /// <summary>
|
---|
172 | /// Evaluates whether the command can currently execute
|
---|
173 | /// </summary>
|
---|
174 | /// <param name="parameter">Context of type T used for evaluating the current possibility of execution</param>
|
---|
175 | /// <returns>True if the command can currently execute, false otherwise</returns>
|
---|
176 | public bool CanExecute(object parameter)
|
---|
177 | {
|
---|
178 | if (null == parameter)
|
---|
179 | {
|
---|
180 | throw new ArgumentNullException("parameter");
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (null == this.canExecuteDelegate)
|
---|
184 | {
|
---|
185 | return true;
|
---|
186 | }
|
---|
187 |
|
---|
188 | T castParameter = parameter as T;
|
---|
189 | if (null == castParameter)
|
---|
190 | {
|
---|
191 | throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture, Resources.DelegateCommandCastException, parameter.GetType().FullName, typeof(T).FullName));
|
---|
192 | }
|
---|
193 |
|
---|
194 | return this.canExecuteDelegate.Invoke(castParameter);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /// <summary>
|
---|
198 | /// Executes the associated delegate
|
---|
199 | /// </summary>
|
---|
200 | /// <param name="parameter">Parameter of type T passed to the associated delegate</param>
|
---|
201 | public void Execute(object parameter)
|
---|
202 | {
|
---|
203 | if (null == parameter)
|
---|
204 | {
|
---|
205 | throw new ArgumentNullException("parameter");
|
---|
206 | }
|
---|
207 |
|
---|
208 | T castParameter = parameter as T;
|
---|
209 | if (null == castParameter)
|
---|
210 | {
|
---|
211 | throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture, Resources.DelegateCommandCastException, parameter.GetType().FullName, typeof(T).FullName));
|
---|
212 | }
|
---|
213 |
|
---|
214 | this.executeDelegate.Invoke(castParameter);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /// <summary>
|
---|
218 | /// Raises the CanExecuteChanged event to signal that the possibility of execution has changed
|
---|
219 | /// </summary>
|
---|
220 | public void InvokeCanExecuteChanged()
|
---|
221 | {
|
---|
222 | if (null != canExecuteDelegate)
|
---|
223 | {
|
---|
224 | EventHandler handler = this.canExecuteEventhandler;
|
---|
225 | if (null != handler)
|
---|
226 | {
|
---|
227 | handler(this, EventArgs.Empty);
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | } |
---|