-
Notifications
You must be signed in to change notification settings - Fork 47
/
ModelBase.cs
83 lines (65 loc) · 2.43 KB
/
ModelBase.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
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Jaya.Shared.Base
{
public abstract class ModelBase : INotifyPropertyChanged
{
readonly Dictionary<string, object> _variables;
public event PropertyChangedEventHandler PropertyChanged;
protected ModelBase()
{
_variables = new Dictionary<string, object>();
}
~ModelBase()
{
_variables.Clear();
}
protected T Get<T>([CallerMemberName]string propertyName = null)
{
if (string.IsNullOrEmpty(propertyName))
return default;
if (_variables.ContainsKey(propertyName))
return (T)_variables[propertyName];
return default;
}
protected bool Set<T>(T value, [CallerMemberName]string propertyName = null, bool raiseNotification = true)
{
if (string.IsNullOrEmpty(propertyName))
return false;
if (value == null)
return false;
else if (value != null && _variables.ContainsKey(propertyName) && (object)value == _variables[propertyName])
return false;
if (_variables.ContainsKey(propertyName))
_variables[propertyName] = value;
else
_variables.Add(propertyName, value);
if (raiseNotification)
RaisePropertyChanged(propertyName);
return true;
}
protected bool Set<T>(ref T backingField, T value, [CallerMemberName]string propertyName = null, bool raiseNotification = true)
{
if (string.IsNullOrEmpty(propertyName))
return false;
if (value == null && backingField == null)
return false;
else if (value != null && value.Equals(backingField))
return false;
backingField = value;
if (raiseNotification)
RaisePropertyChanged(propertyName);
return true;
}
public void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
var handler = PropertyChanged;
if (handler == null)
return;
if (string.IsNullOrEmpty(propertyName))
return;
handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}