-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathDelegateCommand.cs
More file actions
166 lines (157 loc) · 5.84 KB
/
DelegateCommand.cs
File metadata and controls
166 lines (157 loc) · 5.84 KB
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
using System.Windows.Input;
namespace BytecodeApi.Wpf;
/// <summary>
/// Represents a command which uses specified delegates for <see cref="ICommand.Execute(object)" /> and <see cref="ICommand.CanExecute(object)" />.
/// </summary>
public sealed class DelegateCommand : ICommand
{
private readonly Action ExecuteDelegate;
private readonly Func<bool>? CanExecuteDelegate;
event EventHandler? ICommand.CanExecuteChanged
{
add
{
if (CanExecuteDelegate != null)
{
CommandManager.RequerySuggested += value;
}
}
remove
{
if (CanExecuteDelegate != null)
{
CommandManager.RequerySuggested -= value;
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="DelegateCommand" /> class with the specified execute delegate.
/// </summary>
/// <param name="execute">The <see cref="Action" /> to be called when the command is invoked.</param>
public DelegateCommand(Action execute)
{
Check.ArgumentNull(execute);
ExecuteDelegate = execute;
}
/// <summary>
/// Initializes a new instance of the <see cref="DelegateCommand" /> class with the specified execute and canExecute delegates.
/// </summary>
/// <param name="execute">The <see cref="Action" /> to be called when the command is invoked.</param>
/// <param name="canExecute">The <see cref="Func{TResult}" /> that determines whether the command can execute in its current state.</param>
public DelegateCommand(Action execute, Func<bool>? canExecute) : this(execute)
{
CanExecuteDelegate = canExecute;
}
/// <summary>
/// Executes the command. A call to <see cref="CanExecute" /> is performed to check if the command can be executed.
/// </summary>
public void Execute()
{
if (CanExecute())
{
ExecuteDelegate();
}
}
/// <summary>
/// Cetermines whether the command can execute in its current state.
/// </summary>
/// <returns>
/// <see langword="true" />, if the command can be executed;
/// otherwise, <see langword="false" />.
/// </returns>
public bool CanExecute()
{
return CanExecuteDelegate?.Invoke() ?? true;
}
void ICommand.Execute(object? parameter)
{
Execute();
}
bool ICommand.CanExecute(object? parameter)
{
return CanExecute();
}
}
/// <summary>
/// Represents a parameterized command which uses specified delegates for <see cref="ICommand.Execute(object)" /> and <see cref="ICommand.CanExecute(object)" />.
/// </summary>
/// <typeparam name="TParameter">The type of the command parameter.</typeparam>
public sealed class DelegateCommand<TParameter> : ICommand
{
private readonly Action<TParameter?> ExecuteDelegate;
private readonly Func<TParameter?, bool>? CanExecuteDelegate;
event EventHandler? ICommand.CanExecuteChanged
{
add
{
if (CanExecuteDelegate != null)
{
CommandManager.RequerySuggested += value;
}
}
remove
{
if (CanExecuteDelegate != null)
{
CommandManager.RequerySuggested -= value;
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="DelegateCommand{TParameter}" /> class with the specified execute delegate.
/// </summary>
/// <param name="execute">The <see cref="Action{T}" /> to be called when the command is invoked. The first argument of <paramref name="execute" /> is the command parameter.</param>
public DelegateCommand(Action<TParameter?> execute)
{
Check.ArgumentNull(execute);
ExecuteDelegate = execute;
}
/// <summary>
/// Initializes a new instance of the <see cref="DelegateCommand{TParameter}" /> class with the specified execute and canExecute delegates.
/// </summary>
/// <param name="execute">The <see cref="Action{T}" /> to be called when the command is invoked. The first argument of <paramref name="execute" /> is the command parameter.</param>
/// <param name="canExecute">The <see cref="Func{T, TResult}" /> that determines whether the command can execute in its current state. The first argument of <paramref name="canExecute" /> is the command parameter.</param>
public DelegateCommand(Action<TParameter?> execute, Func<TParameter?, bool>? canExecute) : this(execute)
{
CanExecuteDelegate = canExecute;
}
/// <summary>
/// Initializes a new instance of the <see cref="DelegateCommand{TParameter}" /> class with the specified execute and canExecute delegates.
/// </summary>
/// <param name="execute">The <see cref="Action{T}" /> to be called when the command is invoked. The first argument of <paramref name="execute" /> is the command parameter.</param>
/// <param name="canExecute">The <see cref="Func{TResult}" /> that determines whether the command can execute in its current state.</param>
public DelegateCommand(Action<TParameter?> execute, Func<bool>? canExecute) : this(execute, _ => canExecute?.Invoke() ?? true)
{
}
/// <summary>
/// Executes the command with the specified parameter. A call to <see cref="CanExecute(TParameter)" /> is performed to check if the command can be executed.
/// </summary>
/// <param name="parameter">The parameter which is passed to the execute and canExecute delegate.</param>
public void Execute(TParameter? parameter)
{
if (CanExecute(parameter))
{
ExecuteDelegate(parameter);
}
}
/// <summary>
/// Cetermines whether the command can execute in its current state.
/// </summary>
/// <param name="parameter">The parameter which is passed to the canExecute delegate.</param>
/// <returns>
/// <see langword="true" />, if the command can be executed;
/// otherwise, <see langword="false" />.
/// </returns>
public bool CanExecute(TParameter? parameter)
{
return CanExecuteDelegate?.Invoke(parameter) ?? true;
}
void ICommand.Execute(object? parameter)
{
Execute(CSharp.CastOrDefault<TParameter>(parameter));
}
bool ICommand.CanExecute(object? parameter)
{
return CanExecute(CSharp.CastOrDefault<TParameter>(parameter));
}
}