Click here to Skip to main content
15,891,938 members
Articles / Desktop Programming / WPF

Perceptor: An artificially intelligent guided navigation system for WPF

Rate me:
Please Sign up or sign in to vote.
4.95/5 (126 votes)
22 Mar 2009LGPL312 min read 177.3K   1.6K   217  
Knowledge acquired by a neural network is used to predict the element to which a user may intend to navigate.
#region File and License Information
/*
<File>
	<Copyright>Copyright © 2007, Daniel Vaughan. All rights reserved.</Copyright>
	<License see="prj:///Documentation/License.txt"/>
	<Owner Name="Daniel Vaughan" Email="dbvaughan@gmail.com"/>
	<CreationDate>2009-02-08 18:10:38Z</CreationDate>
	<LastSubmissionDate>$Date: $</LastSubmissionDate>
	<Version>$Revision: $</Version>
</File>
*/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;

namespace DanielVaughan.ComponentModel
{
	/// <summary>
	/// This class provides an implementation of the <see cref="INotifyPropertyChanged"/>
	/// interface.
	/// </summary>
	public sealed class PropertyChangedNotifier : INotifyPropertyChanged
	{
		readonly object owner;

		/// <summary>
		/// Initializes a new instance of the <see cref="PropertyChangedNotifier"/> class.
		/// </summary>
		/// <param name="owner">The intended sender of the <code>PropertyChanged</code> event.</param>
		public PropertyChangedNotifier(object owner)
		{
			ArgumentValidator.AssertNotNull(owner, "owner");
			this.owner = owner;
		}

		#region event PropertyChanged

		event PropertyChangedEventHandler propertyChanged;

		/// <summary>
		/// Occurs when a property value changes.
		/// </summary>
		public event PropertyChangedEventHandler PropertyChanged
		{
			add
			{
				propertyChanged += value;
			}
			remove
			{
				propertyChanged -= value;
			}
		}

		void OnPropertyChanged(PropertyChangedEventArgs e)
		{
			if (propertyChanged != null)
			{
				propertyChanged(owner, e);
			}
		}

		public void OnPropertyChanged(string propertyName)
		{
			ArgumentValidator.AssertNotNullOrEmpty(propertyName, "propertyName");
			OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
		}

		#endregion

		readonly Dictionary<string, string> expressions = new Dictionary<string, string>();
		readonly ReaderWriterLockSlim expressionsLock = new ReaderWriterLockSlim();

		/* Slow. Not recommended. */
		public void OnPropertyChanged<T, TResult>(Expression<Func<T, TResult>> expression)
		{
			ArgumentValidator.AssertNotNull(expression, "expression");

			string name;
			expressionsLock.EnterUpgradeableReadLock();
			try
			{
				if (!expressions.TryGetValue(expression.ToString(), out name))
				{
					expressionsLock.EnterWriteLock();
					try
					{
						if (!expressions.TryGetValue(expression.ToString(), out name))
						{
							var memberInfo = GetMemberInfo(expression);
							if (memberInfo == null)
							{
								throw new InvalidOperationException("MemberInfo not found."); /* TODO: Make localizable resource. */
							}
							name = memberInfo.Name;
							expressions.Add(expression.ToString(), name);
						}
					}
					finally
					{
						expressionsLock.ExitWriteLock();
					}
				}
			}
			finally
			{
				expressionsLock.ExitUpgradeableReadLock();
			}
			OnPropertyChanged(name);
		}

		static MemberInfo GetMemberInfo<T, TResult>(Expression<Func<T, TResult>> expression)
		{
			var member = expression.Body as MemberExpression;
			if (member != null)
			{
				return member.Member;
			}

			throw new ArgumentException("MemberExpression expected.", "expression"); /* TODO: Make localizable resource. */
		}
	}
}

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 Lesser General Public License (LGPLv3)


Written By
Engineer
Switzerland Switzerland
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Comments and Discussions