PageRenderTime 30ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 2ms 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//===================================================================================
 17namespace 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
 25    /// <summary>
 26    /// Interaction logic for ModuleControl.xaml
 27    /// </summary>
 28    public partial class ModuleControl : UserControl
 29    {
 30        private ModuleTrackingState moduleTrackingState;
 31
 32        /// <summary>
 33        /// Initializes a new instance of the <see cref="ModuleControl"/> class.
 34        /// </summary>
 35        public ModuleControl()
 36        {
 37            this.InitializeComponent();
 38
 39            this.DataContextChanged += this.ModuleControl_DataContextChanged;
 40            this.OnDataContextChanged();
 41        }
 42        
 43        /// <summary>
 44        /// Raised when the user clicks to load the module.
 45        /// </summary>
 46        public event EventHandler RequestModuleLoad;
 47
 48        private void RaiseRequestModuleLoad()
 49        {
 50            if (this.RequestModuleLoad != null)
 51            {
 52                this.RequestModuleLoad(this, EventArgs.Empty);
 53            }
 54        }
 55
 56        /// <summary>
 57        /// 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.
 58        /// </summary>
 59        /// <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>
 60        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
 61        {
 62            base.OnMouseLeftButtonUp(e);
 63
 64            if (!e.Handled)
 65            {
 66                if ((this.moduleTrackingState != null) && (this.moduleTrackingState.ExpectedInitializationMode == InitializationMode.OnDemand) && (this.moduleTrackingState.ModuleInitializationStatus == ModuleInitializationStatus.NotStarted))
 67                {
 68                    this.RaiseRequestModuleLoad();
 69                    e.Handled = true;
 70                }
 71            }
 72        }
 73
 74        private void UpdateClickToLoadTextBlockVisibility()
 75        {
 76            if ((this.moduleTrackingState != null)
 77                && (this.moduleTrackingState.ExpectedInitializationMode == InitializationMode.OnDemand)
 78                && (this.moduleTrackingState.ModuleInitializationStatus == ModuleInitializationStatus.NotStarted))
 79            {
 80                this.ClickToLoadTextBlock.Visibility = Visibility.Visible;
 81            }
 82            else
 83            {
 84                this.ClickToLoadTextBlock.Visibility = Visibility.Collapsed;
 85            }
 86        }
 87
 88        private void UpdateLoadProgressTextBlockVisibility()
 89        {            
 90            if ((this.moduleTrackingState != null)
 91                && (this.moduleTrackingState.ExpectedDownloadTiming == DownloadTiming.InBackground)
 92                && (this.moduleTrackingState.ModuleInitializationStatus == ModuleInitializationStatus.Downloading))
 93            {
 94                this.LoadProgressPanel.Visibility = Visibility.Visible;
 95            }
 96            else
 97            {
 98                this.LoadProgressPanel.Visibility = Visibility.Collapsed;
 99            }
100        }
101
102        private void ModuleControl_Loaded(object sender, RoutedEventArgs e)
103        {
104            this.UpdateClickToLoadTextBlockVisibility();
105            this.UpdateLoadProgressTextBlockVisibility();
106        }
107
108        private void ModuleControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
109        {
110            this.OnDataContextChanged();
111        }
112
113        private void OnDataContextChanged()
114        {
115            if (this.moduleTrackingState != null)
116            {
117                this.moduleTrackingState.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(this.ModuleTrackingState_PropertyChanged);
118            }
119
120            this.moduleTrackingState = this.DataContext as ModuleTrackingState;
121
122            if (this.moduleTrackingState != null)
123            {
124                this.moduleTrackingState.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(this.ModuleTrackingState_PropertyChanged);
125            }
126
127            this.UpdateClickToLoadTextBlockVisibility();
128            this.UpdateLoadProgressTextBlockVisibility();
129        }
130
131        private void ModuleTrackingState_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
132        {
133            this.UpdateClickToLoadTextBlockVisibility();
134            this.UpdateLoadProgressTextBlockVisibility();
135        }
136    }
137}