Click here to Skip to main content
15,885,244 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 144.8K   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;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using IssueVision.Common;
using MVVMPlugin;

namespace IssueVision.Client
{
    public partial class MainPage : UserControl
    {
        #region "Private Data"
        private bool _noErrorMessage = true;
        private PluginCatalogService _catalogService = PluginCatalogService.Instance;
        #endregion "Private Data"

        #region "Constructor"
        public MainPage()
        {
            InitializeComponent();

            // register for ChangeScreenMessage
            AppMessages.ChangeScreenMessage.Register(this, OnChangeScreenMessage);
            // register for ChangeScreenNoAnimationMessage
            AppMessages.ChangeScreenNoAnimationMessage.Register(this, OnChangeScreenNoAnimationMessage);
            // register for RaiseErrorMessage
            AppMessages.RaiseErrorMessage.Register(this, OnRaiseErrorMessage);
            // register for PleaseConfirmMessage
            AppMessages.PleaseConfirmMessage.Register(this, OnPleaseConfirmMessage);
            // register for StatusUpdateMessage
            AppMessages.StatusUpdateMessage.Register(this, OnStatusUpdateMessage);

            if (!ViewModelBase.IsInDesignModeStatic)
            {
                // set DataContext
                DataContext = _catalogService.FindPlugin(
                    ViewModelTypes.MainPageViewModel, PluginType.ViewModel);
            }
        }
        #endregion "Constructor"

        #region "ChangeScreenMessage"

        private void OnChangeScreenMessage(string changeScreen)
        {
            object currentScreen;
            // call Cleanup() on the current screen before switching
            var cleanUp = mainPageContent.Content as ICleanup;
            if (cleanUp != null)
                cleanUp.Cleanup();

            // reset noErrorMessage
            _noErrorMessage = true;

            switch (changeScreen)
            {
                case ViewTypes.HomeView:
                    currentScreen = new Home();
                    break;
                case ViewTypes.NewIssueView:
                    currentScreen = _catalogService.FindPlugin(
                        ViewTypes.NewIssueView, PluginType.View);
                    break;
                case ViewTypes.AllIssuesView:
                    currentScreen = _catalogService.FindPlugin(
                        ViewTypes.AllIssuesView, PluginType.View);
                    break;
                case ViewTypes.MyIssuesView:
                    currentScreen = _catalogService.FindPlugin(
                        ViewTypes.MyIssuesView, PluginType.View);
                    break;
                case ViewTypes.BugReportView:
                    currentScreen = _catalogService.FindPlugin(
                        ViewTypes.BugReportView, PluginType.View);
                    break;
                case ViewTypes.MyProfileView:
                    currentScreen = _catalogService.FindPlugin(
                        ViewTypes.MyProfileView, PluginType.View);
                    break;
                case ViewTypes.UserMaintenanceView:
                    currentScreen = _catalogService.FindPlugin(
                        ViewTypes.UserMaintenanceView, PluginType.View);
                    break;
                case ViewTypes.AuditIssueView:
                    currentScreen = _catalogService.FindPlugin(
                        ViewTypes.AuditIssueView, PluginType.View);
                    break;
                default:
                    throw new NotImplementedException();
            }
            // change main page content with animation
            currentScreen = mainPageContent.ChangeMainPageContent(currentScreen, true);
            // call ReleasePlugin on replaced screen
            _catalogService.ReleasePlugin(currentScreen);
        }

        #endregion "ChangeScreenMessage"

        #region "ChangeScreenNoAnimationMessage"

        private void OnChangeScreenNoAnimationMessage(string changeScreen)
        {
            object currentScreen;
            // call Cleanup() on the current screen before switching
            var cleanUp = mainPageContent.Content as ICleanup;
            if (cleanUp != null)
                cleanUp.Cleanup();

            // reset noErrorMessage
            _noErrorMessage = true;

            switch (changeScreen)
            {
                case ViewTypes.HomeView:
                    currentScreen = new Home();
                    break;
                case ViewTypes.MyProfileView:
                    currentScreen = _catalogService.FindPlugin(ViewTypes.MyProfileView);
                    break;
                default:
                    throw new NotImplementedException();
            }
            // change main page content without animation
            currentScreen = mainPageContent.ChangeMainPageContent(currentScreen, false);
            // call ReleasePlugin on replaced screen
            _catalogService.ReleasePlugin(currentScreen);
        }

        #endregion "ChangeScreenNoAnimationMessage"

        #region "RaiseErrorMessage"

        private void OnRaiseErrorMessage(Exception ex)
        {
            if (_noErrorMessage && ex != null)
            {
                ErrorWindow.CreateNew(ex);
                // logout after display the error message
                hyperlinkButton_Logout.Command.Execute(true);
                _noErrorMessage = false;
            }
        }

        #endregion "RaiseErrorMessage"

        #region "PleaseConfirmMessage"

        private static void OnPleaseConfirmMessage(DialogMessage dialogMessage)
        {
            if (dialogMessage != null)
            {
                MessageBoxResult result = MessageBox.Show(dialogMessage.Content,
                    dialogMessage.Caption, dialogMessage.Button);

                dialogMessage.ProcessCallback(result);
            }
        }

        #endregion "PleaseConfirmMessage"

        #region "StatusUpdateMessage"

        private static void OnStatusUpdateMessage(DialogMessage dialogMessage)
        {
            if (dialogMessage != null)
            {
                MessageBoxResult result = MessageBox.Show(dialogMessage.Content,
                    dialogMessage.Caption, dialogMessage.Button);

                dialogMessage.ProcessCallback(result);
            }
        }

        #endregion "StatusUpdateMessage"
    }
}

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