Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / WPF

C.B.R.

Rate me:
Please Sign up or sign in to vote.
4.96/5 (52 votes)
22 Oct 2012GPL329 min read 124.3K   1.8K   132  
Comic and electronic publication reader with library management, extended file conversion, and devices support.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;

namespace CBR.Core.Helpers
{
	/// <summary>
	/// Implements the INotifyPropertyChanged interface and exposes a RaisePropertyChanged method for derived 
	/// classes to raise the PropertyChange event.  The event arguments created by this class are cached to prevent 
	/// managed heap fragmentation.
	/// </summary>
	[Serializable]
	public abstract class BindableObject : INotifyPropertyChanged
	{
		#region Data

		private static readonly Dictionary<string, PropertyChangedEventArgs> eventArgCache;
		private const string ERROR_MSG = "{0} is not a public property of {1}";

		#endregion // Data

		#region Constructors

		static BindableObject()
		{
			eventArgCache = new Dictionary<string, PropertyChangedEventArgs>();
		}

		protected BindableObject()
		{
		}

		#endregion // Constructors

		#region Public Members

		/// <summary>
		/// Raised when a public property of this object is set.
		/// </summary>
		[field:NonSerialized]
		public event PropertyChangedEventHandler PropertyChanged;

		/// <summary>
		/// Returns an instance of PropertyChangedEventArgs for 
		/// the specified property name.
		/// </summary>
		/// <param name="propertyName">
		/// The name of the property to create event args for.
		/// </param>		
		public static PropertyChangedEventArgs GetPropertyChangedEventArgs(string propertyName)
		{
			if (String.IsNullOrEmpty(propertyName))
				throw new ArgumentException( "propertyName cannot be null or empty.");

			PropertyChangedEventArgs args;

			// Get the event args from the cache, creating them
			// and adding to the cache if necessary.
			lock (typeof(BindableObject))
			{
				bool isCached = eventArgCache.ContainsKey(propertyName);
				if (!isCached)
				{
					eventArgCache.Add( propertyName, new PropertyChangedEventArgs(propertyName));
				}

				args = eventArgCache[propertyName];
			}

			return args;
		}

		#endregion // Public Members

		#region Protected Members

		/// <summary>
		/// Derived classes can override this method to
		/// execute logic after a property is set. The 
		/// base implementation does nothing.
		/// </summary>
		/// <param name="propertyName">
		/// The property which was changed.
		/// </param>
		protected virtual void AfterPropertyChanged(string propertyName)
		{
		}

		/// <summary>
		/// Attempts to raise the PropertyChanged event, and 
		/// invokes the virtual AfterPropertyChanged method, 
		/// regardless of whether the event was raised or not.
		/// </summary>
		/// <param name="propertyName">
		/// The property which was changed.
		/// </param>
		protected void RaisePropertyChanged(string propertyName)
		{
			this.VerifyProperty(propertyName);

			PropertyChangedEventHandler handler = this.PropertyChanged;
			if (handler != null)
			{
				// Get the cached event args.
				PropertyChangedEventArgs args = GetPropertyChangedEventArgs(propertyName);

				// Raise the PropertyChanged event.
				handler(this, args);
			}

			this.AfterPropertyChanged(propertyName);
		}

		#endregion // Protected Members

		#region Private Helpers

		[Conditional("DEBUG")]
		private void VerifyProperty(string propertyName)
		{
			Type type = this.GetType();

			// Look for a public property with the specified name.
			PropertyInfo propInfo = type.GetProperty(propertyName);

			if (propInfo == null)
			{
				// The property could not be found,
				// so alert the developer of the problem.

				string msg = string.Format( ERROR_MSG, propertyName, type.FullName);

				Debug.Fail(msg);
			}
		}

		#endregion // Private Helpers
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions