-
Notifications
You must be signed in to change notification settings - Fork 3
/
Command.cs
48 lines (40 loc) · 1.18 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
namespace Xakpc.BlazorBits.Bootstrap
{
/// <summary>
/// Very primitive simple generic command
/// </summary>
/// <typeparam name="T"></typeparam>
public class Command<T> : ICommand
{
private readonly Action<T> _action;
private readonly Func<T, bool> _canDoAction;
public Command(Action<T> action, Func<T, bool> canDoAction = null)
{
_action = action;
_canDoAction = canDoAction;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(T parameter)
{
return _canDoAction?.Invoke(parameter) ?? true;
}
public bool CanExecute(object parameter)
{
return CanExecute(parameter == null ? default : (T)parameter);
}
public void Execute(T parameter)
{
if (CanExecute(parameter))
_action.Invoke(parameter);
}
public void Execute(object parameter)
{
Execute(parameter == null ? default : (T)parameter);
}
}
}