-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDemoClass.cs
66 lines (58 loc) · 2.72 KB
/
DemoClass.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
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
namespace MvvmTools.Demo
{
public class DemoClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
bool Set<T>(ref T field, T newValue, [CallerMemberName] string property = "")
{
if (Equals(field, newValue)) return false;
field = newValue;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
(ExecWithParameterAndConditionCommand as Commands.ICommandNotify)?.NotifyCanExecuteChanged();
return true;
}
readonly Action<string> ShowMessage;
public DemoClass() : this(null) { }
public DemoClass(Action<string> showMessage = null)
{
ShowMessage = showMessage ?? (s => System.Diagnostics.Debug.WriteLine(s));
EnableAllCommands = true;
CreateCommands();
}
void CreateCommands()
{
_Parameter = "Test";
ExecCommand = Command.Create(Exec, nameof(CanExec), this);
ExecWithParameterCommand = this.CreateCommand<string>(ExecWithParameter, nameof(CanExecWithParameter));
ExecWithParameterAndConditionCommand = this.CreateCommand<string>(ExecWithParameterAndCondition, nameof(CanExecWithParameterAndCondition));
}
bool _EnableAllCommands;
public bool EnableAllCommands
{
get { return _EnableAllCommands; }
set
{
Set(ref _EnableAllCommands, value);
CanExec = value;
CanExecWithParameter = value;
}
}
private string _Parameter;
public string Parameter { get => _Parameter; set => Set(ref _Parameter, value); }
public void Exec() => ShowMessage("This is a parameterless function.");
bool _CanExec;
public bool CanExec { get => _CanExec; set => Set(ref _CanExec, value); }
public ICommand ExecCommand { get; set; }
public void ExecWithParameter(string parameter) => ShowMessage("This is a parameter function:\n\n" + parameter);
bool _CanExecWithParameter;
public bool CanExecWithParameter { get => _CanExecWithParameter; set => Set(ref _CanExecWithParameter, value); }
public ICommand ExecWithParameterCommand { get; set; }
public void ExecWithParameterAndCondition(string parameter) => ShowMessage("This is a parameter function that execute if parameter value is:\n\n" + parameter);
public bool CanExecWithParameterAndCondition(string parameter) => parameter == "mvvmtools";
public ICommand ExecWithParameterAndConditionCommand { get; set; }
}
}