将模块与 Prism 中的应用程序集成(又名 CompositeWpf)
-
13-09-2019 - |
题
从 微软软件定义网络:
模块中的大多数视图很可能不必直接显示,而只有在用户执行某些操作后才显示。根据应用程序的风格,您可能需要使用菜单、工具栏或其他导航策略供用户访问视图。在模块的初始化方法中,您还可以向应用程序的导航结构进行注册。在导航结构的事件处理程序中(即,当用户单击菜单项时),您可以使用视图注入技术将视图添加到适当的区域。
我有一个类似的场景,我使用 RegisterViewWithRegion 在模块的初始化方法中将视图添加到区域。我很想显示基于视图的用户与菜单(这是一个不同的模块)的交互。
如何在不破坏 Prism 中模块的解耦行为的情况下实现此行为?
是否可以激活/显示已添加到区域的视图(例如由 ModuleA 从 ModuleB 添加到区域)?
解决方案
我所做的是在我的 Shell 中使用以下接口创建一个视图注册表(我在这里进行了简化):
public interface IViewRegistry
{
void RegisterView(string title, string key, Func<UIElement> viewCreationMethod);
void OpenView(string key);
}
这过于简单化了,但希望这能为您提供一个图片。每个模块在初始化时使用此接口向 shell 注册其视图。在我的 shell 中,我创建了一个 ViewStore 来存储这些内容。
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 实现中:
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;
}
}
现在我有两件事:
- 我可以使用 ViewStore 在 shell 中创建菜单。
- 模块打开其他模块拥有的视图的一种方法,无需超出简单的 ModuleDependency 耦合(实际上,甚至 ModuleDependency 也不是必需的,但可能是正确的。
显然这种方式把事情过于简单化了。我有一些东西可以指示视图是否应该是菜单项。我的应用程序有几个菜单等,但这是基础知识,应该可以帮助您继续。
另外,你应该给 Stackoverflow 一点机会让你得到答案......你只给了我们3个小时就放弃了:)
希望这可以帮助。
其他提示
RegisterViewWithRegion不具有接受查看名称作为参数的重载。这可能缓解集成模块。我在 Codeplex上
目前我添加参考其它模块用于查看注射和失去棱镜的松散耦合性质
不隶属于 StackOverflow