-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathDelegateCommand{T}.cs
287 lines (261 loc) · 12.3 KB
/
DelegateCommand{T}.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Input;
using Prism.Properties;
#nullable enable
namespace Prism.Commands
{
/// <summary>
/// An <see cref="ICommand"/> whose delegates can be attached for <see cref="Execute(T)"/> and <see cref="CanExecute(T)"/>.
/// </summary>
/// <typeparam name="T">Parameter type.</typeparam>
/// <remarks>
/// The constructor deliberately prevents the use of value types.
/// Because ICommand takes an object, having a value type for T would cause unexpected behavior when CanExecute(null) is called during XAML initialization for command bindings.
/// Using default(T) was considered and rejected as a solution because the implementor would not be able to distinguish between a valid and defaulted values.
/// <para/>
/// Instead, callers should support a value type by using a nullable value type and checking the HasValue property before using the Value property.
/// <example>
/// <code>
/// public MyClass()
/// {
/// this.submitCommand = new DelegateCommand<int?>(this.Submit, this.CanSubmit);
/// }
///
/// private bool CanSubmit(int? customerId)
/// {
/// return (customerId.HasValue && customers.Contains(customerId.Value));
/// }
/// </code>
/// </example>
/// </remarks>
public class DelegateCommand<T> : DelegateCommandBase
{
readonly Action<T> _executeMethod;
Func<T, bool> _canExecuteMethod;
/// <summary>
/// Initializes a new instance of <see cref="DelegateCommand{T}"/>.
/// </summary>
/// <param name="executeMethod">Delegate to execute when Execute is called on the command. This can be null to just hook up a CanExecute delegate.</param>
/// <remarks><see cref="CanExecute(T)"/> will always return true.</remarks>
public DelegateCommand(Action<T> executeMethod)
: this(executeMethod, (o) => true)
{
}
/// <summary>
/// Initializes a new instance of <see cref="DelegateCommand{T}"/>.
/// </summary>
/// <param name="executeMethod">Delegate to execute when Execute is called on the command. This can be null to just hook up a CanExecute delegate.</param>
/// <param name="canExecuteMethod">Delegate to execute when CanExecute is called on the command. This can be null.</param>
/// <exception cref="ArgumentNullException">When both <paramref name="executeMethod"/> and <paramref name="canExecuteMethod"/> are <see langword="null" />.</exception>
public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
: base()
{
if (executeMethod == null || canExecuteMethod == null)
throw new ArgumentNullException(nameof(executeMethod), Resources.DelegateCommandDelegatesCannotBeNull);
TypeInfo genericTypeInfo = typeof(T).GetTypeInfo();
// DelegateCommand allows object or Nullable<>.
// note: Nullable<> is a struct so we cannot use a class constraint.
if (genericTypeInfo.IsValueType)
{
if ((!genericTypeInfo.IsGenericType) || (!typeof(Nullable<>).GetTypeInfo().IsAssignableFrom(genericTypeInfo.GetGenericTypeDefinition().GetTypeInfo())))
{
throw new InvalidCastException(Resources.DelegateCommandInvalidGenericPayloadType);
}
}
_executeMethod = executeMethod;
_canExecuteMethod = canExecuteMethod;
}
///<summary>
///Executes the command and invokes the <see cref="Action{T}"/> provided during construction.
///</summary>
///<param name="parameter">Data used by the command.</param>
public void Execute(T parameter)
{
try
{
_executeMethod(parameter);
}
catch (Exception ex)
{
if (!ExceptionHandler.CanHandle(ex))
throw;
ExceptionHandler.Handle(ex, parameter);
}
}
///<summary>
///Determines if the command can execute by invoked the <see cref="Func{T,Bool}"/> provided during construction.
///</summary>
///<param name="parameter">Data used by the command to determine if it can execute.</param>
///<returns>
///<see langword="true" /> if this command can be executed; otherwise, <see langword="false" />.
///</returns>
public bool CanExecute(T parameter)
{
try
{
return _canExecuteMethod(parameter);
}
catch (Exception ex)
{
if (!ExceptionHandler.CanHandle(ex))
throw;
ExceptionHandler.Handle(ex, parameter);
return false;
}
}
/// <summary>
/// Handle the internal invocation of <see cref="ICommand.Execute(object)"/>
/// </summary>
/// <param name="parameter">Command Parameter</param>
protected override void Execute(object? parameter)
{
try
{
// Note: We don't call Execute because we would potentially invoke the Try/Catch twice.
// It is also needed here incase (T)parameter throws the exception
_executeMethod((T)parameter!);
}
catch (Exception ex)
{
if (!ExceptionHandler.CanHandle(ex))
throw;
ExceptionHandler.Handle(ex, parameter);
}
}
/// <summary>
/// Handle the internal invocation of <see cref="ICommand.CanExecute(object)"/>
/// </summary>
/// <param name="parameter"></param>
/// <returns><see langword="true"/> if the Command Can Execute, otherwise <see langword="false" /></returns>
protected override bool CanExecute(object? parameter)
{
try
{
// Note: We don't call Execute because we would potentially invoke the Try/Catch twice.
// It is also needed here incase (T)parameter throws the exception
return CanExecute((T)parameter!);
}
catch (Exception ex)
{
if (!ExceptionHandler.CanHandle(ex))
throw;
ExceptionHandler.Handle(ex, parameter);
return false;
}
}
/// <summary>
/// Observes a property that implements INotifyPropertyChanged, and automatically calls DelegateCommandBase.RaiseCanExecuteChanged on property changed notifications.
/// </summary>
/// <typeparam name="TType">The type of the return value of the method that this delegate encapsulates</typeparam>
/// <param name="propertyExpression">The property expression. Example: ObservesProperty(() => PropertyName).</param>
/// <returns>The current instance of DelegateCommand</returns>
public DelegateCommand<T> ObservesProperty<TType>(Expression<Func<TType>> propertyExpression)
{
ObservesPropertyInternal(propertyExpression);
return this;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="canExecuteExpression">The property expression. Example: ObservesCanExecute(() => PropertyName).</param>
/// <returns>The current instance of DelegateCommand</returns>
public DelegateCommand<T> ObservesCanExecute(Expression<Func<bool>> canExecuteExpression)
{
Expression<Func<T, bool>> expression = Expression.Lambda<Func<T, bool>>(canExecuteExpression.Body, Expression.Parameter(typeof(T), "o"));
_canExecuteMethod = expression.Compile();
ObservesPropertyInternal(canExecuteExpression);
return this;
}
/// <summary>
/// Registers an callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
/// </summary>
/// <param name="catch">The Callback</param>
/// <returns>The current instance of <see cref="DelegateCommand"/></returns>
public DelegateCommand<T> Catch(Action<Exception> @catch)
{
ExceptionHandler.Register<Exception>(@catch);
return this;
}
/// <summary>
/// Registers an callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
/// </summary>
/// <param name="catch">The Callback</param>
/// <returns>The current instance of <see cref="DelegateCommand"/></returns>
public DelegateCommand<T> Catch(Action<Exception, object> @catch)
{
ExceptionHandler.Register<Exception>(@catch);
return this;
}
/// <summary>
/// Registers an callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
/// </summary>
/// <typeparam name="TException">The Exception Type</typeparam>
/// <param name="catch">The Callback</param>
/// <returns>The current instance of <see cref="DelegateCommand"/></returns>
public DelegateCommand<T> Catch<TException>(Action<TException> @catch)
where TException : Exception
{
ExceptionHandler.Register<TException>(@catch);
return this;
}
/// <summary>
/// Registers an callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
/// </summary>
/// <typeparam name="TException">The Exception Type</typeparam>
/// <param name="catch">The Callback</param>
/// <returns>The current instance of <see cref="DelegateCommand"/></returns>
public DelegateCommand<T> Catch<TException>(Action<TException, object> @catch)
where TException : Exception
{
ExceptionHandler.Register<TException>(@catch);
return this;
}
/// <summary>
/// Registers an async callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
/// </summary>
/// <param name="catch">The Callback</param>
/// <returns>The current instance of <see cref="DelegateCommand"/></returns>
public DelegateCommand<T> Catch(Func<Exception, Task> @catch)
{
ExceptionHandler.Register<Exception>(@catch);
return this;
}
/// <summary>
/// Registers an async callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
/// </summary>
/// <param name="catch">The Callback</param>
/// <returns>The current instance of <see cref="DelegateCommand"/></returns>
public DelegateCommand<T> Catch(Func<Exception, object, Task> @catch)
{
ExceptionHandler.Register<Exception>(@catch);
return this;
}
/// <summary>
/// Registers an async callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
/// </summary>
/// <typeparam name="TException">The Exception Type</typeparam>
/// <param name="catch">The Callback</param>
/// <returns>The current instance of <see cref="DelegateCommand"/></returns>
public DelegateCommand<T> Catch<TException>(Func<TException, Task> @catch)
where TException : Exception
{
ExceptionHandler.Register<TException>(@catch);
return this;
}
/// <summary>
/// Registers an async callback if an exception is encountered while executing the <see cref="DelegateCommand"/>
/// </summary>
/// <typeparam name="TException">The Exception Type</typeparam>
/// <param name="catch">The Callback</param>
/// <returns>The current instance of <see cref="DelegateCommand"/></returns>
public DelegateCommand<T> Catch<TException>(Func<TException, object, Task> @catch)
where TException : Exception
{
ExceptionHandler.Register<TException>(@catch);
return this;
}
}
}