我有一個簡單的容器層次結構,它具有在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模板項目:(請參閱編輯) –