AutoPopulateExportedViewsBehavior.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.ComponentModel.Composition;
  19. using Microsoft.Practices.Prism.Regions;
  20. namespace Infrastructure
  21. {
  22. [Export(typeof (AutoPopulateExportedViewsBehavior))]
  23. [PartCreationPolicy(CreationPolicy.NonShared)]
  24. public class AutoPopulateExportedViewsBehavior : RegionBehavior, IPartImportsSatisfiedNotification
  25. {
  26. [ImportMany(AllowRecomposition = true)]
  27. public Lazy<object, IViewRegionRegistration>[] RegisteredViews
  28. {
  29. get;
  30. set;
  31. }
  32. #region IPartImportsSatisfiedNotification Members
  33. public void OnImportsSatisfied()
  34. {
  35. AddRegisteredViews();
  36. }
  37. #endregion
  38. protected override void OnAttach()
  39. {
  40. AddRegisteredViews();
  41. }
  42. private void AddRegisteredViews()
  43. {
  44. if (Region == null)
  45. {
  46. return;
  47. }
  48. foreach (var viewEntry in RegisteredViews)
  49. {
  50. if (viewEntry.Metadata.RegionName == Region.Name)
  51. {
  52. object view = viewEntry.Value;
  53. if (!Region.Views.Contains(view))
  54. {
  55. Region.Add(view);
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }