0

我很难理解为什么我们需要 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);
}
4

1 回答 1

0

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);
}

我还发现 ComposeExportedValue 的目的之一是进行受控出口;即配置对象,设置属性等,然后将其导出。否则 MEF 将仅导出创建一个实例。

于 2015-05-18T22:07:17.987 回答