Prism v2:寻求澄清为什么模块需要“向 shell 注册视图类型”

发布于 2024-07-15 02:54:07 字数 704 浏览 5 评论 0原文

我正在阅读 Prism v2 指南 其中指出:

在初始化期间,模块使用 RegionManager 来定位区域 shell 并添加一个或多个视图 这些地区或注册一个或多个 要在其中创建的视图类型 地区

我知道视图是在引导程序中添加的,例如在 GetModuleCatalog() 方法中:

protected override IModuleCatalog GetModuleCatalog()
{
    ModuleCatalog catalog = new ModuleCatalog()
        .AddModule(typeof(HelloWorldModule.HelloWorldModule));
    return catalog;
}

但是注册视图类型意味着什么? 如果模块已经像上面的代码一样“添加视图”,为什么还需要向 shell“注册视图类型”?

I'm reading through the Prism v2 guidelines in which they state:

During initialization, modules use the
RegionManager to locate regions in the
shell and add one or more views to
those regions or register one or more
view types
to be created within those
regions

I understand that views are added in the bootstrapper e.g. in the GetModuleCatalog() method:

protected override IModuleCatalog GetModuleCatalog()
{
    ModuleCatalog catalog = new ModuleCatalog()
        .AddModule(typeof(HelloWorldModule.HelloWorldModule));
    return catalog;
}

But what does it mean to register a view type? Why do modules need to "register a view type" with the shell if they are already "adding their views" as with the above code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风吹雪碎 2024-07-22 02:54:08

在上面的代码中,您正在填写模块目录。 这是 Prism 中模块化工作原理的一部分。 我在此处有一个解释它的截屏视频。 本质上,您是在告诉 Prism 加载 .dll 或 .xap 文件。 这些“模块”可以包含两件事:服务(认为是接口的实现)和视图。

当加载模块(通常是 .dll 或 .xap 文件)时,将调用 Initialize 方法,您可以在其中注册服务和区域:

public class ModuleA : IModule
{
    IRegionManager _regionManager;
    IUnityContainer _container;

    public ModuleA(IRegionManager regionManager, IUnityContainer container)
    {
        _regionManager = regionManager;
        _container = container;
    }

    #region IModule Members

    public void Initialize()
    {
        _container.RegisterType<ICompanyService, CompanyService>();
        _regionManager.RegisterViewWithRegion("MainRegion", typeof(ModuleAView));

    }

    #endregion
}

注意视图注册:

_regionManager.RegisterViewWithRegion("MainRegion", typeof(ModuleAView));

您可以在 Initialize 中注册任意数量的视图。 并在任何模块的任何初始化中(同样,通常是 .xap 或 .dll)。

In the code above, you're filling out a module catalog. This is part of how modularity works in Prism. I have a screencast explaining it here. Essentially, you're telling Prism to load a .dll or .xap file. These "modules" can contain 2 things: services (think implementations of interfaces) and views.

When a Module (usually a .dll or .xap file) is loaded, an Initialize method is called where you can register services and regions:

public class ModuleA : IModule
{
    IRegionManager _regionManager;
    IUnityContainer _container;

    public ModuleA(IRegionManager regionManager, IUnityContainer container)
    {
        _regionManager = regionManager;
        _container = container;
    }

    #region IModule Members

    public void Initialize()
    {
        _container.RegisterType<ICompanyService, CompanyService>();
        _regionManager.RegisterViewWithRegion("MainRegion", typeof(ModuleAView));

    }

    #endregion
}

Notice the view registration:

_regionManager.RegisterViewWithRegion("MainRegion", typeof(ModuleAView));

You could be registering any number of views here in the Initialize. And in any Initialize for any Module (again, usually a .xap or .dll).

倒数 2024-07-22 02:54:07

在您的代码中,您不是将视图添加到引导程序,而是将模块添加到 ModuleCatalog。 CAB/Prism/Composite UI 世界中的模块可以包含视图,但很多时候它提供其他模块可以使用的某种附加服务。 例如,假设我有一个 Shell,它恰好使用某些对接管理器来显示视图。 我希望模块使用 API IDockingWindowService 来显示/隐藏窗口。 我希望服务实现能够轻松互换,因此我创建了一个模块,其中包含名为 DockingWindowService 的服务并实现 IDockingWindowService。 我使用 ModuleCatalog 注册该模块。

