Click here to Skip to main content
15,895,799 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 145.7K   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.IO;
using System.Windows.Controls;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using IssueVision.Common;
using MVVMPlugin;

namespace IssueVision.Client
{
    public partial class IssueEditor : UserControl, ICleanup
    {
        #region "Constructor"
        public IssueEditor()
        {
            InitializeComponent();

            // register for ReadOnlyIssueMessage
            AppMessages.ReadOnlyIssueMessage.Register(this, OnReadOnlyIssueMessage);
            // register for OpenFileMessage
            AppMessages.OpenFileMessage.Register(this, OnOpenFileMessage);
            // register for SaveFileMessage
            AppMessages.SaveFileMessage.Register(this, OnSaveFileMessage);

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

        #region "ReadOnlyIssueMessage"

        private void OnReadOnlyIssueMessage(Boolean readOnly)
        {
            if (readOnly)
            {
                // prepare the screen for read-only issue
                textBox_IssueTitle.IsReadOnly = true;
                comboBox_Type.IsEnabled = false;
                comboBox_Priority.IsEnabled = false;
                comboBox_Severity.IsEnabled = false;
                comboBox_Status.IsEnabled = false;
                comboBox_Substatus.IsEnabled = false;
                dataGrid_Platform.IsEnabled = false;
                comboBox_AssignedTo.IsEnabled = false;
                comboBox_Resolution.IsEnabled = false;
                textBox_Description.IsReadOnly = true;
                textBox_ReproSteps.IsReadOnly = true;
                listBox_Files.IsEnabled = false;
                button_Add.IsEnabled = false;
                button_Remove.IsEnabled = false;
                button_Save.IsEnabled = false;
                dataGrid_Attributes.IsEnabled = false;
                button_AddAttribute.IsEnabled = false;
                button_RemoveAttribute.IsEnabled = false;
            }
            else
            {
                // prepare the screen for read/update issue
                textBox_IssueTitle.IsReadOnly = false;
                comboBox_Type.IsEnabled = true;
                comboBox_Priority.IsEnabled = true;
                comboBox_Severity.IsEnabled = true;
                comboBox_Status.IsEnabled = true;
                comboBox_Substatus.IsEnabled = true;
                dataGrid_Platform.IsEnabled = true;
                comboBox_AssignedTo.IsEnabled = true;
                comboBox_Resolution.IsEnabled = true;
                textBox_Description.IsReadOnly = false;
                textBox_ReproSteps.IsReadOnly = false;
                listBox_Files.IsEnabled = true;
                button_Add.IsEnabled = true;
                button_Remove.IsEnabled = true;
                button_Save.IsEnabled = true;
                dataGrid_Attributes.IsEnabled = true;
                button_AddAttribute.IsEnabled = true;
                button_RemoveAttribute.IsEnabled = true;
            }
        }

        #endregion "ReadOnlyIssueMessage"

        #region "OpenFileMessage"

        private static void OnOpenFileMessage(NotificationMessageAction<FileInfo> message)
        {
            try
            {
                var ofDialog = new OpenFileDialog
                {
                    Filter = "All Files (*.*)|*.*",
                    Multiselect = false
                };

                // show the dialog box
                if (ofDialog.ShowDialog().GetValueOrDefault(false))
                {
                    // send the file selected back
                    message.Execute(ofDialog.File);
                }

            }
            catch (Exception ex)
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(ex);
            }
        }

        #endregion "OpenFileMessage"

        #region "SaveFileMessage"

        private static void OnSaveFileMessage(Data.Web.File saveFile)
        {
            try
            {
                var sfDialog = new SaveFileDialog
                {
                    Filter = "All Files (*.*)|*.*",
                    DefaultExt =
                        saveFile.FileName.Contains(".")
                            ? saveFile.FileName.Substring(
                                saveFile.FileName.LastIndexOf('.'))
                            : saveFile.FileName
                };

                // show the dialog box
                if (sfDialog.ShowDialog().GetValueOrDefault(false))
                {
                    using (Stream stream = sfDialog.OpenFile())
                    {
                        stream.Write(saveFile.Data, 0, saveFile.Data.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(ex);
            }
        }

        #endregion "SaveFileMessage"

        #region "ICleanup interface implementation"

        public void Cleanup()
        {
            // call Cleanup on its ViewModel
            ((ICleanup)DataContext).Cleanup();
            // 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