PageRenderTime 75ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/PasswordMgr/Gwn.Library.Prism.Phone/Regions/RegionAdapterBase.cs

#
C# | 177 lines | 87 code | 21 blank | 69 comment | 19 complexity | 383d29185e75d7ae4e5682171ae0b52b MD5 | raw file
Possible License(s): LGPL-2.0
  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.Globalization;
  19. using System.Windows;
  20. using Microsoft.Practices.Prism.Properties;
  21. using Microsoft.Practices.Prism.Regions.Behaviors;
  22. namespace Microsoft.Practices.Prism.Regions
  23. {
  24. /// <summary>
  25. /// Base class to facilitate the creation of <see cref="IRegionAdapter"/> implementations.
  26. /// </summary>
  27. /// <typeparam name="T">Type of object to adapt.</typeparam>
  28. public abstract class RegionAdapterBase<T> : IRegionAdapter where T : class
  29. {
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="RegionAdapterBase&lt;T&gt;"/> class.
  32. /// </summary>
  33. public RegionAdapterBase(){}
  34. /// <summary>
  35. /// Initializes a new instance of <see cref="RegionAdapterBase{T}"/>.
  36. /// </summary>
  37. /// <param name="regionBehaviorFactory">The factory used to create the region behaviors to attach to the created regions.</param>
  38. protected RegionAdapterBase(IRegionBehaviorFactory regionBehaviorFactory)
  39. {
  40. this.RegionBehaviorFactory = regionBehaviorFactory;
  41. }
  42. /// <summary>
  43. /// Gets or sets the factory used to create the region behaviors to attach to the created regions.
  44. /// </summary>
  45. protected IRegionBehaviorFactory RegionBehaviorFactory { get; set; }
  46. /// <summary>
  47. /// Adapts an object and binds it to a new <see cref="IRegion"/>.
  48. /// </summary>
  49. /// <param name="regionTarget">The object to adapt.</param>
  50. /// <param name="regionName">The name of the region to be created.</param>
  51. /// <returns>The new instance of <see cref="IRegion"/> that the <paramref name="regionTarget"/> is bound to.</returns>
  52. public IRegion Initialize(T regionTarget, string regionName)
  53. {
  54. if (regionName == null)
  55. {
  56. throw new ArgumentNullException("regionName");
  57. }
  58. IRegion region = this.CreateRegion();
  59. region.Name = regionName;
  60. SetObservableRegionOnHostingControl(region, regionTarget);
  61. this.Adapt(region, regionTarget);
  62. this.AttachBehaviors(region, regionTarget);
  63. this.AttachDefaultBehaviors(region, regionTarget);
  64. return region;
  65. }
  66. /// <summary>
  67. /// Adapts an object and binds it to a new <see cref="IRegion"/>.
  68. /// </summary>
  69. /// <param name="regionTarget">The object to adapt.</param>
  70. /// <param name="regionName">The name of the region to be created.</param>
  71. /// <returns>The new instance of <see cref="IRegion"/> that the <paramref name="regionTarget"/> is bound to.</returns>
  72. /// <remarks>This methods performs validation to check that <paramref name="regionTarget"/>
  73. /// is of type <typeparamref name="T"/>.</remarks>
  74. /// <exception cref="ArgumentNullException">When <paramref name="regionTarget"/> is <see langword="null" />.</exception>
  75. /// <exception cref="InvalidOperationException">When <paramref name="regionTarget"/> is not of type <typeparamref name="T"/>.</exception>
  76. IRegion IRegionAdapter.Initialize(object regionTarget, string regionName)
  77. {
  78. return this.Initialize(GetCastedObject(regionTarget), regionName);
  79. }
  80. /// <summary>
  81. /// This method adds the default behaviors by using the <see cref="IRegionBehaviorFactory"/> object.
  82. /// </summary>
  83. /// <param name="region">The region being used.</param>
  84. /// <param name="regionTarget">The object to adapt.</param>
  85. protected virtual void AttachDefaultBehaviors(IRegion region, T regionTarget)
  86. {
  87. if (region == null) throw new ArgumentNullException("region");
  88. if (regionTarget == null) throw new ArgumentNullException("regionTarget");
  89. IRegionBehaviorFactory behaviorFactory = this.RegionBehaviorFactory;
  90. if (behaviorFactory != null)
  91. {
  92. DependencyObject dependencyObjectRegionTarget = regionTarget as DependencyObject;
  93. foreach (string behaviorKey in behaviorFactory)
  94. {
  95. if (!region.Behaviors.ContainsKey(behaviorKey))
  96. {
  97. IRegionBehavior behavior = behaviorFactory.CreateFromKey(behaviorKey);
  98. if (dependencyObjectRegionTarget != null)
  99. {
  100. IHostAwareRegionBehavior hostAwareRegionBehavior = behavior as IHostAwareRegionBehavior;
  101. if (hostAwareRegionBehavior != null)
  102. {
  103. hostAwareRegionBehavior.HostControl = dependencyObjectRegionTarget;
  104. }
  105. }
  106. region.Behaviors.Add(behaviorKey, behavior);
  107. }
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// Template method to attach new behaviors.
  113. /// </summary>
  114. /// <param name="region">The region being used.</param>
  115. /// <param name="regionTarget">The object to adapt.</param>
  116. protected virtual void AttachBehaviors(IRegion region, T regionTarget)
  117. {
  118. }
  119. /// <summary>
  120. /// Template method to adapt the object to an <see cref="IRegion"/>.
  121. /// </summary>
  122. /// <param name="region">The new region being used.</param>
  123. /// <param name="regionTarget">The object to adapt.</param>
  124. protected abstract void Adapt(IRegion region, T regionTarget);
  125. /// <summary>
  126. /// Template method to create a new instance of <see cref="IRegion"/>
  127. /// that will be used to adapt the object.
  128. /// </summary>
  129. /// <returns>A new instance of <see cref="IRegion"/>.</returns>
  130. protected abstract IRegion CreateRegion();
  131. private static T GetCastedObject(object regionTarget)
  132. {
  133. if (regionTarget == null)
  134. {
  135. throw new ArgumentNullException("regionTarget");
  136. }
  137. T castedObject = regionTarget as T;
  138. if (castedObject == null)
  139. {
  140. throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.AdapterInvalidTypeException, typeof(T).Name));
  141. }
  142. return castedObject;
  143. }
  144. private static void SetObservableRegionOnHostingControl(IRegion region, T regionTarget)
  145. {
  146. DependencyObject targetElement = regionTarget as DependencyObject;
  147. if (targetElement != null)
  148. {
  149. // Set the region as a dependency property on the control hosting the region
  150. // Because we are using an observable region, the hosting control can detect that the
  151. // region has actually been created. This is an ideal moment to hook up custom behaviors
  152. RegionManager.GetObservableRegion(targetElement).Value = region;
  153. }
  154. }
  155. }
  156. }