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