// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// A base class that implements the infrastructure for property change notification and automatically performs UI thread marshalling.
// Borrowed from Caliburn Micro
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrake.ApplicationServices.Utilities
{
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using HandBrake.ApplicationServices.Utilities.Interfaces;
///
/// A base class that implements the infrastructure for property change notification and automatically performs UI thread marshalling.
///
[Serializable]
public class PropertyChangedBase : INotifyPropertyChangedEx, INotifyPropertyChanged
{
[NonSerialized]
private bool isNotifying;
///
/// Gets or sets a value indicating whether the Enables/Disables property change notification.
///
[Browsable(false)]
public bool IsNotifying
{
get
{
return this.isNotifying;
}
set
{
this.isNotifying = value;
}
}
///
/// Occurs when a property value changes.
///
public event PropertyChangedEventHandler PropertyChanged = (param0, param1) => { };
///
/// Initializes a new instance of the class.
/// Creates an instance of .
///
public PropertyChangedBase()
{
this.IsNotifying = true;
}
///
/// Raises a change notification indicating that all bindings should be refreshed.
///
public void Refresh()
{
this.NotifyOfPropertyChange(string.Empty);
}
///
/// Notifies subscribers of the property change.
///
/// Name of the property.
public virtual void NotifyOfPropertyChange(string propertyName)
{
if (!this.IsNotifying)
return;
Execute.OnUIThread((System.Action)(() => this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName))));
}
///
/// Notifies subscribers of the property change.
///
/// The type of the property.The property expression.
public void NotifyOfPropertyChange(Expression> property)
{
this.NotifyOfPropertyChange(ExtensionMethods.GetMemberInfo((Expression)property).Name);
}
///
/// Raises the event directly.
///
/// The instance containing the event data.
[EditorBrowsable(EditorBrowsableState.Never)]
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler changedEventHandler = this.PropertyChanged;
if (changedEventHandler == null)
return;
changedEventHandler((object)this, e);
}
///
/// Called when the object is deserialized.
///
/// The streaming context.
[OnDeserialized]
public void OnDeserialized(StreamingContext c)
{
this.IsNotifying = true;
}
///
/// Used to indicate whether or not the IsNotifying property is serialized to Xml.
///
///
/// Whether or not to serialize the IsNotifying property. The default is false.
///
public virtual bool ShouldSerializeIsNotifying()
{
return false;
}
}
}