-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathCommand.cs
81 lines (70 loc) · 2.17 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
using System;
namespace Template10.Mvvm
{
// http://codepaste.net/jgxazh
using System.Diagnostics;
public class Command : System.Windows.Input.ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
public event EventHandler CanExecuteChanged;
public Command(Action execute, Func<bool> canexecute = null)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canexecute ?? (() => true);
}
[DebuggerStepThrough]
public bool CanExecute(object p)
{
try { return _canExecute(); }
catch { return false; }
}
public void Execute(object p)
{
if (!CanExecute(p))
return;
try { _execute(); }
catch { Debugger.Break(); }
}
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
public class Command<T> : System.Windows.Input.ICommand
{
private readonly Action<T> _execute;
private readonly Func<T, bool> _canExecute;
public event EventHandler CanExecuteChanged;
public Command(Action<T> execute, Func<T, bool> canexecute = null)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canexecute ?? (e => true);
}
[DebuggerStepThrough]
public bool CanExecute(object p)
{
try
{
var _Value = (T)Convert.ChangeType(p, typeof(T));
return _canExecute == null ? true : _canExecute(_Value);
}
catch { return false; }
}
public void Execute(object p)
{
if (!CanExecute(p))
return;
var _Value = (T)Convert.ChangeType(p, typeof(T));
_execute(_Value);
}
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
}