Click here to Skip to main content
15,891,951 members
Articles / Mobile Apps / Windows Phone 7

Implementing Wizard Functionality for Windows Phone 7 Applications

Rate me:
Please Sign up or sign in to vote.
4.88/5 (12 votes)
5 Aug 2011CPOL23 min read 38.7K   683   17  
This article presents how to build a wizard for WP7 applications.
using System;
using System.ComponentModel;
using System.Reflection;
using System.Diagnostics;

namespace WizardLibrary
{
    public sealed class WizardStep:IValidableContent, INotifyPropertyChanged
    {
        #region fields
        private string title;
        private bool allowReturn;
        private Wizard wizard;
        private object content;
        private bool allowFinish;
        #endregion

        public WizardStep()
        {
            AllowReturn = true;
        }

        #region inpc implementation
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        #endregion

        #region properties
        public string Title
        {
            get { return title; }
            set 
            {
                if (title == value)
                    return;
                title = value;
                NotifyPropertyChanged("Title");
            }
        }       

        public bool AllowReturn
        {
            get { return allowReturn; }
            set 
            {
                if (allowReturn == value)
                    return;
                allowReturn = value;
                NotifyPropertyChanged("AllowReturn");
            }
        }
        public bool AllowFinish
        {
            get { return allowFinish; }
            set
            {
                if (allowFinish == value)
                    return;
                allowFinish = value;
                NotifyPropertyChanged("AllowFinish");
            }
        }    

        public Wizard Wizard
        {
            get { return wizard; }
            set 
            {
                if (wizard == value)
                    return;
                wizard = value;
                NotifyPropertyChanged("Wizard");
            }
        }

        public object Content
        {
            get { return content; }
            set
            {
                if (content == value)
                    return;
                //unsubscribe the old content
                unsubscribe(content);
                //set the new content
                content = value;
                //subscribe the new content
                subscribe(content);
                NotifyPropertyChanged("Content");
                //also invalidate the commands
                if (wizard != null)
                    wizard.Invalidate();
            }
        }
        #endregion

        public bool IsValid()
        {
            IValidableContent v = content as IValidableContent;
            if (v != null)
                return v.IsValid();
            return true;
        }

        #region private helpers
        private void ContentChanged(object sender, PropertyChangedEventArgs e)
        {
            //when any property changes invalidate the wizard commands
            if (wizard != null)
                Wizard.Invalidate();
        }
        private void subscribe(object obj)
        {
            INotifyPropertyChanged c = obj as INotifyPropertyChanged;
            if (c != null)
                c.PropertyChanged += ContentChanged;
            else
                return;
            Debug.WriteLine("subscribed " + obj);
            Type type = obj.GetType();
            //get all the public properties
            PropertyInfo[] pis = type.GetProperties();
            //iterate over them and call subscribe()
            for (int i = 0; i < pis.Length; i++)
            {
                object val = pis[i].GetValue(obj, null);
                //call subscribe() recursively even if it does not 
                //implement inpc. this will be checked anyway the next time
                //subscribe() is called
                subscribe(val);
            }
        }
        private void unsubscribe(object obj)
        {
            INotifyPropertyChanged c = obj as INotifyPropertyChanged;
            if (c != null)
                c.PropertyChanged -= ContentChanged;
            else
                return;
            Debug.WriteLine("unsubscribed "+ obj);
            Type type = obj.GetType();
            //get all the public properties
            PropertyInfo[] pis = type.GetProperties();
            //iterate over them and call unsubscribe()
            for (int i = 0; i < pis.Length; i++)
            {
                object val = pis[i].GetValue(obj, null);
                //call unsubscribe() recursively even if it does not 
                //implement inpc. this will be checked anyway the next time
                //unsubscribe() is called
                unsubscribe(val);
            }
        }
        #endregion

        #region destructor
        ~WizardStep()
        {
            unsubscribe(content);
        }
        #endregion
    }
}

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
Software Developer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions