Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version
Go to top

Migrate from Basic to MVVM and MEF Composable Patterns for a Silverlight Application - Part 3

, 3 May 2012
The article series shows how to upgrade a Silverlight application having basic patterns to the MVVM and MEF composable patterns with easy approaches and detailed coding explanations.
MigrateToMvvmMef-Part1_Src.zip
MigrateToMvvmMef-Part1_Src
ProductApp.Common
Bin
Release
Constants
ModuleServices
obj
ProductApp.Common.csproj.user
Properties
ProductApp.Main
Assets
Bin
Release
obj
ProductApp.Main.csproj.user
Properties
Views
ProductApp.suo
ProductApp.Views
Assets
Bin
Release
obj
ProductApp.Views.csproj.user
Properties
ProductApp.Web
bin
ClientBin
obj
ProductApp.Web.csproj.user
Properties
ProductRiaLib
ProductRiaLib.Web
App_Data
bin
Debug
EntityFramework.dll
Microsoft.ServiceModel.DomainServices.EntityFramework.dll
Release
Models
obj
ProductRiaLib.Web.csproj.user
Properties
Services
Bin
Release
obj
ProductRiaLib.csproj.user
Properties
MigrateToMvvmMef-Part2_Src.zip
MigrateToMvvmMef-Part2_Src
ProductApp.Common
Bin
Release
Constants
EventArguments
Models
ModuleServices
obj
ProductApp.Common.csproj.user
Properties
ProductApp.Main
Assets
Bin
Release
obj
ProductApp.Main.csproj.user
Properties
ViewModels
Views
ProductApp.Models
Bin
Release
obj
ProductApp.Models.csproj.user
Properties
ProductApp.suo
ProductApp.ViewModels
Bin
Release
obj
ProductApp.ViewModels.csproj.user
Properties
ProductApp.Views
Assets
Bin
Release
obj
ProductApp.Views.csproj.user
Properties
ProductApp.Web
bin
ClientBin
obj
ProductApp.Web.csproj.user
Properties
ProductRiaLib
ProductRiaLib.Web
App_Data
bin
Debug
EntityFramework.dll
Microsoft.ServiceModel.DomainServices.EntityFramework.dll
Release
Models
obj
ProductRiaLib.Web.csproj.user
Properties
Services
Bin
Release
obj
ProductRiaLib.csproj.user
Properties
_Assemblies
GalaSoft.MvvmLight.SL5.dll
System.Windows.Interactivity.dll
MigrateToMvvmMef-Part3_Src.zip
MigrateToMvvmMef-Part3_Src
AnotherXap.Models
AnotherXap.Models.csproj.user
Bin
Release
obj
Properties
AnotherXap.ViewModels
AnotherXap.ViewModels.csproj.user
Bin
Release
obj
Properties
AnotherXap.Views
AnotherXap.Views.csproj.user
Assets
Bin
Release
obj
Properties
ProductApp.Common
Bin
Release
Constants
EventArguments
Models
ModuleServices
obj
ProductApp.Common.csproj.user
Properties
SaveState
ProductApp.Main
Assets
Bin
Release
Models
obj
ProductApp.Main.csproj.user
Properties
ViewModels
Views
ProductApp.Models
Bin
Release
obj
ProductApp.Models.csproj.user
Properties
ProductApp.suo
ProductApp.ViewModels
Bin
Release
obj
ProductApp.ViewModels.csproj.user
Properties
ProductApp.Views
Assets
Bin
Release
obj
ProductApp.Views.csproj.user
Properties
ProductApp.Web
bin
ClientBin
obj
ProductApp.Web.csproj.user
Properties
ProductRiaLib
ProductRiaLib.Web
App_Data
bin
Debug
EntityFramework.dll
Microsoft.ServiceModel.DomainServices.EntityFramework.dll
Release
Models
obj
ProductRiaLib.Web.csproj.user
Properties
Services
Bin
Release
obj
ProductRiaLib.csproj.user
Properties
_Assemblies
GalaSoft.MvvmLight.SL5.dll
System.Windows.Interactivity.dll
using System;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using ProductApp.Common;

namespace ProductApp.Main.Views
{
    public partial class MainPage : UserControl
    {
        private ModuleCatalogService _catalogService = ModuleCatalogService.Instance;

        public MainPage()
        {
            InitializeComponent();

            // Register MVVMLight messages
            Messenger.Default.Register(this, MessageToken.LoadScreenMessage, new Action<string>(OnLoadScreenMessage));
            Messenger.Default.Register(this, MessageToken.RaiseErrorMessage, new Action<Exception>(OnRaiseErrorMessage));
            Messenger.Default.Register(this, MessageToken.UseDialogMessage, new Action<DialogMessage>(OnUseDialogMessage));

            if (!ViewModelBase.IsInDesignModeStatic)
            {
                // Import the ViewModel module into the View DataContext so that 
                // the members of the ViewModel can be exposed
                DataContext = _catalogService.GetModule(ModuleID.MainPageViewModel);
            }
        }

        // Method to be executed when receiving the message
        private void OnLoadScreenMessage(string moduleId)        {
            
            object newScreen;
            try
            {
                // Import selected View module and then
                // set the commandArg for UI changes
                switch (moduleId)
                {
                    case ModuleID.ProductListView:
                        newScreen = _catalogService.GetModule(ModuleID.ProductListView);                        
                        break; 
                        // Add for other modules here
                    default:
                        throw new NotImplementedException();
                }

                // Set the new screen
                MainContent.Content = newScreen;

                // UI - Set link button state
                SetLinkButtonState(moduleId);
            }
            catch (Exception ex)
            {
                OnRaiseErrorMessage(ex);
            }
        }

        // UI
        private void SetLinkButtonState(string buttonArg)
        {
            foreach (UIElement child in LinksStackPanel.Children)
            {
                HyperlinkButton hb = child as HyperlinkButton;
                if (hb != null && hb.Command != null)
                {
                    if (hb.CommandParameter.ToString().Equals(buttonArg))
                    {
                        VisualStateManager.GoToState(hb, "ActiveLink", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(hb, "InactiveLink", true);
                    }
                }
            }
        }

        private void OnRaiseErrorMessage(Exception ex)
        {
            // Error message display
            ChildWindow errorWin = new ErrorWindow(ex.Message, ex.StackTrace);
            errorWin.Show();
        }

        private void OnUseDialogMessage(DialogMessage dialogMessage)
        {
            // MVVMLight DialogMessage callback processes
            if (dialogMessage != null)
            {
                MessageBoxResult result = MessageBox.Show(dialogMessage.Content,
                    dialogMessage.Caption, dialogMessage.Button);
                dialogMessage.ProcessCallback(result);
            }
        }
    }
}

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)

Share

About the Author

Shenwei Liu

United States United States
Shenwei is a software developer and architect, and has been working on business applications using Microsoft and Oracle technologies since 1996. He obtained Microsoft Certified Systems Engineer (MCSE) in 1998 and Microsoft Certified Solution Developer (MCSD) in 1999. He has experience in ASP.NET, C#, Visual Basic, Windows and Web Services, Silverlight, WPF, JavaScript/AJAX, HTML, SQL Server, and Oracle.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 3 May 2012
Article Copyright 2012 by Shenwei Liu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid