在 Prism aka CompositeWpf 中将模块与应用程序集成 [英] Integrating Modules with Application in Prism aka CompositeWpf

查看:51
本文介绍了在 Prism aka CompositeWpf 中将模块与应用程序集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 MSDN:

您的模块中的大多数视图可能不必直接显示,而必须在用户执行某些操作后才能显示.根据应用程序的风格,您可能希望为用户使用菜单、工具栏或其他导航策略来访问视图.在模块的初始化方法中,也可以注册应用的导航结构.在导航结构的事件处理程序中(即,当用户单击菜单项时),您可以使用视图注入技术将视图添加到适当的区域.

It is likely that most of the views in your modules will not have to be displayed directly, but only after some action by the user. Depending on the style of application, you may want to use menus, toolbars, or other navigation strategies for your users to access views. In the initialization method of the module, you can also register with the application's navigation structure. In the event handlers of your navigation structure (that is, when a user clicks on a menu item), you might use View Injection techniques to add views to the appropriate regions.

我有一个类似的场景,我在 Module 的 Initialization 方法中使用 RegisterViewWithRegion 将视图添加到区域.我很想显示基于视图的用户与菜单(这是一个不同的模块)的交互.

I've a similar scenario, I'm adding Views to Regions in the Initialization method of Module using RegisterViewWithRegion. I'd love to display a View based user interaction with a Menu (which is a different module).

如何在不破坏 Prism 中模块的解耦行为的情况下实现这种行为?

是否可以激活/显示已添加到区域的视图,例如模块 B 中的模块 A?

推荐答案

我所做的是在我的 Shell 中创建一个具有以下界面的视图注册表(我在这里进行了简化):

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);
}

这太简单了,但希望这能给你一个画面.每个模块在初始化时使用此接口向 shell 注册其视图.在我的 shell 中,我创建了一个 ViewStore 来存储这些东西.

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 });
     }
}

然后从我的 IViewRegistry 实现:

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;
     }
}

现在我有两件事:

  1. 可用于在我的 shell 中创建菜单的 ViewStore.
  2. 一种模块打开其他模块拥有的视图的方法,而无需在简单的 ModuleDependencies 之外进行耦合(实际上,即使 ModuleDependency 也不是必需的,但可能是正确的.

显然这种方式过于简单化了.我有一些指示视图是否应该是菜单项的东西.我的应用程序有多个菜单等,但这是基础知识,应该可以帮助您.

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.

另外,你应该给 Stackoverflow 一点机会让你得到答案......你只给了我们 3 个小时就放弃了 :)

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 :)

希望这会有所帮助.

这篇关于在 Prism aka CompositeWpf 中将模块与应用程序集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