diff --git a/CefSharp.Wpf.Example/CefSharp.Wpf.Example.csproj b/CefSharp.Wpf.Example/CefSharp.Wpf.Example.csproj index 7b4b6c1a1..63fe18a9a 100644 --- a/CefSharp.Wpf.Example/CefSharp.Wpf.Example.csproj +++ b/CefSharp.Wpf.Example/CefSharp.Wpf.Example.csproj @@ -98,6 +98,7 @@ + MainView.xaml diff --git a/CefSharp.Wpf.Example/Mvvm/ViewModelBase.cs b/CefSharp.Wpf.Example/Mvvm/ViewModelBase.cs new file mode 100644 index 000000000..528139f2a --- /dev/null +++ b/CefSharp.Wpf.Example/Mvvm/ViewModelBase.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq.Expressions; + +namespace CefSharp.Wpf.Example.Mvvm +{ + public class ViewModelBase : ObjectBase, INotifyPropertyChanged + { + #region INotifyPropertyChanged + + private PropertyChangedEventHandler propertyChanged; + /// + public event PropertyChangedEventHandler PropertyChanged + { + add { if (!IsDisposed) propertyChanged += value; } + remove { propertyChanged -= value; } + } + + /// + /// Is called when a property is changed and raises a + /// + /// + /// The old value. + /// The new value. + /// The instance containing the event data. + protected virtual void OnPropertyChanged(T oldvalue, T newValue, PropertyChangedEventArgs e) + { + var handlers = propertyChanged; + if (handlers == null) + { + return; + } + + handlers(this, e); + } + + public static PropertyChangedEventArgs GetArgs(Expression> propertyexpression) + { + if (propertyexpression == null) + { + throw new ArgumentNullException("propertyexpression"); + } + + var body = propertyexpression.Body as MemberExpression; + if (body == null) + { + throw new ArgumentException("Lambda must return a property."); + } + + return new PropertyChangedEventArgs(body.Member.Name); + } + + /// + /// Sets the specified field and calls if the value changed. + /// + /// + /// The field. + /// The value. + /// The instance containing the event data. + protected void Set(ref T field, T value, PropertyChangedEventArgs e) + { + var oldvalue = field; + + if (EqualityComparer.Default.Equals(oldvalue, value)) + { + return; + } + + field = value; + + OnPropertyChanged(oldvalue, value, e); + } + + #endregion + } +} diff --git a/CefSharp/ObjectBase.cs b/CefSharp/ObjectBase.cs index f4bc8730a..c97965e5b 100644 --- a/CefSharp/ObjectBase.cs +++ b/CefSharp/ObjectBase.cs @@ -1,14 +1,12 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq.Expressions; namespace CefSharp { - // TODO: Why do we need this one? - // TODO: Would like to get rid of the INotifyPropertyChanged part of this; it doesn't belong in CefSharp IMO. More so in the - // WPF example, or something like that. - public class ObjectBase : INotifyPropertyChanged, IDisposable + /// + /// This Class provides tool functionality for managed Dispose Pattern + /// http://msdn.microsoft.com/en-us/library/fs2xkftw(v=vs.110).aspx + /// + public class ObjectBase : IDisposable { #region IDisposable @@ -38,63 +36,31 @@ namespace CefSharp protected virtual void DoDispose(bool isDisposing) { - propertyChanged = null; } + /// + /// If this Object is disposed the Method will throw a . + /// Use this method to guard methods that are not allowed to be called after dispose was called + /// + /// + protected virtual void DisposeGuard() + { + if (IsDisposed) + { + throw new ObjectDisposedException(GetType().FullName); + } + } + + /// + /// Gets a value indicating whether this instance is disposed. + /// This Value Should be used for lifetime management. + /// i.e. EventHandlers may not be registered if this object is already disposed + /// + /// + /// true if this instance is disposed; otherwise, false. + /// public bool IsDisposed { get; private set; } #endregion - - #region INotifyPropertyChanged - - private PropertyChangedEventHandler propertyChanged; - public event PropertyChangedEventHandler PropertyChanged - { - add { if (!IsDisposed) propertyChanged += value; } - remove { propertyChanged -= value; } - } - - protected virtual void OnPropertyChanged(T oldvalue, T newValue, PropertyChangedEventArgs e) - { - var handlers = propertyChanged; - if (handlers == null) - { - return; - } - - handlers(this, e); - } - - public static PropertyChangedEventArgs GetArgs(Expression> propertyexpression) - { - if (propertyexpression == null) - { - throw new ArgumentNullException("propertyexpression"); - } - - var body = propertyexpression.Body as MemberExpression; - if (body == null) - { - throw new ArgumentException("Lambda must return a property."); - } - - return new PropertyChangedEventArgs(body.Member.Name); - } - - protected void Set(ref T field, T value, PropertyChangedEventArgs e) - { - var oldvalue = field; - - if (EqualityComparer.Default.Equals(oldvalue, value)) - { - return; - } - - field = value; - - OnPropertyChanged(oldvalue, value, e); - } - - #endregion } }