Integrating Modules with Application in Prism aka CompositeWpf

邮差的信 提交于 2019-12-03 23:05:47

What I do is create a view registry in my Shell with the following interface (I'm simplifying here):

public interface IViewRegistry
{
     void RegisterView(string title, string key, Func<UIElement> viewCreationMethod);
     void OpenView(string key);
}

This is way oversimplifying, but hopefully this gives you a picture. Each module registers its views with the shell using this interface on initialization. In my shell, I create a ViewStore that stores these things.

public static class ViewStore
{
     public Dictionary<string, ViewEntry> Views { get; set; }
     static ViewStore()
     {
          Views = new Dictionary<string, ViewEntry>();
     }

     public void RegisterView(string name, string key, Func<UIElement> createMethod)
     {
         Views.Add(key, new ViewEntry() { Name = name, CreateMethod = createMethod });
     }
}

Then from my IViewRegistry implementation:

public class ViewRegistryService : IViewRegistry
{
     public void RegisterView(string title, string key, Func<UIElement> createMethod)
     {
          ViewStore.RegisterView(title, key, createMethod);
     }

     public void OpenView(string key)
     {
          //Check here with your region manager to see if
          //the view is already open, if not, inject it
          var view = _regionManager.Regions["MyRegion"].GetView(key);
          if(view != null)
          {
               view = ViewStore.Views[key]();
               _regionManager.Regions["MyRegion"].Add(view, key);
          }
          _regionManager.Regions["MyRegion"].Activate(view);
     }

     private IRegionManager _regionManager;
     public ViewRegistryService(IRegionManager rm)
     {
          _regionManager = rm;
     }
}

Now I have two things:

  1. A ViewStore I can use to create a menu in my shell.
  2. A way for modules to open views owned by other modules without coupling beyond simple ModuleDependencies (in reality, even the ModuleDependency is not necessary, but probably correct.

Obviously this way oversimplifies things. I have things that indicate whether or not a view should be a menu item. My app has several menus, etc, but this is the basics and should get you going.

Also, you should give Stackoverflow a little bit of a chance to get you an answer... you only gave us 3 hours before you gave up :)

Hope this helps.

RegisterViewWithRegion does not have an overload that accept View name as parameter. This could have eased integrating Modules. I've added a work item in Codeplex

Currently I'm adding reference to other Modules for View Injection and losing loosely coupled nature of Prism

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!