using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Threading;
using Kent.Boogaart.HelperTrinity.Extensions;
namespace Kent.Boogaart.MVVM
{
///
/// A base class for all view models.
///
///
///
/// This class serves as a base class for view models. It implements and provides access to a .
///
///
public abstract class ViewModel : INotifyPropertyChanged
{
private readonly Dispatcher _dispatcher;
///
/// Constructs an instance of ViewModel.
///
protected ViewModel()
{
if (Application.Current != null)
{
_dispatcher = Application.Current.Dispatcher;
}
else
{
//this is useful for unit tests where there is no application running
_dispatcher = Dispatcher.CurrentDispatcher;
}
}
///
/// Occurs whenever a property on this object changes in value.
///
[field:NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
///
/// Gets a that can be used to dispatch messages to the UI thread.
///
protected Dispatcher Dispatcher
{
get { return _dispatcher; }
}
///
/// Raises the event.
///
///
/// The event arguments.
///
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChanged.Raise(this, e);
}
///
/// A convenience method that raises the event for a specified property name.
///
///
/// The name of the property that changed.
///
protected void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
}
}