复合框架工作流程将创建此服务,将其注册到引导程序,并且在此之后加载的任何模块都将能够使用 IDockingWindowService。 这个服务不是视图,而是逻辑; 只是想指出这种区别。 也就是说,一个模块可以包含 0 个或多个视图(或者,作为简化,用户控件)。 UI的单位是View。 模块更多的是逻辑和/或 UI 捆绑概念。

回到您的特定问题:文档所说的是,如果您使用区域来显示视图,则可以向区域注册视图类型。 每当显示 Region 时,它都会使用 Unity 容器自动构建 View。

In your code you are not adding Views to the bootstrapper but Modules to the ModuleCatalog. A Module in the CAB/Prism/Composite UI world can contain Views, but many times it provides some sort of add-on service that other Modules can use. For example, let's say I have a Shell that happens to uses some docking manager to display views. I want modules to use an API IDockingWindowService to show/hide window. I want the service implementation to be easily interchangeable so I create a Module that contains a service called DockingWindowService and implements IDockingWindowService. I register this Module with the ModuleCatalog.

The composite framework workflow would create this service, registers it with the bootstrapper and any modules loaded after this fact would be able to use the IDockingWindowService. This service is not a view, but logic; just wanted to point out that distinction. That being said, a Module can contain 0 or more Views (or, as a simplification, UserControls). The unit of UI is the View. A Module is more of a logic and/or UI bundling concept.

Back to your particular question: What the documentation is saying is that if you use Regions to display your Views, you can register the View types with the Region. Whenever the Region is shown, it will automatically build the View using the Unity container.

哑剧 2024-07-22 02:54:07

当您向某个区域注册类型时,只要显示该区域,该类型就会被实例化。

如果您找到一个区域,然后向其中添加视图,则不需要向该视图注册类型,因为您正在做这项工作,而不是让区域管理器来做。

When you register a type with a Region that type is instatiated whenever the region is shown.

If you locate a region and then add views to it you don't need to register a type with that view, since you're doing the work rather than letting the region manager do it.

一桥轻雨一伞开 2024-07-22 02:54:07

在您的示例中,您将模块添加到应用程序模块,我将其视为带有实现 IModule 接口的类(模块初始化器类)的库的加载

每次调用该模块初始化器类的 Intialize 方法时,模块都会注册它自己的 IoC 映射,并且模块工作所需的其他东西。

现在,模块可以在模块初始化期间加载视图(添加菜单项或工具栏项等)。 这将涵盖您的问题的“在模块初始化期间添加一个或多个视图”部分。

除了在初始化期间显示视图之外,模块通常还包含更多视图,这些视图不会在模块加载时显示,而是通常对某些事件做出反应(UserLoogingIn 事件可能需要显示登录视图)。 为了让 Prism 显示视图,视图和表示模型之间的所有映射都必须在模块初始值设定项中定义。

像这样的东西(基于 RI 代码风格)

this.container.Register();
this.container.Register();

因此,模块初始化程序将通过定义统一所需的映射来注册视图,以在加载视图的区域管理器操作期间解析视图。

In your example you are adding module to application modules which I think of it as a loading of library with class implementing IModule interface (module initializer class)

Every time when Intialize method of that module initializer class is invoked, module register it's own IoC mappings and other things needed for module work.

Now, a module can load a view during the module initialziation, (adding the menu item or toolbar item etc). That would cover "adding of one or more views during the module initialization" part of your question.

Beside showing the views during the initialization, module usually contains more views which are not to be shown in the moment of module loading but instead usually in reaction to some event (UserLoogingIn event could requirel login vew to be shown). In order for Prism to show that view all of the mappings between the view and presentation model have to be defined already in module initializer.

Something like this (based on RI code style)

this.container.Register();
this.container.Register();

So, module initializer would register views by defining mappings needed for unity to resolve the view during the region manager operation of loading view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文