PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/MVVM RI/MVVM.Client/Infrastructure/SingleViewUIService.cs

#
C# | 93 lines | 44 code | 8 blank | 41 comment | 0 complexity | e4704199c473275800cb1fbef00b354a MD5 | raw file
 1//===================================================================================
 2// Microsoft patterns & practices
 3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
 4//===================================================================================
 5// Copyright (c) Microsoft Corporation.  All rights reserved.
 6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
 7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
 8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 9// FITNESS FOR A PARTICULAR PURPOSE.
10//===================================================================================
11// The example companies, organizations, products, domain names,
12// e-mail addresses, logos, people, places, and events depicted
13// herein are fictitious.  No association with any real company,
14// organization, product, domain name, email address, logo, person,
15// places, or events is intended or should be inferred.
16//===================================================================================
17using System.ComponentModel.Composition;
18using System.ComponentModel.Composition.Hosting;
19using System.Windows.Controls;
20using MVVM.Client.Infrastructure.StateManagement;
21
22namespace MVVM.Client.Infrastructure
23{
24    /// <summary>
25    /// Implementation of the <see cref="ISingleViewUIService"/> that relies on a <see cref="IMainView"/>
26    /// </summary>
27    /// <typeparam name="TMainView"><see cref="IMainView"/> type</typeparam>
28    /// <seealso cref="ISingleViewUIService"/>
29    /// <seealso cref="IState{T}"/>
30    /// <seealso cref="ICurrentState{T}"/>
31    public abstract class SingleViewUIService<TMainView> : ISingleViewUIService
32        where TMainView : UserControl, IMainView, new()
33    {
34        private readonly TMainView mainWindow;
35
36        protected SingleViewUIService()
37        {
38            this.mainWindow = new TMainView();
39        }
40
41        [Import(typeof(ViewFactory))]
42        public ViewFactory ViewFactory { get; set; }
43
44        [Import(typeof(StateHandler))]
45        public StateHandler StateHandler { get; set; }
46
47        /// <summary>
48        /// Gets the MainWindow which hosts the current view;
49        /// </summary>
50        public IMainView MainWindow
51        {
52            get
53            {
54                return this.mainWindow;
55            }
56        }
57
58        /// <summary>
59        /// Shows the view identified by <see cref="viewName"/> as the current view.
60        /// </summary>
61        /// <param name="viewName">The view name.</param>
62        public void ShowView(string viewName)
63        {
64            var view = this.ViewFactory.GetView(viewName);
65            this.MainWindow.CurrentView = view;
66        }
67
68        /// <summary>
69        /// Shows the view identified by <see cref="viewName"/> as the current view, making <paramref name="context"/>
70        /// available to views and view models.
71        /// </summary>
72        /// <remarks>
73        /// <paramref name="context"/> is a plain object. It must be imported by the view associated to 
74        /// <paramref name="viewName"/> or its view model. See <see cref="IState{T}"/> for details.
75        /// </remarks>
76        /// <typeparam name="T">The type for the <paramref name="context"/>.</typeparam>
77        /// <param name="viewName">The view name.</param>
78        /// <param name="context">Context information required by the target view.</param>
79        public void ShowView<T>(string viewName, T context)
80        {
81            var previousValue = StateHandler.SetState(context);
82
83            try
84            {
85                this.ShowView(viewName);
86            }
87            finally
88            {
89                StateHandler.SetState(previousValue);
90            }
91        }
92    }
93}