在我的Prism 4 MEF應用程序中,我無法通過bootstrapper實例訪問Container(容器屬性受保護)。
對於這樣的功能,我在我的bootstrapper類中創建了特殊的方法來獲取或設置需要的對象,如記錄器或其他東西。
在卡梅倫變型的情況下,它看起來像這樣:
public partial class App : Application
{
private Bootstrapper _bootstrapper;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
_bootstrapper = new MyBootstrapper();
_bootstrapper.Run();
}
protected override void OnExit(ExitEventArgs e)
{
ILoggerFacade logger = bootstrapper.GetLogger();
logger.Log("Application Exitting", Category.Info, Priority.Low);
base.OnExit(e);
}
}
class MyBootstrapper : MefBootstrapper
{
public ILoggerFacade GetLogger()
{
return Container.GetExportedValue<ILoggerFacade>();
// as for Logger you can get it by property of Bootstrapper class:
// return Logger;
}
...
}