Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

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

, 7 Jul 2011
Part 1 of a series describing the creation of a Silverlight business application using MEF, MVVM Light, and WCF RIA Services.
IssueVisionforSilverlightSetup20110630.zip
IssueVision for Silverlight Setup
IssueVisionSetup.msi
setup.exe
IssueVision_for_Silverlight_setup.zip
IssueVisionSetup.msi
setup.exe
IssueVision_Silverlight20110630.zip
IssueVision.Silverlight
Assemblies
GalaSoft.MvvmLight.Extras.SL4.dll
GalaSoft.MvvmLight.SL4.dll
MenuControls.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
System.Windows.Controls.Toolkit.dll
System.Windows.Controls.Toolkit.Internals.dll
System.Windows.Interactivity.dll
Database
IssueVision.Client
IssueVision.Client.csproj.user
Assets
Signin.png
Controls
Properties
Views
IssueVision.Common
IssueVision.Common.csproj.user
Controls
Helpers
Models
Properties
Resources
Themes
IssueVision.Data
IssueVision.Data.csproj.user
Properties
Web
Resources
IssueVision.Data.Web
IssueVision.edmx
Helpers
MetadataClasses
Properties
Resources
Services
IssueVision.Model
IssueVision.Model.csproj.user
Properties
IssueVision.Setup
IssueVisionSetup.vdproj
IssueVision.ViewModel
IssueVision.ViewModel.csproj.user
Properties
IssueVision.Web
IssueVision.Web.csproj.user
Properties
IssueVision_Silverlight_20110316.zip
GalaSoft.MvvmLight.Extras.SL4.dll
GalaSoft.MvvmLight.SL4.dll
MenuControls.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
System.Windows.Controls.Toolkit.dll
System.Windows.Controls.Toolkit.Internals.dll
System.Windows.Interactivity.dll
IssueVision.Client.csproj.user
Signin.png
DataSources
IssueVision.Data.Web.IssueVisionContext.datasource
IssueVision.Data.Web.PasswordResetContext.datasource
IssueVision.Common.csproj.user
IssueVision.Data.csproj.user
IssueVision.edmx
DataTypes
IssueVision.Model.csproj.user
IssueVisionSetup.vdproj
IssueVision.ViewModel.csproj.user
IssueVision.Web.csproj.user
IssueVision_Silverlight_20110606.zip
GalaSoft.MvvmLight.Extras.SL4.dll
GalaSoft.MvvmLight.SL4.dll
MenuControls.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
System.Windows.Controls.Toolkit.dll
System.Windows.Controls.Toolkit.Internals.dll
System.Windows.Interactivity.dll
IssueVision.Client.csproj.user
Signin.png
IssueVision.Common.csproj.user
IssueVision.Data.csproj.user
IssueVision.edmx
IssueVision.Model.csproj.user
IssueVisionSetup.vdproj
IssueVision.ViewModel.csproj.user
IssueVision.Web.csproj.user
IssueVision_Silverlight_Setup.zip
IssueVisionSetup.msi
setup.exe
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Windows.Printing;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using IssueVision.Data.Web;
using IssueVision.Common;

