PageRenderTime 28ms CodeModel.GetById 19ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/Quickstarts/Modularity/Desktop/ModularityWithMef/ModularityWithMef.Desktop/Shell.xaml.cs

#
C# | 156 lines | 65 code | 16 blank | 75 comment | 0 complexity | bc72f052430985cd653f0de8c782caeb 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;
 18using System.ComponentModel.Composition;
 19using System.Globalization;
 20using System.Windows;
 21using Microsoft.Practices.Prism.Logging;
 22using Microsoft.Practices.Prism.Modularity;
 23using ModuleTracking;
 24
 25namespace ModularityWithMef.Desktop
 26{
 27    /// <summary>
 28    /// Interaction logic for Shell.xaml
 29    /// </summary>
 30    [Export]
 31    public partial class Shell : Window, IPartImportsSatisfiedNotification
 32    {
 33
 34#pragma warning disable 0649 // Imported by MEF
 35        // The shell imports IModuleTracker once to record updates as modules are downloaded.        
 36        [Import(AllowRecomposition = false)] private IModuleTracker moduleTracker;
 37
 38        // The shell imports IModuleManager once to load modules on-demand.
 39        [Import(AllowRecomposition = false)] private IModuleManager moduleManager;
 40
 41        // The shell imports the logger once to output logs to the UI.
 42        [Import(AllowRecomposition = false)] private CallbackLogger logger;
 43#pragma warning restore 0649 
 44
 45        /// <summary>
 46        /// Initializes a new instance of the <see cref="Shell"/> class.
 47        /// </summary>
 48        public Shell()
 49        {
 50            this.InitializeComponent();
 51        }
 52
 53        /// <summary>
 54        /// Logs the specified message.  Called by the CallbackLogger.
 55        /// </summary>
 56        /// <param name="message">The message.</param>
 57        /// <param name="category">The category.</param>
 58        /// <param name="priority">The priority.</param>
 59        public void Log(string message, Category category, Priority priority)
 60        {
 61            this.TraceTextBox.AppendText(
 62                                            string.Format(
 63                                                        CultureInfo.CurrentUICulture, 
 64                                                        "[{0}][{1}] {2}\r\n", 
 65                                                        category,
 66                                                        priority, 
 67                                                        message));
 68        }
 69
 70        /// <summary>
 71        /// Called when a part's imports have been satisfied and it is safe to use.
 72        /// </summary>
 73        public void OnImportsSatisfied()
 74        {
 75            // IPartImportsSatisfiedNotification is useful when you want to coordinate doing some work
 76            // with imported parts independent of when the UI is visible.
 77
 78            // I use the IModuleTracker as the data-context for data-binding.
 79            // This quickstart only demonstrates modularity for Prism and does not use data-binding patterns such as MVVM.
 80            this.DataContext = this.moduleTracker;
 81
 82            // I set this shell's Log method as the callback for receiving log messages.
 83            this.logger.Callback = this.Log;
 84            this.logger.ReplaySavedLogs();
 85
 86            // I subscribe to events to help track module loading/loaded progress.
 87            // The ModuleManager uses the Async Events Pattern.
 88            this.moduleManager.LoadModuleCompleted += this.ModuleManager_LoadModuleCompleted;
 89            this.moduleManager.ModuleDownloadProgressChanged += this.ModuleManager_ModuleDownloadProgressChanged;
 90        }
 91
 92        /// <summary>
 93        /// Handles the RequestModuleLoad event of the ModuleB control.
 94        /// </summary>
 95        /// <param name="sender">The source of the event.</param>
 96        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
 97        private void ModuleB_RequestModuleLoad(object sender, EventArgs e)
 98        {
 99            // The ModuleManager uses the Async Events Pattern.
100            this.moduleManager.LoadModule(WellKnownModuleNames.ModuleB);
101        }
102
103        /// <summary>
104        /// Handles the RequestModuleLoad event of the ModuleC control.
105        /// </summary>
106        /// <param name="sender">The source of the event.</param>
107        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
108        private void ModuleC_RequestModuleLoad(object sender, EventArgs e)
109        {
110            // The ModuleManager uses the Async Events Pattern.
111            this.moduleManager.LoadModule(WellKnownModuleNames.ModuleC);
112        }
113
114        /// <summary>
115        /// Handles the RequestModuleLoad event of the ModuleE control.
116        /// </summary>
117        /// <param name="sender">The source of the event.</param>
118        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
119        private void ModuleE_RequestModuleLoad(object sender, EventArgs e)
120        {
121            // The ModuleManager uses the Async Events Pattern.
122            this.moduleManager.LoadModule(WellKnownModuleNames.ModuleE);
123        }
124
125        /// <summary>
126        /// Handles the RequestModuleLoad event of the ModuleF control.
127        /// </summary>
128        /// <param name="sender">The source of the event.</param>
129        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
130        private void ModuleF_RequestModuleLoad(object sender, EventArgs e)
131        {
132            // The ModuleManager uses the Async Events Pattern.
133            this.moduleManager.LoadModule(WellKnownModuleNames.ModuleF);
134        }
135
136        /// <summary>
137        /// Handles the LoadModuleProgressChanged event of the ModuleManager control.
138        /// </summary>
139        /// <param name="sender">The source of the event.</param>
140        /// <param name="e">The <see cref="Microsoft.Practices.Composite.Modularity.ModuleDownloadProgressChangedEventArgs"/> instance containing the event data.</param>
141        private void ModuleManager_ModuleDownloadProgressChanged(object sender, ModuleDownloadProgressChangedEventArgs e)
142        {
143            this.moduleTracker.RecordModuleDownloading(e.ModuleInfo.ModuleName, e.BytesReceived, e.TotalBytesToReceive);
144        }
145
146        /// <summary>
147        /// Handles the LoadModuleCompleted event of the ModuleManager control.
148        /// </summary>
149        /// <param name="sender">The source of the event.</param>
150        /// <param name="e">The <see cref="Microsoft.Practices.Composite.Modularity.LoadModuleCompletedEventArgs"/> instance containing the event data.</param>
151        private void ModuleManager_LoadModuleCompleted(object sender, LoadModuleCompletedEventArgs e)
152        {
153            this.moduleTracker.RecordModuleLoaded(e.ModuleInfo.ModuleName);
154        }
155    }
156}