【发布时间】:2011-09-29 05:43:59
【问题描述】:
我有一个简单的容器层次结构,其中一个父容器使用目录目录在 Shell MEFBootstrapper 中定义,子容器使用不同的目录从父容器中创建。
我的子容器也使用 DirectoryCatalog(与父容器不同的路径),我可以看到容器在运行时具有装配和零件信息。
但是,位于子容器中的模块的 Initialize() 方法永远不会被调用。
我的目标是将子容器用作会话构造,允许用户创建新会话并在它们之间切换。但是,如果我无法初始化组成模块(并将它们的视图放入区域中),我就会陷入困境。
我曾想过使用事件聚合器从我的会话管理器中引发一个事件,以允许模块侦听该事件并自行初始化,但这似乎也不起作用。
我。为什么在加载到子容器中的模块上不调用 Initialize ii.如何从容器实例“触发”初始化(在模块上下文之外?)您可以遍历容器中的程序集并以这种方式触发初始化吗???
[来自 shell 项目中的 MefBootstrapper]
protected override DependencyObject CreateShell()
{
ExportProvider ep = this.Container as ExportProvider;
this.Container.ComposeExportedValue(ep);
[来自管理我的会话(容器)的服务]
[ImportingConstructor]
public SessionService(ExportProvider provider)
{
[新会话(容器)的构造函数]
private void Init(ComposablePartCatalog catalog, ExportProvider provider, string name, int callId, bool useContextProxy)
{
this._Name = name;
this._CallID = callId;
this.startTime = DateTime.Now;
this.appHost = new CompositionContainer(catalog, new ExportProvider[] { provider });
}
=====
被要求包含未调用初始化方法的模块代码(尽管被加载到有问题的容器中......我什至可以懒惰地实例化模块,但直接调用 Initialize() 会导致注入操作在方法正确。
namespace Module1
{
//, InitializationMode = InitializationMode.OnDemand
[ModuleExport("Module1.ModuleInit", typeof(Module1.ModuleInit))]
public class ModuleInit : IModule
{
private readonly IRegionManager _regionManager;
public IServiceLocator _serviceLocator;
[ImportingConstructor]
public ModuleInit(IRegionManager regionManager, IServiceLocator serviceLocator)
{
_regionManager = regionManager;
_serviceLocator = serviceLocator;
}
#region IModule Members
public void Initialize()
{
// Use View Discovery to automatically display the MasterView when the TopLeft region is displayed.
_regionManager.RegisterViewWithRegion(RegionNames.TopLeftRegion, () => _serviceLocator.GetInstance<MasterView>());
}
#endregion
}
}
【问题讨论】:
-
你能展示一个带有
Initialize方法的示例模块吗? -
当然,非常简单,直接来自 VS2010 中的 PRISM 模板项目:(见编辑)
标签: module prism mef containers hierarchy