Click here to Skip to main content
15,895,084 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 MainPage : UserControl
    {
        #region "Private Data Members"
        private bool noErrorMessage = true;
        #endregion "Private Data Members"

        #region "Constructor"
        public MainPage()
        {
            InitializeComponent();

            if (!ViewModelBase.IsInDesignModeStatic)
            {
                // Use MEF To load the View Model
                this.DataContext = App.Container.GetExportedValue<ViewModelBase>(
                    ViewModelTypes.MainPageViewModel);
            }

            // register for ChangeScreenMessage
            AppMessages.ChangeScreenMessage.Register(this, OnChangeScreenMessage);
            // register for RaiseErrorMessage
            AppMessages.RaiseErrorMessage.Register(this, OnRaiseErrorMessage);
            // register for PleaseConfirmMessage
            AppMessages.PleaseConfirmMessage.Register(this, OnPleaseConfirmMessage);
            // register for StatusUpdateMessage
            AppMessages.StatusUpdateMessage.Register(this, OnStatusUpdateMessage);
        }
        #endregion "Constructor"

        #region "ChangeScreenMessage"

        private void OnChangeScreenMessage(string changeScreen)
        {
            // call Cleanup() on the current screen before switching
            ICleanup currentScreen = this.mainPageContent.Content as ICleanup;
            if (currentScreen != null)
                currentScreen.Cleanup();

            // reset noErrorMessage
            this.noErrorMessage = true;

            switch (changeScreen)
            {
                case ViewTypes.HomeView:
                    this.mainPageContent.Content = new Home();
                    break;
                case ViewTypes.NewIssueView:
                    this.mainPageContent.Content = new NewIssue();
                    break;
                case ViewTypes.AllIssuesView:
                    this.mainPageContent.Content = new AllIssues();
                    break;
                case ViewTypes.MyIssuesView:
                    this.mainPageContent.Content = new MyIssues();
                    break;
                case ViewTypes.BugReportView:
                    this.mainPageContent.Content = new Reports();
                    break;
                case ViewTypes.MyProfileView:
                    this.mainPageContent.Content = new MyProfile();
                    break;
                case ViewTypes.UserMaintenanceView:
                    this.mainPageContent.Content = new UserMaintenance();
                    break;
                default:
                    throw new NotImplementedException();
            }
        }

        #endregion "ChangeScreenMessage"

        #region "RaiseErrorMessage"

        private void OnRaiseErrorMessage(Exception ex)
        {
            if (this.noErrorMessage && ex != null)
            {
                ErrorWindow.CreateNew(ex);
                // logout after display the error message
                this.hyperlinkButton_Logout.Command.Execute(true);
                this.noErrorMessage = false;
            }
        }

        #endregion "RaiseErrorMessage"

        #region "PleaseConfirmMessage"

        private void OnPleaseConfirmMessage(DialogMessage dialogMessage)
        {
            if (dialogMessage != null)
            {
                MessageBoxResult result = MessageBox.Show(dialogMessage.Content,
                    dialogMessage.Caption, dialogMessage.Button);

                dialogMessage.ProcessCallback(result);
            }
        }

        #endregion "PleaseConfirmMessage"

        #region "StatusUpdateMessage"

        private void OnStatusUpdateMessage(DialogMessage dialogMessage)
        {
            if (dialogMessage != null)
            {
                MessageBoxResult result = MessageBox.Show(dialogMessage.Content,
                    dialogMessage.Caption, dialogMessage.Button);

                dialogMessage.ProcessCallback(result);
            }
        }

        #endregion "StatusUpdateMessage"
    }
}

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