namespace IssueVision.ViewModel
{
    [Export(ViewModelTypes.BugReportViewModel, typeof(ViewModelBase))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class BugReportViewModel : ViewModelBase
    {
        #region "Private Data Members"
        private IIssueVisionModel _issueVisionModel;
        private ReportPrintout reportPrintOut;
        private int printedItems;
        #endregion "Private Data Members"

        #region "Constructor"
        [ImportingConstructor]
        public BugReportViewModel(IIssueVisionModel issueVisionModel)
        {
            _issueVisionModel = issueVisionModel;

            // set up event handling
            _issueVisionModel.GetAllUnresolvedIssuesComplete += new EventHandler<EntityResultsArgs<Issue>>(_issueVisionModel_GetAllUnresolvedIssuesComplete);
            _issueVisionModel.GetActiveBugCountByMonthComplete += new EventHandler<InvokeOperationEventArgs>(_issueVisionModel_GetActiveBugCountByMonthComplete);
            _issueVisionModel.GetResolvedBugCountByMonthComplete += new EventHandler<InvokeOperationEventArgs>(_issueVisionModel_GetResolvedBugCountByMonthComplete);
            _issueVisionModel.GetActiveBugCountByPriorityComplete += new EventHandler<InvokeOperationEventArgs>(_issueVisionModel_GetActiveBugCountByPriorityComplete);
            _issueVisionModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_issueVisionModel_PropertyChanged);

            // load all issues
            _issueVisionModel.GetAllUnresolvedIssuesAsync();
            _issueVisionModel.GetActiveBugCountByMonthAsync(6);
            _issueVisionModel.GetResolvedBugCountByMonthAsync(6);
            _issueVisionModel.GetActiveBugCountByPriorityAsync();
        }
        #endregion "Constructor"

        #region "ICleanup interface implementation"
        public override void Cleanup()
        {
            if (_issueVisionModel != null)
            {
                // unregister all events
                _issueVisionModel.GetAllUnresolvedIssuesComplete -= new EventHandler<EntityResultsArgs<Issue>>(_issueVisionModel_GetAllUnresolvedIssuesComplete);
                _issueVisionModel.GetActiveBugCountByMonthComplete -= new EventHandler<InvokeOperationEventArgs>(_issueVisionModel_GetActiveBugCountByMonthComplete);
                _issueVisionModel.GetResolvedBugCountByMonthComplete -= new EventHandler<InvokeOperationEventArgs>(_issueVisionModel_GetResolvedBugCountByMonthComplete);
                _issueVisionModel.GetActiveBugCountByPriorityComplete -= new EventHandler<InvokeOperationEventArgs>(_issueVisionModel_GetActiveBugCountByPriorityComplete);
                _issueVisionModel.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(_issueVisionModel_PropertyChanged);
                _issueVisionModel = null;
            }
            // set properties back to null
            AllIssues = null;
            ActiveBugCountByMonth = null;
            ResolvedBugCountByMonth = null;
            ActiveBugCountByPriority = null;
            // unregister any messages for this ViewModel
            base.Cleanup();
        }
        #endregion "ICleanup interface implementation"

        #region "Public Properties"

        private IEnumerable<Issue> _allIssues;

        public IEnumerable<Issue> AllIssues
        {
            get { return _allIssues; }
            private set
            {
                if (!ReferenceEquals(_allIssues, value))
                {
                    _allIssues = value;
                    this.RaisePropertyChanged("AllIssues");
                }
            }
        }

        private IEnumerable<BugCountByMonth> _activeBugCountByMonth;

        public IEnumerable<BugCountByMonth> ActiveBugCountByMonth
        {
            get { return _activeBugCountByMonth; }
            private set
            {
                if (!ReferenceEquals(_activeBugCountByMonth, value))
                {
                    _activeBugCountByMonth = value;
                    this.RaisePropertyChanged("ActiveBugCountByMonth");
                }
            }
        }

        private IEnumerable<BugCountByMonth> _resolvedBugCountByMonth;

        public IEnumerable<BugCountByMonth> ResolvedBugCountByMonth
        {
            get { return _resolvedBugCountByMonth; }
            private set
            {
                if (!ReferenceEquals(_resolvedBugCountByMonth, value))
                {
                    _resolvedBugCountByMonth = value;
                    this.RaisePropertyChanged("ResolvedBugCountByMonth");
                }
            }
        }

        private IEnumerable<BugCountByPriority> _activeBugCountByPriority;

        public IEnumerable<BugCountByPriority> ActiveBugCountByPriority
        {
            get { return _activeBugCountByPriority; }
            private set
            {
                if (!ReferenceEquals(_activeBugCountByPriority, value))
                {
                    _activeBugCountByPriority = value;
                    this.RaisePropertyChanged("ActiveBugCountByPriority");
                }
            }
        }

        #endregion "Public Properties"

        #region "Public Commands"

        private RelayCommand _printReportCommand = null;

        public RelayCommand PrintReportCommand
        {
            get
            {
                if (_printReportCommand == null)
                {
                    _printReportCommand = new RelayCommand(
                        () => this.OnPrintReportCommand(),
                        () => (this._issueVisionModel != null) && !(this._issueVisionModel.IsBusy));
                }
                return _printReportCommand;
            }
        }

        private void OnPrintReportCommand()
        {
            try
            {
                PrintDocument printDocument = new PrintDocument();
                printDocument.BeginPrint += new EventHandler<BeginPrintEventArgs>(printDocument_BeginPrint);
                printDocument.PrintPage += new EventHandler<PrintPageEventArgs>(printDocument_PrintPage);
                printDocument.EndPrint += new EventHandler<EndPrintEventArgs>(printDocument_EndPrint);
                printDocument.Print("IssueVision Bug Report");
            }
            catch (Exception ex)
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(ex);
            }
        }

