Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
3 May 2012CPOL12 min read 24.7K   464   9  
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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ProductRiaLib.Web.Services;
using ProductRiaLib.Web.Models;
using System.ComponentModel.Composition;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using ProductApp.Common;

namespace ProductApp.Views
{
    [Export(typeof(IModule)), ExportMetadata(MetadataKeys.Name, ModuleID.ProductListView)]
    public partial class ProductList : UserControl, IModule, ICleanup
    {
        private ModuleCatalogService _catalogService = ModuleCatalogService.Instance;
        private ChildWindow _addProdScreen;

        public ProductList()
        {
            InitializeComponent();

            // Register the massage handler for loading child window
            Messenger.Default.Register(this, MessageToken.LoadAddProductViewMessage,
                                       new Action<string>(OnLoadAddProductViewMessage));

            if (!ViewModelBase.IsInDesignModeStatic)
            {
                // Set the DataContext to the imported ViewModel
                DataContext = ModuleCatalogService.Instance.GetModule(ModuleID.ProductListViewModel);
            }
        }

        private void OnLoadAddProductViewMessage(string message)
        {
            // Load AddProductWindowView lazy module
            _addProdScreen = _catalogService.GetModuleLazy(ModuleID.AddProductWindowView) as ChildWindow;
            _addProdScreen.Show();
        }

        public void Cleanup()
        {
            if (_addProdScreen != null)
            {
                // Call Cleanup() in the child window if opened                
                ((ICleanup)_addProdScreen).Cleanup();

                // Remove the child window View Lazy module from Catelog Service
                ModuleCatalogService.Instance.ReleaseModuleLazy((IModule)_addProdScreen);

                _addProdScreen = null;
            }

            if (DataContext != null)
            {
                // Remove its imported ViewModel in the Category Service
                // The context.Dispose() will also call Cleanup() in released module
                // refrenced from MVVM Light ViewModelBase
                ModuleCatalogService.Instance.ReleaseModule((IModule)DataContext);

                DataContext = null;
            }

            // Clean up any messages this class registered
            Messenger.Default.Unregister(this);
        }
    }
}

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
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.

Comments and Discussions