简体   繁体   中英

WPF+PRISM+MEF initializing DownloadedPartCatalogCollection

I am trying to initialize a module from my MefBootStrapper implementation

Type type = typeof(OrderDetailsModule.OrderDetailsModule);

ModuleInfo mi = new ModuleInfo {
    ModuleName = type.Name,
    Ref = new Uri(type.Assembly.Location, UriKind.RelativeOrAbsolute).AbsoluteUri,
        InitializationMode =InitializationMode.WhenAvailable,
        ModuleType = type.AssemblyQualifiedName
    };

this.ModuleCatalog.AddModule(mi);

I get an error

failed to load type for module OrderDetailsModule. \\r\\nError was: Unable to locate the module with type 'OrderDetailsModule.OrderDetailsModule, OrderDetailsModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' among the exported modules. Make sure the module name in the module catalog matches that specified on ModuleExportAttribute for the module type.."

Digging down Prism, in MefModuleInitializer, there is an if (this.downloadedPartCatalogs.TryGet(moduleInfo, out partCatalog)) and the downloaded parts is empty. I can see in the MefModuleInitializer class, downloadedPartCatalogs is injected via the ImportingConstructor attribute.

This is my OrderDetailsModule class

[Export("OrderDetailsModule")]
public class OrderDetailsModule
{
}

Question is, where do i export the downloadedPartCatalogs?

Your module class must implement the IModule interface and be attributed with the ModuleExportAttribute.

[ModuleExport(typeof(ModuleD))]
public class ModuleD : IModule 
{...}

Or use the AssemblyCatalog to automatically to discover and load modules.

protected override void ConfigureAggregateCatalog()
{
    base.ConfigureAggregateCatalog();

    this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleA).Assembly));
    this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleC).Assembly));
    . . .
}

Please read the Prism documentation: https://github.com/PrismLibrary/Prism/blob/master/Documentation/WPF/30-ModularApplicationDevelopment.md#modules-in-mef

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM