我很难理解为什么我们需要 ComposeExportedValue(objval) 而不仅仅是使用 [Export] 属性。
我有一个在 shell 中创建的应用程序对象,这个应用程序对象需要注入到 prism 模块中。
public class ShellBootsrapper : MefBootstrapper
{
[Export(typeof(IMyApplication))]
public MyApplication myApp;
protected override DependencyObject CreateShell()
{
this.Container.ComposeExportedValue<IMyApplication>(myApp);
return this.Container.GetExportedValue<Shell>();
}
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
myApp = new MyApplication();
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Module1.Module1).Assembly));
}
}
模块 1 只能在我使用时导入ComposeExportedValue<IMyApplication>(myApp);
[ModuleExport(typeof(Module1))]
public class Module1 : IModule
{
private readonly IRegionManager regionManager;
[Import]
private IMyApplication myApp;
}
我希望 [Export] 就足够了,但显然不是?
编辑:
我public MyApplication myApp;
从引导程序中删除了 shell.xaml.cs(more sensible) ,一切正常。我得出结论;MEF 组合正在进行中,导出根本不起作用。这就是 prism 内部库使用 ComposeExportedValue(object val) 进行导出的原因
protected virtual void RegisterBootstrapperProvidedTypes()
{
this.Container.ComposeExportedValue<ILoggerFacade>(this.Logger);
this.Container.ComposeExportedValue<IModuleCatalog>(this.ModuleCatalog);
this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocatorAdapter(this.Container));
this.Container.ComposeExportedValue<AggregateCatalog>(this.AggregateCatalog);
}