Microscopic Traffic Simulator

- Main Page
- Packages
- Classes
- Files

- File List

ViewModelBase.cs
Go to the documentation of this file.
1 using Microscopic_Traffic_Simulator.ViewModels.Messages;
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 }
Microscopic_Traffic_Simulator.ViewModels.Messages
Definition: Message.cs:4
Microscopic_Traffic_Simulator.ViewModels.ViewModelBase.VerifyPropertyName
void VerifyPropertyName(string propertyName)
Warns the developer if this object does not have a public property with the specified name...
Definition: ViewModelBase.cs:27
Microscopic_Traffic_Simulator.ViewModels.ViewModelBase.OnDispose
virtual void OnDispose()
Child classes can override this method to perform clean-up logic, such as removing event handlers...
Definition: ViewModelBase.cs:84
System
Microscopic_Traffic_Simulator.ViewModels.ViewModelBase.Dispose
void Dispose()
Invoked when this object is being removed from the application and will be subject to garbage collect...
Definition: ViewModelBase.cs:75
Microscopic_Traffic_Simulator.ViewModels.Messages.Messenger
Class representing messenger for communicating between viewmodels.
Definition: Messenger.cs:9
Microscopic_Traffic_Simulator.ViewModels
Definition: CanvasViewModel.cs:8
Microscopic_Traffic_Simulator.ViewModels.ViewModelBase.ViewModelBase
ViewModelBase()
Create viewmodel base.
Definition: ViewModelBase.cs:18
Microscopic_Traffic_Simulator.ViewModels.ViewModelBase
Definition: ViewModelBase.cs:8
Microscopic_Traffic_Simulator
Definition: App.xaml.cs:12
Microscopic_Traffic_Simulator.ViewModels.ViewModelBase.PropertyChanged
PropertyChangedEventHandler PropertyChanged
Raised when a property on this object has a new value.
Definition: ViewModelBase.cs:53
Microscopic_Traffic_Simulator.ViewModels.ViewModelBase.messenger
Messenger messenger
Instance of messenger for communicating between view models.
Definition: ViewModelBase.cs:13
Microscopic_Traffic_Simulator.ViewModels.ViewModelBase.OnPropertyChanged
virtual void OnPropertyChanged(string propertyName)
Raises this object&#39;s PropertyChanged event.
Definition: ViewModelBase.cs:59