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

namespace IssueVision.Client
{
    public partial class IssueEditor : UserControl, ICleanup
    {
        #region "Private Data Members"
        private Lazy<ViewModelBase> _viewModelExport;
        #endregion "Private Data Members"

        #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)
            {
                // Use MEF To load the View Model
                _viewModelExport = App.Container.GetExport<ViewModelBase>(
                    ViewModelTypes.IssueEditorViewModel);
                if (_viewModelExport != null) DataContext = _viewModelExport.Value;
            }
        }
        #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);
            // set DataContext to null and call ReleaseExport()
            DataContext = null;
            App.Container.ReleaseExport(_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