This repository has been archived by the owner on Nov 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
DelegateCommand.cs
103 lines (93 loc) · 3.25 KB
/
DelegateCommand.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
namespace WinUX.Mvvm.Input
{
using System;
using System.Diagnostics;
using System.Windows.Input;
/// <summary>
/// Defines a command for executing an action without a parameter.
/// </summary>
public sealed class DelegateCommand : ICommand
{
private readonly Action executeAction;
private readonly Func<bool> canExecute;
/// <summary>
/// Initializes a new instance of the <see cref="DelegateCommand"/> class.
/// </summary>
/// <param name="executeAction">
/// The action to execute when called.
/// </param>
/// <exception cref="ArgumentNullException">
/// Thrown if the execute action is null.
/// </exception>
public DelegateCommand(Action executeAction)
: this(executeAction, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DelegateCommand"/> class.
/// </summary>
/// <param name="executeAction">
/// The action to execute when called.
/// </param>
/// <param name="canExecute">
/// The function to call to determine if the command can execute the action.
/// </param>
/// <exception cref="ArgumentNullException">
/// Thrown if the execute action is null.
/// </exception>
public DelegateCommand(Action executeAction, Func<bool> canExecute)
{
if (executeAction == null) throw new ArgumentNullException(nameof(executeAction));
this.executeAction = executeAction;
this.canExecute = canExecute ?? (() => true);
}
/// <summary>
/// Occurs when changes occur that affect whether or not the command should execute.
/// </summary>
public event EventHandler CanExecuteChanged;
/// <summary>
/// Defines the method that determines whether the command can execute in its current state.
/// </summary>
/// <returns>
/// true if this command can be executed; otherwise, false.
/// </returns>
/// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
public bool CanExecute(object parameter)
{
try
{
return this.canExecute();
}
catch
{
return false;
}
}
/// <summary>
/// Defines the method to be called when the command is invoked.
/// </summary>
/// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
public void Execute(object parameter)
{
if (!this.CanExecute(parameter))
{
return;
}
try
{
this.executeAction();
}
catch
{
Debugger.Break();
}
}
/// <summary>
/// Raises the can execute changed event.
/// </summary>
public void RaiseCanExecuteChanged()
{
this.CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
}