Click here to Skip to main content
15,888,181 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 145K   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
{
    [ExportPlugin(ViewTypes.MyIssuesView, PluginType.View)]
    public partial class MyIssues : UserControl, ICleanup
    {
        #region "Private Data Members"
        private const double MinimumWidth = 640;
        #endregion "Private Data Members"

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

            // register any AppMessages here

            if (!ViewModelBase.IsInDesignModeStatic)
            {
                // set DataContext
                DataContext = PluginCatalogService.Instance.FindPlugin(
                    ViewModelTypes.MyIssuesViewModel, 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 += Content_Resized;
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.Resized -= 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)
                Width = MinimumWidth;
            else
                Width = Application.Current.Host.Content.ActualWidth;
            // stretch the UserControl to the current brower Height - Title Height(42) - Menu Height(22)
            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)DataContext).Cleanup();
            // call Cleanup on IssueEditor
            var issueEditor = issueEditorContentControl.Content as ICleanup;
            if (issueEditor != null)
                issueEditor.Cleanup();
            issueEditorContentControl.Content = null;
            // cleanup itself
            Messenger.Default.Unregister(this);
            // call ReleasePlugin on its ViewModel
            PluginCatalogService.Instance.ReleasePlugin(DataContext);
            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