Click here to Skip to main content
15,878,945 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.6K   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 System.ComponentModel.Composition;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using IssueVision.Common;
using MVVMPlugin;

namespace IssueVision.Client
{
    [ExportPlugin(ViewTypes.AllIssuesView, PluginType.View)]
    public partial class AllIssues : UserControl, ICleanup
    {
        #region "Private Data Members"
        private const double MinimumWidth = 640;
        #endregion "Private Data Members"

        #region "Constructor"
        public AllIssues()
        {
            InitializeComponent();
            // add the IssueEditor
            issueEditorContentControl.Content = new IssueEditor();
            // initialize the UserControl Width & Height
            this.Content_Resized(this, null);

            // register any AppMessages here

            if (!ViewModelBase.IsInDesignModeStatic)
            {
                // set DataContext
                this.DataContext = PluginCatalogService.Instance.FindPlugin(
                    ViewModelTypes.AllIssuesViewModel, PluginType.ViewModel);
            }
        }
        #endregion "Constructor"

        #region "Logic to dynamically set UserControl Width and Height"

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.Resized -= new EventHandler(Content_Resized);
        }

        private void Content_Resized(object sender, EventArgs e)
        {
            // stretch the UserControl to the current browser width if wider than MinimumWidth
            if (Application.Current.Host.Content.ActualWidth < MinimumWidth)
                this.Width = MinimumWidth;
            else
                this.Width = Application.Current.Host.Content.ActualWidth;
            // stretch the UserControl to the current brower Height - Title Height(42) - Menu Height(22)
            this.Height = Application.Current.Host.Content.ActualHeight - 42 - 22;
        }

        #endregion "Logic to dynamically set UserControl Width and Height"

        #region "ICleanup interface implementation"

        public void Cleanup()
        {
            // call Cleanup on its ViewModel
            ((ICleanup)this.DataContext).Cleanup();
            // call Cleanup on IssueEditor
            ICleanup issueEditor = this.issueEditorContentControl.Content as ICleanup;
            if (issueEditor != null)
                issueEditor.Cleanup();
            this.issueEditorContentControl.Content = null;
            // cleanup itself
            Messenger.Default.Unregister(this);
            // call ReleasePlugin on its ViewModel
            PluginCatalogService.Instance.ReleasePlugin(this.DataContext);
            this.DataContext = null;
        }

        #endregion "ICleanup interface implementation"
    }
}

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