PageRenderTime 41ms CodeModel.GetById 14ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

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