-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.cs
88 lines (73 loc) · 2.04 KB
/
Command.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
using System;
using System.Windows.Input;
namespace CodeMonkeys.MVVM.Commands
{
public class Command :
ICommand
{
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
public event EventHandler CanExecuteChanged;
public Command(
Action<object> execute)
{
if (execute == null)
throw new ArgumentNullException(
nameof(execute));
_execute = execute;
}
public Command(
Action execute)
: this(parameter => execute())
{
if (execute == null)
throw new ArgumentNullException(
nameof(execute));
}
public Command(
Action<object> execute,
Predicate<object> canExecute)
: this(execute)
{
if (canExecute == null)
throw new ArgumentNullException(
nameof(canExecute));
_canExecute = canExecute;
}
public Command(
Action execute,
Func<bool> canExecute)
: this(
parameter => execute(),
parameter => canExecute())
{
if (execute == null)
throw new ArgumentNullException(
nameof(execute));
if (canExecute == null)
throw new ArgumentNullException(
nameof(canExecute));
}
public void Execute(
object parameter)
{
_execute(
parameter);
}
public bool CanExecute(
object parameter)
{
if (_canExecute != null)
return _canExecute(
parameter);
return true;
}
public void UpdateCanExecute()
{
var threadSafeCall = CanExecuteChanged;
threadSafeCall?.Invoke(
this,
EventArgs.Empty);
}
}
}