Click here to Skip to main content
15,892,737 members
Articles / Desktop Programming / XAML

A Pluggable Architecture for Building Silverlight Applications with MVVM

Rate me:
Please Sign up or sign in to vote.
4.71/5 (23 votes)
6 Jul 2011CPOL7 min read 145.4K   2.2K   90  
This article describes building a sample Silverlight application with the MVVM Light toolkit, WCF RIA Services, and a pluggable application architecture using MEF.
using System.Windows;
using System.Windows.Controls;

namespace IssueVision.Common
{
    /// <summary>
    /// Custom control class with "LoggedIn" and "LoggedOut" states
    /// </summary>
    [TemplateVisualState(Name = "LoggedIn", GroupName = "CommonStates"),
    TemplateVisualState(Name = "LoggedOut", GroupName = "CommonStates")]
    public class MainPageControl : Control
    {
        public MainPageControl()
        {
            DefaultStyleKey = typeof(MainPageControl);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            UpdateVisualState(false);
        }

        #region "Dependency Properties"

        public object TitleContent
        {
            get { return GetValue(TitleContentProperty); }
            set { SetValue(TitleContentProperty, value); }
        }

        public static readonly DependencyProperty TitleContentProperty =
            DependencyProperty.Register("TitleContent",
                                        typeof(object),
                                        typeof(MainPageControl), null);

        public object LoggedInMenuContent
        {
            get { return GetValue(LoggedInMenuContentProperty); }
            set { SetValue(LoggedInMenuContentProperty, value); }
        }

        public static readonly DependencyProperty LoggedInMenuContentProperty =
            DependencyProperty.Register("LoggedInMenuContent",
                                        typeof(object),
                                        typeof(MainPageControl), null);

        public object LoggedOutMenuContent
        {
            get { return GetValue(LoggedOutMenuContentProperty); }
            set { SetValue(LoggedOutMenuContentProperty, value); }
        }

        public static readonly DependencyProperty LoggedOutMenuContentProperty =
            DependencyProperty.Register("LoggedOutMenuContent",
                                        typeof(object),
                                        typeof(MainPageControl), null);

        public object LoginPageContent
        {
            get { return GetValue(LoginPageContentProperty); }
            set { SetValue(LoginPageContentProperty, value); }
        }

        public static readonly DependencyProperty LoginPageContentProperty =
            DependencyProperty.Register("LoginPageContent",
                                        typeof(object),
                                        typeof(MainPageControl), null);

        public object MainPageContent
        {
            get { return GetValue(MainPageContentProperty); }
            set { SetValue(MainPageContentProperty, value); }
        }

        public static readonly DependencyProperty MainPageContentProperty =
            DependencyProperty.Register("MainPageContent",
                                        typeof(object),
                                        typeof(MainPageControl), null);

        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius",
                                        typeof(CornerRadius),
                                        typeof(MainPageControl), null);

        public bool IsLoggedIn
        {
            get { return (bool)GetValue(IsLoggedInProperty); }
            set { SetValue(IsLoggedInProperty, value); }
        }

        public static readonly DependencyProperty IsLoggedInProperty =
            DependencyProperty.Register("IsLoggedIn",
                                        typeof(bool),
                                        typeof(MainPageControl),
                                        new PropertyMetadata(false, OnIsLoggedInChanged));

        private static void OnIsLoggedInChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var mainPageControl = sender as MainPageControl;

            if (mainPageControl != null)
            {
                mainPageControl.UpdateVisualState(true);
            }
        }

        #endregion "Dependency Properties"

        #region "Internal Method"
        internal void UpdateVisualState(bool useTransitions)
        {
            VisualStateManager.GoToState(this, IsLoggedIn ? "LoggedIn" : "LoggedOut", useTransitions);
        }

        #endregion "Internal Method"
    }
}

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 (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

Comments and Discussions