-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathObservableObject.cs
63 lines (56 loc) · 2.52 KB
/
ObservableObject.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
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
public class ObservableObject : INotifyPropertyChanged
{
/// <summary>Occurs when a property value changes. </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>Updates the property and raises the changed event, but only if the new value does not equal the old value. </summary>
/// <param name="propertyName">The property name as lambda. </param>
/// <param name="oldValue">A reference to the backing field of the property. </param>
/// <param name="newValue">The new value. </param>
/// <returns>True if the property has changed. </returns>
protected bool Set<T>(ref T oldValue, T newValue, [CallerMemberName] String propertyName = null)
{
return Set(propertyName, ref oldValue, newValue);
}
/// <summary>Updates the property and raises the changed event, but only if the new value does not equal the old value. </summary>
/// <param name="propertyName">The property name as lambda. </param>
/// <param name="oldValue">A reference to the backing field of the property. </param>
/// <param name="newValue">The new value. </param>
/// <returns>True if the property has changed. </returns>
protected virtual bool Set<T>(String propertyName, ref T oldValue, T newValue)
{
if (Equals(oldValue, newValue))
{
return false;
}
oldValue = newValue;
RaisePropertyChanged(new PropertyChangedEventArgs(propertyName));
return true;
}
/// <summary>Raises the property changed event. </summary>
/// <param name="propertyName">The property name. </param>
protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
RaisePropertyChanged(new PropertyChangedEventArgs(propertyName));
}
/// <summary>Raises the property changed event. </summary>
/// <param name="args">The arguments. </param>
protected async virtual void RaisePropertyChanged(PropertyChangedEventArgs args)
{
if (InitializeSwitch.Dispatcher != null)
{
await InitializeSwitch.Dispatcher?.RunAsync(() => { PropertyChanged?.Invoke(this, args); });
}
else
{
PropertyChanged?.Invoke(this, args);
}
}
/// <summary>Raises the property changed event for all properties (string.Empty). </summary>
protected void RaiseAllPropertiesChanged()
{
RaisePropertyChanged(new PropertyChangedEventArgs(string.Empty));
}
}