-
Notifications
You must be signed in to change notification settings - Fork 3
/
ViewModelBase.cs
330 lines (312 loc) · 13.7 KB
/
ViewModelBase.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Decompiled with JetBrains decompiler
// Type: GalaSoft.MvvmLight.ViewModelBase
// Assembly: GalaSoft.MvvmLight, Version=5.1.1.35049, Culture=neutral, PublicKeyToken=e7570ab207bcb616
// MVID: 5BEDB485-56A9-4001-8E5C-95D46B1034CD
// Assembly location: C:\Users\Asus\OneDrive\Repos\MyDiary\packages\MvvmLightLibs.5.1.1.0\lib\net45\GalaSoft.MvvmLight.dll
using GalaSoft.MvvmLight.Helpers;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace GalaSoft.MvvmLight
{
/// <summary>
/// A base class for the ViewModel classes in the MVVM pattern.
/// </summary>
public abstract class ViewModelBase : ObservableObject, ICleanup
{
private static bool? _isInDesignMode;
private IMessenger _messengerInstance;
/// <summary>
/// Initializes a new instance of the ViewModelBase class.
/// </summary>
public ViewModelBase()
: this((IMessenger) null)
{
}
/// <summary>
/// Initializes a new instance of the ViewModelBase class.
/// </summary>
/// <param name="messenger">An instance of a <see cref="T:GalaSoft.MvvmLight.Messaging.Messenger" />
/// used to broadcast messages to other objects. If null, this class
/// will attempt to broadcast using the Messenger's default
/// instance.</param>
public ViewModelBase(IMessenger messenger) => this.MessengerInstance = messenger;
/// <summary>
/// Gets a value indicating whether the control is in design mode
/// (running under Blend or Visual Studio).
/// </summary>
public bool IsInDesignMode => ViewModelBase.IsInDesignModeStatic;
/// <summary>
/// Gets a value indicating whether the control is in design mode
/// (running in Blend or Visual Studio).
/// </summary>
public static bool IsInDesignModeStatic
{
get
{
if (!ViewModelBase._isInDesignMode.HasValue)
ViewModelBase._isInDesignMode = new bool?(ViewModelBase.IsInDesignModePortable());
return ViewModelBase._isInDesignMode.Value;
}
}
private static bool IsInDesignModePortable()
{
switch (DesignerLibrary.DetectedDesignerLibrary)
{
case DesignerPlatformLibrary.Net:
return ViewModelBase.IsInDesignModeNet();
case DesignerPlatformLibrary.WinRt:
return ViewModelBase.IsInDesignModeMetro();
case DesignerPlatformLibrary.Silverlight:
bool flag = ViewModelBase.IsInDesignModeSilverlight();
if (!flag)
flag = ViewModelBase.IsInDesignModeNet();
return flag;
default:
return false;
}
}
private static bool IsInDesignModeSilverlight()
{
try
{
Type type = Type.GetType("System.ComponentModel.DesignerProperties, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e");
if (type == null)
return false;
PropertyInfo declaredProperty = type.GetTypeInfo().GetDeclaredProperty("IsInDesignTool");
return declaredProperty != null && (bool) declaredProperty.GetValue((object) null, (object[]) null);
}
catch
{
return false;
}
}
private static bool IsInDesignModeMetro()
{
try
{
return (bool) Type.GetType("Windows.ApplicationModel.DesignMode, Windows, ContentType=WindowsRuntime").GetTypeInfo().GetDeclaredProperty("DesignModeEnabled").GetValue((object) null, (object[]) null);
}
catch
{
return false;
}
}
private static bool IsInDesignModeNet()
{
try
{
Type type1 = Type.GetType("System.ComponentModel.DesignerProperties, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
if (type1 == null)
return false;
object obj1 = type1.GetTypeInfo().GetDeclaredField("IsInDesignModeProperty").GetValue((object) null);
Type type2 = Type.GetType("System.ComponentModel.DependencyPropertyDescriptor, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
Type type3 = Type.GetType("System.Windows.FrameworkElement, PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
if (type2 == null || type3 == null)
return false;
List<MethodInfo> list = type2.GetTypeInfo().GetDeclaredMethods("FromProperty").ToList<MethodInfo>();
if (list == null || list.Count == 0)
return false;
MethodInfo methodInfo = list.FirstOrDefault<MethodInfo>((Func<MethodInfo, bool>) (mi => mi.IsPublic && mi.IsStatic && mi.GetParameters().Length == 2));
if (methodInfo == null)
return false;
object obj2 = methodInfo.Invoke((object) null, new object[2]
{
obj1,
(object) type3
});
if (obj2 == null)
return false;
PropertyInfo declaredProperty1 = type2.GetTypeInfo().GetDeclaredProperty("Metadata");
if (declaredProperty1 == null)
return false;
object obj3 = declaredProperty1.GetValue(obj2, (object[]) null);
Type type4 = Type.GetType("System.Windows.PropertyMetadata, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
if (obj3 == null || type4 == null)
return false;
PropertyInfo declaredProperty2 = type4.GetTypeInfo().GetDeclaredProperty("DefaultValue");
return declaredProperty2 != null && (bool) declaredProperty2.GetValue(obj3, (object[]) null);
}
catch
{
return false;
}
}
/// <summary>
/// Gets or sets an instance of a <see cref="T:GalaSoft.MvvmLight.Messaging.IMessenger" /> used to
/// broadcast messages to other objects. If null, this class will
/// attempt to broadcast using the Messenger's default instance.
/// </summary>
protected IMessenger MessengerInstance
{
get => this._messengerInstance ?? Messenger.Default;
set => this._messengerInstance = value;
}
/// <summary>
/// Unregisters this instance from the Messenger class.
/// <para>To cleanup additional resources, override this method, clean
/// up and then call base.Cleanup().</para>
/// </summary>
public virtual void Cleanup() => this.MessengerInstance.Unregister((object) this);
/// <summary>
/// Broadcasts a PropertyChangedMessage using either the instance of
/// the Messenger that was passed to this class (if available)
/// or the Messenger's default instance.
/// </summary>
/// <typeparam name="T">The type of the property that
/// changed.</typeparam>
/// <param name="oldValue">The value of the property before it
/// changed.</param>
/// <param name="newValue">The value of the property after it
/// changed.</param>
/// <param name="propertyName">The name of the property that
/// changed.</param>
protected virtual void Broadcast<T>(T oldValue, T newValue, string propertyName) => this.MessengerInstance.Send<PropertyChangedMessage<T>>(new PropertyChangedMessage<T>((object) this, oldValue, newValue, propertyName));
/// <summary>
/// Raises the PropertyChanged event if needed, and broadcasts a
/// PropertyChangedMessage using the Messenger instance (or the
/// static default instance if no Messenger instance is available).
/// </summary>
/// <typeparam name="T">The type of the property that
/// changed.</typeparam>
/// <param name="propertyName">The name of the property that
/// changed.</param>
/// <param name="oldValue">The property's value before the change
/// occurred.</param>
/// <param name="newValue">The property's value after the change
/// occurred.</param>
/// <param name="broadcast">If true, a PropertyChangedMessage will
/// be broadcasted. If false, only the event will be raised.</param>
/// <remarks>If the propertyName parameter
/// does not correspond to an existing property on the current class, an
/// exception is thrown in DEBUG configuration only.</remarks>
protected virtual void RaisePropertyChanged<T>(
[CallerMemberName] string propertyName = null,
T oldValue = default(T),
T newValue = default(T),
bool broadcast = false)
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentException("This method cannot be called with an empty string", nameof (propertyName));
this.RaisePropertyChanged(propertyName);
if (!broadcast)
return;
this.Broadcast<T>(oldValue, newValue, propertyName);
}
/// <summary>
/// Raises the PropertyChanged event if needed, and broadcasts a
/// PropertyChangedMessage using the Messenger instance (or the
/// static default instance if no Messenger instance is available).
/// </summary>
/// <typeparam name="T">The type of the property that
/// changed.</typeparam>
/// <param name="propertyExpression">An expression identifying the property
/// that changed.</param>
/// <param name="oldValue">The property's value before the change
/// occurred.</param>
/// <param name="newValue">The property's value after the change
/// occurred.</param>
/// <param name="broadcast">If true, a PropertyChangedMessage will
/// be broadcasted. If false, only the event will be raised.</param>
protected virtual void RaisePropertyChanged<T>(
Expression<Func<T>> propertyExpression,
T oldValue,
T newValue,
bool broadcast)
{
PropertyChangedEventHandler propertyChangedHandler = this.PropertyChangedHandler;
if (propertyChangedHandler == null && !broadcast)
return;
string propertyName = ObservableObject.GetPropertyName<T>(propertyExpression);
if (propertyChangedHandler != null)
propertyChangedHandler((object) this, new PropertyChangedEventArgs(propertyName));
if (!broadcast)
return;
this.Broadcast<T>(oldValue, newValue, propertyName);
}
/// <summary>
/// Assigns a new value to the property. Then, raises the
/// PropertyChanged event if needed, and broadcasts a
/// PropertyChangedMessage using the Messenger instance (or the
/// static default instance if no Messenger instance is available).
/// </summary>
/// <typeparam name="T">The type of the property that
/// changed.</typeparam>
/// <param name="propertyExpression">An expression identifying the property
/// that changed.</param>
/// <param name="field">The field storing the property's value.</param>
/// <param name="newValue">The property's value after the change
/// occurred.</param>
/// <param name="broadcast">If true, a PropertyChangedMessage will
/// be broadcasted. If false, only the event will be raised.</param>
/// <returns>True if the PropertyChanged event was raised, false otherwise.</returns>
protected bool Set<T>(
Expression<Func<T>> propertyExpression,
ref T field,
T newValue,
bool broadcast)
{
if (EqualityComparer<T>.Default.Equals(field, newValue))
return false;
T oldValue = field;
field = newValue;
this.RaisePropertyChanged<T>(propertyExpression, oldValue, field, broadcast);
return true;
}
/// <summary>
/// Assigns a new value to the property. Then, raises the
/// PropertyChanged event if needed, and broadcasts a
/// PropertyChangedMessage using the Messenger instance (or the
/// static default instance if no Messenger instance is available).
/// </summary>
/// <typeparam name="T">The type of the property that
/// changed.</typeparam>
/// <param name="propertyName">The name of the property that
/// changed.</param>
/// <param name="field">The field storing the property's value.</param>
/// <param name="newValue">The property's value after the change
/// occurred.</param>
/// <param name="broadcast">If true, a PropertyChangedMessage will
/// be broadcasted. If false, only the event will be raised.</param>
/// <returns>True if the PropertyChanged event was raised, false otherwise.</returns>
protected bool Set<T>(string propertyName, ref T field, T newValue = default(T), bool broadcast = false)
{
if (EqualityComparer<T>.Default.Equals(field, newValue))
return false;
T oldValue = field;
field = newValue;
this.RaisePropertyChanged<T>(propertyName, oldValue, field, broadcast);
return true;
}
/// <summary>
/// Assigns a new value to the property. Then, raises the
/// PropertyChanged event if needed, and broadcasts a
/// PropertyChangedMessage using the Messenger instance (or the
/// static default instance if no Messenger instance is available).
/// </summary>
/// <typeparam name="T">The type of the property that
/// changed.</typeparam>
/// <param name="field">The field storing the property's value.</param>
/// <param name="newValue">The property's value after the change
/// occurred.</param>
/// <param name="broadcast">If true, a PropertyChangedMessage will
/// be broadcasted. If false, only the event will be raised.</param>
/// <param name="propertyName">(optional) The name of the property that
/// changed.</param>
/// <returns>True if the PropertyChanged event was raised, false otherwise.</returns>
protected bool Set<T>(ref T field, T newValue = default(T), bool broadcast = false, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, newValue))
return false;
T oldValue = field;
field = newValue;
this.RaisePropertyChanged<T>(propertyName, oldValue, field, broadcast);
return true;
}
}
}