Microscopic Traffic Simulator
ViewModelBase.cs
Go to the documentation of this file.
2 using System;
3 using System.ComponentModel;
4 using System.Diagnostics;
5 
6 namespace Microscopic_Traffic_Simulator.ViewModels
7 {
8  abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
9  {
13  protected Messenger messenger;
14 
18  protected ViewModelBase() { }
19 
25  [Conditional("DEBUG")]
26  [DebuggerStepThrough]
27  public void VerifyPropertyName(string propertyName)
28  {
29  // Verify that the property name matches a real,
30  // public, instance property on this object.
31  if (propertyName != string.Empty && TypeDescriptor.GetProperties(this)[propertyName] == null)
32  {
33  string msg = "Invalid property name: " + propertyName;
34 
35  if (this.ThrowOnInvalidPropertyName)
36  throw new Exception(msg);
37  else
38  Debug.Fail(msg);
39  }
40  }
41 
48  protected virtual bool ThrowOnInvalidPropertyName { get; private set; }
49 
53  public event PropertyChangedEventHandler PropertyChanged;
54 
59  protected virtual void OnPropertyChanged(string propertyName)
60  {
61  this.VerifyPropertyName(propertyName);
62 
63  PropertyChangedEventHandler handler = this.PropertyChanged;
64  if (handler != null)
65  {
66  var e = new PropertyChangedEventArgs(propertyName);
67  handler(this, e);
68  }
69  }
70 
75  public void Dispose()
76  {
77  this.OnDispose();
78  }
79 
84  protected virtual void OnDispose() { }
85 
86 #if DEBUG
87  ~ViewModelBase()
91  {
92  string msg = string.Format("{0} ({1}) Finalized", this.GetType().Name, this.GetHashCode());
93  System.Diagnostics.Debug.WriteLine(msg);
94  }
95 #endif
96  }
97 }
void VerifyPropertyName(string propertyName)
Warns the developer if this object does not have a public property with the specified name...
virtual void OnDispose()
Child classes can override this method to perform clean-up logic, such as removing event handlers...
void Dispose()
Invoked when this object is being removed from the application and will be subject to garbage collect...
Class representing messenger for communicating between viewmodels.
Definition: Messenger.cs:9
PropertyChangedEventHandler PropertyChanged
Raised when a property on this object has a new value.
Messenger messenger
Instance of messenger for communicating between view models.
virtual void OnPropertyChanged(string propertyName)
Raises this object's PropertyChanged event.