Click here to Skip to main content
15,879,096 members
Articles / Desktop Programming / XAML

A Sample Silverlight 4 Application Using MEF, MVVM, and WCF RIA Services - Part 1

Rate me:
Please Sign up or sign in to vote.
4.84/5 (108 votes)
7 Jul 2011CPOL9 min read 2.1M   30.9K   298  
Part 1 of a series describing the creation of a Silverlight business application using MEF, MVVM Light, and WCF RIA Services.
using System;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel.Composition;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using IssueVision.Common;

namespace IssueVision.Client
{
    public partial class NewIssue : UserControl, ICleanup
    {
        #region "Private Data Members"
        private const double MinimumWidth = 640;
        private Lazy<ViewModelBase> _viewModelExport;
        #endregion "Private Data Members"

        #region "Constructor"
        public NewIssue()
        {
            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)
            {
                // Use MEF To load the View Model
                _viewModelExport = App.Container.GetExport<ViewModelBase>(
                    ViewModelTypes.NewIssueViewModel);
                this.DataContext = _viewModelExport.Value;
            }
        }
        #endregion "Constructor"

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

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

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            App.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 (App.Current.Host.Content.ActualWidth < MinimumWidth)
                this.Width = MinimumWidth;
            else
                this.Width = App.Current.Host.Content.ActualWidth;
            // stretch the UserControl to the current brower Height - Title Height(42) - Menu Height(22)
            this.Height = App.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);
            // set DataContext to null and call ReleaseExport()
            this.DataContext = null;
            App.Container.ReleaseExport<ViewModelBase>(_viewModelExport);
            _viewModelExport = 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