PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ViewModel/ViewModelBase.cs

https://github.com/shader/QuickArch
C# | 116 lines | 73 code | 17 blank | 26 comment | 9 complexity | 74255e204bdfca6c071ed881be052049 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel;
  6. using System.Diagnostics;
  7. using System.Windows.Input;
  8. namespace QuickArch.ViewModel
  9. {
  10. public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
  11. {
  12. RelayCommand _closeCommand;
  13. public virtual string DisplayName { get; set; }
  14. #region Debugging Aides
  15. /// <summary>
  16. /// Warns the developer if this object does not have
  17. /// a public property with the specified name. This
  18. /// method does not exist in a Release build.
  19. /// </summary>
  20. [Conditional("DEBUG")]
  21. [DebuggerStepThrough]
  22. public void VerifyPropertyName(string propertyName)
  23. {
  24. // Verify that the property name matches a real,
  25. // public, instance property on this object.
  26. if (TypeDescriptor.GetProperties(this)[propertyName] == null)
  27. {
  28. string msg = "Invalid property name: " + propertyName;
  29. if (this.ThrowOnInvalidPropertyName)
  30. throw new Exception(msg);
  31. else
  32. Debug.Fail(msg);
  33. }
  34. }
  35. /// <summary>
  36. /// Returns whether an exception is thrown, or if a Debug.Fail() is used
  37. /// when an invalid property name is passed to the VerifyPropertyName method.
  38. /// The default value is false, but subclasses used by unit tests might
  39. /// override this property's getter to return true.
  40. /// </summary>
  41. protected virtual bool ThrowOnInvalidPropertyName { get; private set; }
  42. #endregion // Debugging Aides
  43. #region CloseCommand
  44. //returns the command that attempts to remove this workspace from the UI
  45. public ICommand CloseCommand
  46. {
  47. get
  48. {
  49. if (_closeCommand == null)
  50. _closeCommand = new RelayCommand(param => this.OnRequestClose());
  51. return _closeCommand;
  52. }
  53. }
  54. #endregion
  55. #region RequestClose [event]
  56. //raised when this workspace should be removed from the UI
  57. public event EventHandler RequestClose;
  58. void OnRequestClose()
  59. {
  60. EventHandler handler = this.RequestClose;
  61. if (handler != null)
  62. handler(this, EventArgs.Empty);
  63. }
  64. #endregion
  65. #region INotifyPropertyChanged Members
  66. /// <summary>
  67. /// Raised when a property on this object has a new value.
  68. /// </summary>
  69. public event PropertyChangedEventHandler PropertyChanged;
  70. /// <summary>
  71. /// Raises this object's PropertyChanged event.
  72. /// </summary>
  73. /// <param name="propertyName">The property that has a new value.</param>
  74. protected virtual void OnPropertyChanged(string propertyName)
  75. {
  76. this.VerifyPropertyName(propertyName);
  77. PropertyChangedEventHandler handler = this.PropertyChanged;
  78. if (handler != null)
  79. {
  80. var e = new PropertyChangedEventArgs(propertyName);
  81. handler(this, e);
  82. }
  83. }
  84. #endregion // INotifyPropertyChanged Members
  85. #region IDisposable Members
  86. //invoked when this objected is being removed from the application and will
  87. //be subject to garbage collection
  88. public void Dispose()
  89. {
  90. this.OnDispose();
  91. }
  92. //Child classes can override this method to perform
  93. //clean up logic, such as removing event handlers
  94. protected virtual void OnDispose()
  95. {
  96. }
  97. #endregion
  98. }
  99. }