AutoPopulateExportedViewsBehavior.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. protected override void OnAttach()
  27. {
  28. AddRegisteredViews();
  29. }
  30. public void OnImportsSatisfied()
  31. {
  32. AddRegisteredViews();
  33. }
  34. private void AddRegisteredViews()
  35. {
  36. if (this.Region != null)
  37. {
  38. foreach (var viewEntry in this.RegisteredViews)
  39. {
  40. if (viewEntry.Metadata.RegionName == this.Region.Name)
  41. {
  42. var view = viewEntry.Value;
  43. if (!this.Region.Views.Contains(view))
  44. {
  45. this.Region.Add(view);
  46. }
  47. }
  48. }
  49. }
  50. }
  51. [ImportMany(AllowRecomposition = true)]
  52. public Lazy<object, IViewRegionRegistration>[] RegisteredViews { get; set; }
  53. }
  54. }