PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/PrismLibrary/Desktop/Prism/Regions/RegionManagerExtensions.cs

#
C# | 174 lines | 74 code | 21 blank | 79 comment | 20 complexity | 0e1088a3396303c6a559c35cd79e7ddf 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.Globalization;
  19. using System.Threading;
  20. using Microsoft.Practices.Prism.Properties;
  21. using Microsoft.Practices.ServiceLocation;
  22. namespace Microsoft.Practices.Prism.Regions
  23. {
  24. /// <summary>
  25. /// Class that creates a fluent interface for the <see cref="IRegionManager"/> class, with respect to
  26. /// adding views to regions (View Injection pattern), registering view types to regions (View Discovery pattern)
  27. /// </summary>
  28. public static class RegionManagerExtensions
  29. {
  30. /// <summary>
  31. /// Add a view to the Views collection of a Region. Note that the region must already exist in this regionmanager.
  32. /// </summary>
  33. /// <param name="regionManager">The regionmanager that this extension method effects.</param>
  34. /// <param name="regionName">The name of the region to add a view to</param>
  35. /// <param name="view">The view to add to the views collection</param>
  36. /// <returns>The RegionManager, to easily add several views. </returns>
  37. public static IRegionManager AddToRegion(this IRegionManager regionManager, string regionName, object view)
  38. {
  39. if (regionManager == null) throw new ArgumentNullException("regionManager");
  40. if (!regionManager.Regions.ContainsRegionWithName(regionName))
  41. {
  42. throw new ArgumentException(
  43. string.Format(Thread.CurrentThread.CurrentCulture, Resources.RegionNotFound, regionName), "regionName");
  44. }
  45. IRegion region = regionManager.Regions[regionName];
  46. return region.Add(view);
  47. }
  48. /// <summary>
  49. /// Associate a view with a region, by registering a type. When the region get's displayed
  50. /// this type will be resolved using the ServiceLocator into a concrete instance. The instance
  51. /// will be added to the Views collection of the region
  52. /// </summary>
  53. /// <param name="regionManager">The regionmanager that this extension method effects.</param>
  54. /// <param name="regionName">The name of the region to associate the view with.</param>
  55. /// <param name="viewType">The type of the view to register with the </param>
  56. /// <returns>The regionmanager, for adding several views easily</returns>
  57. public static IRegionManager RegisterViewWithRegion(this IRegionManager regionManager, string regionName, Type viewType)
  58. {
  59. var regionViewRegistry = ServiceLocator.Current.GetInstance<IRegionViewRegistry>();
  60. regionViewRegistry.RegisterViewWithRegion(regionName, viewType);
  61. return regionManager;
  62. }
  63. /// <summary>
  64. /// Associate a view with a region, using a delegate to resolve a concreate instance of the view.
  65. /// When the region get's displayed, this delelgate will be called and the result will be added to the
  66. /// views collection of the region.
  67. /// </summary>
  68. /// <param name="regionManager">The regionmanager that this extension method effects.</param>
  69. /// <param name="regionName">The name of the region to associate the view with.</param>
  70. /// <param name="getContentDelegate">The delegate used to resolve a concreate instance of the view.</param>
  71. /// <returns>The regionmanager, for adding several views easily</returns>
  72. public static IRegionManager RegisterViewWithRegion(this IRegionManager regionManager, string regionName, Func<object> getContentDelegate)
  73. {
  74. var regionViewRegistry = ServiceLocator.Current.GetInstance<IRegionViewRegistry>();
  75. regionViewRegistry.RegisterViewWithRegion(regionName, getContentDelegate);
  76. return regionManager;
  77. }
  78. /// <summary>
  79. /// Adds a region to the regionmanager with the name received as argument.
  80. /// </summary>
  81. /// <param name="regionCollection">The regionmanager's collection of regions.</param>
  82. /// <param name="regionName">The name to be given to the region.</param>
  83. /// <param name="region">The region to be added to the regionmanager.</param>
  84. /// <exception cref="ArgumentNullException">Thrown if <paramref name="region"/> or <paramref name="regionCollection"/> is <see langword="null"/>.</exception>
  85. /// <exception cref="ArgumentException">Thrown if <paramref name="regionName"/> and <paramref name="region"/>'s name do not match and the <paramref name="region"/> <see cref="IRegion.Name"/> is not <see langword="null"/>.</exception>
  86. public static void Add(this IRegionCollection regionCollection, string regionName, IRegion region)
  87. {
  88. if (region == null) throw new ArgumentNullException("region");
  89. if (regionCollection == null) throw new ArgumentNullException("regionCollection");
  90. if (region.Name != null && region.Name != regionName)
  91. {
  92. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.RegionManagerWithDifferentNameException, region.Name, regionName), "regionName");
  93. }
  94. if (region.Name == null)
  95. {
  96. region.Name = regionName;
  97. }
  98. regionCollection.Add(region);
  99. }
  100. /// <summary>
  101. /// Navigates the specified region manager.
  102. /// </summary>
  103. /// <param name="regionManager">The regionmanager that this extension method effects.</param>
  104. /// <param name="regionName">The name of the region to call Navigate on.</param>
  105. /// <param name="source">The URI of the content to display.</param>
  106. /// <param name="navigationCallback">The navigation callback.</param>
  107. public static void RequestNavigate(this IRegionManager regionManager, string regionName, Uri source, Action<NavigationResult> navigationCallback)
  108. {
  109. if (regionManager == null) throw new ArgumentNullException("regionManager");
  110. if (navigationCallback == null) throw new ArgumentNullException("navigationCallback");
  111. if (regionManager.Regions.ContainsRegionWithName(regionName))
  112. {
  113. regionManager.Regions[regionName].RequestNavigate(source, navigationCallback);
  114. }
  115. else
  116. {
  117. navigationCallback(new NavigationResult(new NavigationContext(null, source), false));
  118. }
  119. }
  120. /// <summary>
  121. /// Navigates the specified region manager.
  122. /// </summary>
  123. /// <param name="regionManager">The regionmanager that this extension method effects.</param>
  124. /// <param name="regionName">The name of the region to call Navigate on.</param>
  125. /// <param name="source">The URI of the content to display.</param>
  126. public static void RequestNavigate(this IRegionManager regionManager, string regionName, Uri source)
  127. {
  128. RequestNavigate(regionManager, regionName, source, nr => { });
  129. }
  130. /// <summary>
  131. /// Navigates the specified region manager.
  132. /// </summary>
  133. /// <param name="regionManager">The regionmanager that this extension method effects.</param>
  134. /// <param name="regionName">The name of the region to call Navigate on.</param>
  135. /// <param name="source">The URI of the content to display.</param>
  136. /// <param name="navigationCallback">The navigation callback.</param>
  137. public static void RequestNavigate(this IRegionManager regionManager, string regionName, string source, Action<NavigationResult> navigationCallback)
  138. {
  139. if (source == null) throw new ArgumentNullException("source");
  140. RequestNavigate(regionManager, regionName, new Uri(source, UriKind.RelativeOrAbsolute), navigationCallback);
  141. }
  142. /// <summary>
  143. /// Navigates the specified region manager.
  144. /// </summary>
  145. /// <param name="regionManager">The regionmanager that this extension method effects.</param>
  146. /// <param name="regionName">The name of the region to call Navigate on.</param>
  147. /// <param name="source">The URI of the content to display.</param>
  148. public static void RequestNavigate(this IRegionManager regionManager, string regionName, string source)
  149. {
  150. RequestNavigate(regionManager, regionName, source, nr => { });
  151. }
  152. }
  153. }