        #endregion "Public Commands"

        #region "Private Methods"

        private void _issueVisionModel_GetAllUnresolvedIssuesComplete(object sender, EntityResultsArgs<Issue> e)
        {
            if (!e.HasError)
            {
                this.AllIssues = e.Results
                    .OrderBy(g => g.Priority)
                    .ThenBy(g => g.Severity);
            }
            else
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(e.Error);
            }
        }

        private void _issueVisionModel_GetActiveBugCountByMonthComplete(object sender, InvokeOperationEventArgs e)
        {
            if (!e.HasError)
            {
                List<BugCountByMonth> bugCountList = new List<BugCountByMonth>();
                string[] bugCountStringArray = e.InvokeOp.Value as string[];
                foreach (string bugCount in bugCountStringArray)
                {
                    bugCountList.Add(
                        new BugCountByMonth
                        { 
                            Month = bugCount.Substring(0, 3) + bugCount.Substring(5, 2), 
                            Count = Convert.ToInt32(bugCount.Substring(8))
                        });
                }
                this.ActiveBugCountByMonth = bugCountList.AsEnumerable<BugCountByMonth>();
            }
            else
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(e.Error);
            }
        }


        private void _issueVisionModel_GetResolvedBugCountByMonthComplete(object sender, InvokeOperationEventArgs e)
        {
            if (!e.HasError)
            {
                List<BugCountByMonth> bugCountList = new List<BugCountByMonth>();
                string[] bugCountStringArray = e.InvokeOp.Value as string[];
                foreach (string bugCount in bugCountStringArray)
                {
                    bugCountList.Add(
                        new BugCountByMonth
                        {
                            Month = bugCount.Substring(0, 3) + bugCount.Substring(5, 2),
                            Count = Convert.ToInt32(bugCount.Substring(8))
                        });
                }
                this.ResolvedBugCountByMonth = bugCountList.AsEnumerable<BugCountByMonth>();
            }
            else
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(e.Error);
            }
        }

        private void _issueVisionModel_GetActiveBugCountByPriorityComplete(object sender, InvokeOperationEventArgs e)
        {
            if (!e.HasError)
            {
                List<BugCountByPriority> bugCountList = new List<BugCountByPriority>();
                string[] bugCountStringArray = e.InvokeOp.Value as string[];
                foreach (string bugCount in bugCountStringArray)
                {
                    bugCountList.Add(
                        new BugCountByPriority
                        {
                            Priority = Convert.ToByte(bugCount.Substring(0, bugCount.IndexOf('/'))),
                            Count = Convert.ToInt32(bugCount.Substring(bugCount.IndexOf('/') + 1))
                        });
                }
                this.ActiveBugCountByPriority = bugCountList.AsEnumerable<BugCountByPriority>();
            }
            else
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(e.Error);
            }
        }

        private void _issueVisionModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("IsBusy"))
            {
                PrintReportCommand.RaiseCanExecuteChanged();
            }
        }

        private void printDocument_BeginPrint(object sender, BeginPrintEventArgs e)
        {
            // get the charts currently in display as a WriteableBitmap
            WriteableBitmap writeableBitmap = null;
            var message = new NotificationMessageAction<WriteableBitmap>("Get Charts", s => writeableBitmap = s);
            AppMessages.GetChartsMessage.Send(message);

            this.reportPrintOut = new ReportPrintout(writeableBitmap);
            this.printedItems = 0;
        }

        private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            if (this.reportPrintOut != null)
            {
                e.PageVisual = this.reportPrintOut;
                this.reportPrintOut.Paginate(this.AllIssues, this.printedItems, this.printedItems == 0, e.PrintableArea);
                this.printedItems += this.reportPrintOut.NumItemsShown;
                e.HasMorePages = printedItems < this.AllIssues.Count();
            }
            else
                e.HasMorePages = false;
        }

        private void printDocument_EndPrint(object sender, EndPrintEventArgs e)
        {
            this.reportPrintOut = null;
        }

        #endregion "Private Methods"
    }
}

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)

Share

About the Author

Weidong Shen
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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 7 Jul 2011
Article Copyright 2010 by Weidong Shen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid