Click here to Skip to main content
15,896,269 members
Articles / Programming Languages / C#

Using Smart cards with a Windows Store App

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
10 Feb 2013CPOL8 min read 32.2K   1.7K   18  
This article describes a solution to access API and resources that are not available with WinRT.
// 
//  This code has been extracted from the following resource
//  https://mytoolkit.svn.codeplex.com/svn/Shared/
//  The conditional define WINRT has been replaced with NETFX_CORE which
//  is defined by default when creating a WINRT library
//
//  If you take it for your own usage, please refer to the original copyright      
//

using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Runtime.Serialization;

#if NETFX_CORE
using Core.Extension;
using Windows.UI.Xaml.Data; 
using System.Reflection;
using System.ComponentModel;
using System.Runtime.CompilerServices;
#else
using System.ComponentModel;
#endif

namespace Core.Mvvm
{
    [DataContract]
    public class NotifyPropertyChanged<TClass> : NotifyPropertyChanged
    {
        public bool SetProperty<T>(Expression<Func<TClass, T>> expression, ref T oldValue, T newValue)
        {
            return SetProperty(((MemberExpression)expression.Body).Member.Name, ref oldValue, newValue);
        }

        public void RaisePropertyChanged<T>(Expression<Func<TClass, T>> expression)
        {
            RaisePropertyChanged(((MemberExpression)expression.Body).Member.Name);
        }

        public void SetDependency<TProp1, TProp2>(Expression<Func<TClass, TProp1>> propertyName,
            Expression<Func<TClass, TProp2>> dependentPropertyName)
        {
            SetDependency(((MemberExpression)propertyName.Body).Member.Name,
                ((MemberExpression)dependentPropertyName.Body).Member.Name);
        }
    }

    [DataContract]
    public class NotifyPropertyChanged : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private List<KeyValuePair<string, string>> dependencies;

        public bool SetProperty<TClass, TProp>(Expression<Func<TClass, TProp>> expression, ref TProp oldValue, TProp newValue)
        {
            return SetProperty(((MemberExpression)expression.Body).Member.Name, ref oldValue, newValue);
        }

        public bool SetProperty<T>(String propertyName, ref T oldValue, T newValue)
        {
            if (Equals(oldValue, newValue))
                return false;

            oldValue = newValue;
            RaisePropertyChanged(propertyName);
            return true;
        }

        public void SetDependency(string propertyName, string dependentPropertyName)
        {
            if (dependencies == null)
                dependencies = new List<KeyValuePair<string, string>>();

            if (dependencies.Any(d => d.Key == propertyName && d.Value == dependentPropertyName))
                return;

            dependencies.Add(new KeyValuePair<string, string>(propertyName, dependentPropertyName));
        }

        public void SetDependency<TClass, TProp1, TProp2>(Expression<Func<TClass, TProp1>> propertyName,
            Expression<Func<TClass, TProp2>> dependentPropertyName)
        {
            SetDependency(((MemberExpression)propertyName.Body).Member.Name,
                ((MemberExpression)dependentPropertyName.Body).Member.Name);
        }

        public void RaisePropertyChanged<TClass, TProp>(Expression<Func<TClass, TProp>> expression)
        {
            RaisePropertyChanged(((MemberExpression)expression.Body).Member.Name);
        }

#if NETFX_CORE
        public bool SetProperty<T>(ref T oldValue, T newValue, [CallerMemberName] String propertyName = null)
		{
			return SetProperty(propertyName, ref oldValue, newValue); 
		}

		public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
#else
        public void RaisePropertyChanged(string propertyName = null)
#endif
        {
#if DEBUG
#if NETFX_CORE
            if (GetType().GetTypeInfo().GetProperty(propertyName) == null)
						throw new ArgumentException("propertyName");
#else
            if (GetType().GetProperty(propertyName) == null)
                throw new ArgumentException("propertyName");
#endif
#endif

            var copy = PropertyChanged; // avoid concurrent changes
            if (copy != null)
                copy(this, new PropertyChangedEventArgs(propertyName));

            if (dependencies != null)
            {
                foreach (var d in dependencies.Where(d => d.Key == propertyName))
                    RaisePropertyChanged(d.Value);
            }
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Architect Connect In Private
Singapore Singapore
Software Architect, COM, .NET and Smartcard based security specialist.

I've been working in the software industry since I graduated in Electrical and Electronics Engineering. I chose software because I preferred digital to analog.

I started to program with 6802 machine code and evolved to the current .NET technologies... that was a long way.

For more than 20 years I have always worked in technical positions as I simply like to get my hands dirty and crack my brain when things don't go right!

After 12 years in the smart card industry I can claim a strong knowledge in security solutions based on those really small computers!
I've been back into business to design the licensing system for the enterprise solution for Consistel using a .NET smart card (yes they can run .NET CLR!)

I'm currently designing a micro-payment solution using the NXP DESFire EV1 with the ACSO6 SAM of ACS. I can then add a full proficient expertise on those systems and NFC payments.
This technology being under strict NDA by NXP I cannot publish any related article about it, however I can provide professional consulting for it.

You can contact me for professional matter by using the forum or via my LinkedIn profile.

Comments and Discussions