AutoPopulateExportedViewsBehavior.cs 2.1 KB

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