-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionCommand.cs
More file actions
85 lines (77 loc) · 2.27 KB
/
ActionCommand.cs
File metadata and controls
85 lines (77 loc) · 2.27 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace UniversalPlatformTools
{
/// <summary>
/// An implementation of <see cref="ICommand"/> wrapping a method of type <see cref="Action"/>/>.
/// </summary>
public class ActionCommand : ICommand
{
private Action action;
/// <summary>
/// Initializes a new instance of the class ignoring the <see cref="ICommand"/> parameter.
/// </summary>
/// <param name="action">The <see cref="Action"/></param>
public ActionCommand(Action action)
{
this.action = action;
}
public void Execute(object parameter) => action?.Invoke();
bool ICommand.CanExecute(object parameter)
{
return true;
}
private EventHandler CanExcecuteChanged;
event EventHandler ICommand.CanExecuteChanged
{
add
{
CanExcecuteChanged += value;
}
remove
{
CanExcecuteChanged -= value;
}
}
}
/// <summary>
/// An implementation of <see cref="ICommand"/> wrapping a method of type <see cref="Action{T}"/>/>.
/// </summary>
public class ActionCommand<T>: ICommand
{
private readonly Action<T> action;
/// <summary>
/// Initializes a new instance of the class using a parameter to pass by the <see cref="ICommand.Execute(object)"/> method.
/// </summary>
/// <param name="action"></param>
public ActionCommand(Action<T> action)
{
this.action = action;
}
public void Execute(T parameter) => action?.Invoke(parameter);
bool ICommand.CanExecute(object parameter)
{
return true;
}
void ICommand.Execute(object parameter)
{
Execute((T)parameter);
}
private EventHandler CanExcecuteChanged;
event EventHandler ICommand.CanExecuteChanged
{
add
{
CanExcecuteChanged += value;
}
remove
{
CanExcecuteChanged -= value;
}
}
}
}