我有一个简单的容器层次结构,它具有在Shell MEFBootstrapper中使用Directory目录定义的父容器,以及使用不同目录创建父容器的子容器。MEF子容器模块未初始化
我的子容器也使用DirectoryCatalog(与父级不同的路径),我可以看到容器在运行时有装配和零件信息。
但是,位于子容器中的模块的Initialize()方法永远不会被调用。
我的目标是使用子容器作为会话结构,允许用户创建新会话并在它们之间切换。但是,如果我不能让组成模块初始化(并将他们的观点放入区域),我就会陷入困境。
我曾经想过使用事件聚合器从会话管理器中引发一个事件,以允许模块监听事件并自行初始化,但这似乎也不起作用。
i。为什么不初始化调用加载到子容器中的模块 ii。我怎样才能“触发”从容器实例初始化(在模块上下文之外?)你可以迭代容器中的程序集并触发初始化吗?
[从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方法没有被调用(尽管被加载到问题的容器中......我甚至可以懒惰实例化模块,但直接调用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模板项目:(请参阅编辑) –