如何在PRISM中获取模块实例 [英] How to get a module instance in PRISM

查看:89
本文介绍了如何在PRISM中获取模块实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PRISM桌面应用程序,该应用程序在DirectoryModuleCatalog的帮助下从目录加载模块.

I have a PRISM desktop app which loads modules from a directory with the help of the DirectoryModuleCatalog.

一切都很好,但是我找不到找到已加载模块实例的方法. IModuleManager和IModuleCatalog没有类似 getInstance(ModuleInfo)之类的方法.

Everything is fine, except that I cannot find a way to get the instance of a loaded module. IModuleManager and IModuleCatalog don't have a method like getInstance(ModuleInfo) or similar.

请参见

moduleManager.LoadModule(moduleInfo.ModuleName);

此行正确加载了模块(moduleManager为IModuleManager类型),但是接下来我该怎么做才能获取此模块的实际实例?

This line loads the module properly(moduleManager is of type IModuleManager), but what do I have to do next to get the actual instance of this module?

模块是根据用户需求加载的,因此我无法在启动时在引导程序中注册所有模块.

The modules are loaded on demand of the user, so I cannot register all modules at startup within the bootstrapper.

推荐答案

如果按模块实例的意思是实现IModule的类,则必须将实例显式注册到容器中才能获取它.

If by Module instance you mean the class that implement IModule, then you must explicitly register the instance into the container to be able to get it.

尽管上述方法可行,但您根本不应该采用这种方法.想法是模块类特定于特定模块,并且仅应用于模块初始化目的.

Although the aforementioned will work, you should not take that approach at all. The idea is that the module classes are specific to a particular module and should only be used for module initialization purposes.

我将每个模块的Start方法放在一个单独的组件(IStartable)中,使用不同的ID在容器中注册每个组件,并解析/导入IEnumerable以获取具有start方法的所有实例.

I would place each module's Start method in a separate component (IStartable), register each component in the container with a different Id and resolve/import an IEnumerable to get all instances that have the start method.

希望这会有所帮助

这篇关于如何在PRISM中获取模块实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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