Composite WPF (Prism) module resource data templates

后端 未结 3 517
余生分开走
余生分开走 2020-12-08 03:11

Given that I have a shell application and a couple of separate module projects using Microsoft CompoisteWPF (Prism v2)...

On receiving a command, a module creates a

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 03:44

    To avoid your shell app from having to know anything about your modules and your modules from reaching out into the shell in any way, I'd provide an interface to your modules like this:

    IMergeDictionaryRegistry
    {
         void AddDictionaryResource(Uri packUri);
    }
    

    You'd ask for this interface in your Module code:

    public class MyModule : IModule
    {
         IMergeDictionaryRegistry _merger;
         public MyModule(IMergeDictionaryRegistry merger)
         {
              _merger = merger;
         }
    
         public void Initialize()
         {
              _merger.AddDictionaryResource(new Uri("pack://application:,,,/Module1;component/Module1Resources.xaml");
         }
    }
    

    You would then implement this in your shell to do this:

    public MergeDictionaryRegistry : IMergeDictionaryRegistry
    {
         public void AddDictionaryResource(Uri packUri)
         {
              Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
              {
                   Source = packUri;
              });
         }
    }
    

    And then finally, in your Bootstrapper's ConfigureContainer:

    public override void ConfigureContainer()
    {
         base.ConfigureContainer();
         Container.RegisterType();
    }
    

    This will get you the functionality you want and your Shell and your Module will remain independent of each other. This has the added benefit of being more testable in that you have no need to spin up an Application to test your module code (just mock IMergeDictionaryRegistry and you are done).

    Let us know how this goes for you.

提交回复
热议问题