PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 137 lines | 92 code | 16 blank | 29 comment | 25 complexity | 3b92f044adb40ae4e0962e11136c980b 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. namespace ModularityWithMef.Desktop
  18. {
  19. using System;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Input;
  23. using Microsoft.Practices.Prism.Modularity;
  24. /// <summary>
  25. /// Interaction logic for ModuleControl.xaml
  26. /// </summary>
  27. public partial class ModuleControl : UserControl
  28. {
  29. private ModuleTrackingState moduleTrackingState;
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="ModuleControl"/> class.
  32. /// </summary>
  33. public ModuleControl()
  34. {
  35. this.InitializeComponent();
  36. this.DataContextChanged += this.ModuleControl_DataContextChanged;
  37. this.OnDataContextChanged();
  38. }
  39. /// <summary>
  40. /// Raised when the user clicks to load the module.
  41. /// </summary>
  42. public event EventHandler RequestModuleLoad;
  43. private void RaiseRequestModuleLoad()
  44. {
  45. if (this.RequestModuleLoad != null)
  46. {
  47. this.RequestModuleLoad(this, EventArgs.Empty);
  48. }
  49. }
  50. /// <summary>
  51. /// Invoked when an unhandled <see cref="E:System.Windows.UIElement.MouseLeftButtonUp"/> routed event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
  52. /// </summary>
  53. /// <param name="e">The <see cref="T:System.Windows.Input.MouseButtonEventArgs"/> that contains the event data. The event data reports that the left mouse button was released.</param>
  54. protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
  55. {
  56. base.OnMouseLeftButtonUp(e);
  57. if (!e.Handled)
  58. {
  59. if ((this.moduleTrackingState != null) && (this.moduleTrackingState.ExpectedInitializationMode == InitializationMode.OnDemand) && (this.moduleTrackingState.ModuleInitializationStatus == ModuleInitializationStatus.NotStarted))
  60. {
  61. this.RaiseRequestModuleLoad();
  62. e.Handled = true;
  63. }
  64. }
  65. }
  66. private void UpdateClickToLoadTextBlockVisibility()
  67. {
  68. if ((this.moduleTrackingState != null)
  69. && (this.moduleTrackingState.ExpectedInitializationMode == InitializationMode.OnDemand)
  70. && (this.moduleTrackingState.ModuleInitializationStatus == ModuleInitializationStatus.NotStarted))
  71. {
  72. this.ClickToLoadTextBlock.Visibility = Visibility.Visible;
  73. }
  74. else
  75. {
  76. this.ClickToLoadTextBlock.Visibility = Visibility.Collapsed;
  77. }
  78. }
  79. private void UpdateLoadProgressTextBlockVisibility()
  80. {
  81. if ((this.moduleTrackingState != null)
  82. && (this.moduleTrackingState.ExpectedDownloadTiming == DownloadTiming.InBackground)
  83. && (this.moduleTrackingState.ModuleInitializationStatus == ModuleInitializationStatus.Downloading))
  84. {
  85. this.LoadProgressPanel.Visibility = Visibility.Visible;
  86. }
  87. else
  88. {
  89. this.LoadProgressPanel.Visibility = Visibility.Collapsed;
  90. }
  91. }
  92. private void ModuleControl_Loaded(object sender, RoutedEventArgs e)
  93. {
  94. this.UpdateClickToLoadTextBlockVisibility();
  95. this.UpdateLoadProgressTextBlockVisibility();
  96. }
  97. private void ModuleControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
  98. {
  99. this.OnDataContextChanged();
  100. }
  101. private void OnDataContextChanged()
  102. {
  103. if (this.moduleTrackingState != null)
  104. {
  105. this.moduleTrackingState.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(this.ModuleTrackingState_PropertyChanged);
  106. }
  107. this.moduleTrackingState = this.DataContext as ModuleTrackingState;
  108. if (this.moduleTrackingState != null)
  109. {
  110. this.moduleTrackingState.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(this.ModuleTrackingState_PropertyChanged);
  111. }
  112. this.UpdateClickToLoadTextBlockVisibility();
  113. this.UpdateLoadProgressTextBlockVisibility();
  114. }
  115. private void ModuleTrackingState_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  116. {
  117. this.UpdateClickToLoadTextBlockVisibility();
  118. this.UpdateLoadProgressTextBlockVisibility();
  119. }
  120. }
  121. }