-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathObservableWindow.cs
More file actions
203 lines (186 loc) · 9.67 KB
/
ObservableWindow.cs
File metadata and controls
203 lines (186 loc) · 9.67 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Threading;
namespace BytecodeApi.Wpf.Controls;
/// <summary>
/// Class that wraps a <see cref="Window" /> object, implementing the <see cref="INotifyPropertyChanged" /> interface.
/// </summary>
public class ObservableWindow : Window, INotifyPropertyChanged, INotifyPropertyChanging
{
/// <summary>
/// Occurs when a property value is changing.
/// </summary>
public event PropertyChangingEventHandler? PropertyChanging;
/// <summary>
/// Occurs when a property value has changed.
/// </summary>
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// Initializes a new instance of the <see cref="ObservableWindow" /> class.
/// </summary>
public ObservableWindow()
{
}
/// <summary>
/// Executes the specified <see cref="Action" /> synchronously on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
public void Dispatch(Action callback)
{
Check.ArgumentNull(callback);
Dispatcher.Invoke(callback);
}
/// <summary>
/// Executes the specified <see cref="Action" /> synchronously at the specified priority on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <param name="priority">The priority that determines in what order the specified callback is invoked relative to the other pending operations in the <see cref="Dispatcher" />.</param>
public void Dispatch(Action callback, DispatcherPriority priority)
{
Check.ArgumentNull(callback);
Dispatcher.Invoke(callback, priority);
}
/// <summary>
/// Executes the specified <see cref="Action" /> synchronously at the specified priority on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <param name="priority">The priority that determines in what order the specified callback is invoked relative to the other pending operations in the <see cref="Dispatcher" />.</param>
/// <param name="cancellationToken">An object that indicates whether to cancel the action.</param>
public void Dispatch(Action callback, DispatcherPriority priority, CancellationToken cancellationToken)
{
Check.ArgumentNull(callback);
Dispatcher.Invoke(callback, priority, cancellationToken);
}
/// <summary>
/// Executes the specified <see cref="Action" /> synchronously at the specified priority on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <param name="priority">The priority that determines in what order the specified callback is invoked relative to the other pending operations in the <see cref="Dispatcher" />.</param>
/// <param name="cancellationToken">An object that indicates whether to cancel the action.</param>
/// <param name="timeout">The minimum amount of time to wait for the operation to start.</param>
public void Dispatch(Action callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout)
{
Check.ArgumentNull(callback);
Dispatcher.Invoke(callback, priority, cancellationToken, timeout);
}
/// <summary>
/// Executes the specified <see cref="Func{TResult}" /> synchronously on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <typeparam name="T">The return type of <paramref name="callback" />.</typeparam>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <returns>
/// The result of <paramref name="callback" />.
/// </returns>
public T Dispatch<T>(Func<T> callback)
{
Check.ArgumentNull(callback);
return Dispatcher.Invoke(callback);
}
/// <summary>
/// Executes the specified <see cref="Func{TResult}" /> synchronously at the specified priority on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <typeparam name="T">The return type of <paramref name="callback" />.</typeparam>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <param name="priority">The priority that determines in what order the specified callback is invoked relative to the other pending operations in the <see cref="Dispatcher" />.</param>
/// <returns>
/// The result of <paramref name="callback" />.
/// </returns>
public T Dispatch<T>(Func<T> callback, DispatcherPriority priority)
{
Check.ArgumentNull(callback);
return Dispatcher.Invoke(callback, priority);
}
/// <summary>
/// Executes the specified <see cref="Func{TResult}" /> synchronously at the specified priority on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <typeparam name="T">The return type of <paramref name="callback" />.</typeparam>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <param name="priority">The priority that determines in what order the specified callback is invoked relative to the other pending operations in the <see cref="Dispatcher" />.</param>
/// <param name="cancellationToken">An object that indicates whether to cancel the action.</param>
/// <returns>
/// The result of <paramref name="callback" />.
/// </returns>
public T Dispatch<T>(Func<T> callback, DispatcherPriority priority, CancellationToken cancellationToken)
{
Check.ArgumentNull(callback);
return Dispatcher.Invoke(callback, priority, cancellationToken);
}
/// <summary>
/// Executes the specified <see cref="Func{TResult}" /> synchronously at the specified priority on the thread the <see cref="Dispatcher" /> is associated with.
/// </summary>
/// <typeparam name="T">The return type of <paramref name="callback" />.</typeparam>
/// <param name="callback">A delegate to invoke through the dispatcher.</param>
/// <param name="priority">The priority that determines in what order the specified callback is invoked relative to the other pending operations in the <see cref="Dispatcher" />.</param>
/// <param name="cancellationToken">An object that indicates whether to cancel the action.</param>
/// <param name="timeout">The minimum amount of time to wait for the operation to start.</param>
/// <returns>
/// The result of <paramref name="callback" />.
/// </returns>
public T Dispatch<T>(Func<T> callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout)
{
Check.ArgumentNull(callback);
return Dispatcher.Invoke(callback, priority, cancellationToken, timeout);
}
/// <summary>
/// Method that can be used by the <see langword="set" /> accessor of a property. This method raises the <see cref="PropertyChanging" /> event and the <see cref="PropertyChanged" /> event.
/// </summary>
/// <typeparam name="T">The type of the property.</typeparam>
/// <param name="field">A reference to the backing field of the property.</param>
/// <param name="value">The new value for the property.</param>
/// <param name="propertyName">A <see cref="string" /> specifying the name of the property. If <see langword="null" /> is provided, the <see cref="CallerMemberNameAttribute" /> is used to automatically get the property name.</param>
/// <returns>
/// <see langword="true" />, if the value changed; <see langword="false" />, if the new value is equal to the old value.
/// </returns>
protected virtual bool Set<T>(ref T field, T value, [CallerMemberName] string propertyName = null!)
{
Check.ArgumentNull(propertyName);
if (!EqualityComparer<T>.Default.Equals(field, value))
{
RaisePropertyChanging(propertyName);
field = value;
RaisePropertyChanged(propertyName);
return true;
}
else
{
return false;
}
}
/// <summary>
/// Raises the <see cref="PropertyChanging" /> event on a property specified by a name.
/// </summary>
/// <param name="propertyName">A <see cref="string" /> specifying the name of the property. If <see langword="null" /> is provided, the <see cref="CallerMemberNameAttribute" /> is used to automatically get the property name.</param>
protected void RaisePropertyChanging([CallerMemberName] string propertyName = null!)
{
Check.ArgumentNull(propertyName);
Check.ArgumentEx.StringNotEmpty(propertyName);
OnPropertyChanging(new PropertyChangingEventArgs(propertyName));
}
/// <summary>
/// Raises the <see cref="PropertyChanged" /> event on a property specified by a name.
/// </summary>
/// <param name="propertyName">A <see cref="string" /> specifying the name of the property. If <see langword="null" /> is provided, the <see cref="CallerMemberNameAttribute" /> is used to automatically get the property name.</param>
protected void RaisePropertyChanged([CallerMemberName] string propertyName = null!)
{
Check.ArgumentNull(propertyName);
Check.ArgumentEx.StringNotEmpty(propertyName);
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// Raises the <see cref="PropertyChanging" /> event.
/// </summary>
/// <param name="e">The event data for the <see cref="PropertyChangingEventArgs" /> event.</param>
protected virtual void OnPropertyChanging(PropertyChangingEventArgs e)
{
PropertyChanging?.Invoke(this, e);
}
/// <summary>
/// Raises the <see cref="PropertyChanged" /> event.
/// </summary>
/// <param name="e">The event data for the <see cref="PropertyChangedEventArgs" /> event.</param>
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChanged?.Invoke(this, e);
}
}