2017-08-07 4 views
0

J'ai quelques plugins d'extension dans le répertoire plugins. Je vais importer des plugins dans ViewModel et l'utiliser, mais je ne peux pas l'importer avec succès. Je crois avoir aucun moyen de configurer avec succès Configurer, demander des conseilsComment Caliburn.micro utilise MEF pour importer des plugins personnalisés

BootStrapper.

public class AppBootstrapper : BootstrapperBase 
{ 
    private CompositionContainer container; 

    public AppBootstrapper() 
    { 
     Initialize(); 
    } 

    protected override void Configure() 
    { 
     string pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins"); 
     if (!Directory.Exists(pluginPath)) 
      Directory.CreateDirectory(pluginPath); 

     var fi   = new DirectoryInfo(pluginPath).GetFiles("*.dll"); 
     AssemblySource.Instance.AddRange(fi.Select(fileInfo => Assembly.LoadFrom(fileInfo.FullName))); 

     var catalog  = new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()); 
     var batch   = new CompositionBatch(); 
     container   = new CompositionContainer(catalog); 

     batch.AddExportedValue(container); 
     batch.AddExportedValue<IWindowManager>(new WindowManager()); 

     batch.AddExportedValue<IEventAggregator>(new EventAggregator()); 
     batch.AddExportedValue(catalog); 

     container.Compose(batch); 
    } 

    protected override void BuildUp(object instance) 
    { 
     base.BuildUp(instance); 
    } 

    protected override object GetInstance(Type service, string key) 
    { 
     var contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(service) : key; 
     var exports = this.container.GetExportedValues<object>(contract); 

     if (exports.Any()) 
     { 
      return exports.First(); 
     } 

     throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract)); 
    } 

    protected override IEnumerable<object> GetAllInstances(Type service) 
    { 
     return this.container.GetExportedValues<object>(AttributedModelServices.GetContractName(service)); 
    } 

    protected override void OnStartup(object sender, StartupEventArgs e) 
    { 
     DisplayRootViewFor<IShell>(); 
    } 
} 

ViewModel

[ImportMany] 
IEnumerable<Lazy<IPlugin, IPluginsMetaData>> plugins; 

Répondre

0

En fait, mon Bootstrapper et pas de problème, le problème apparaît dans ma classe IPlugin. Dans le processus de recherche du problème, j'ai également trouvé la méthode Bootstrapper Configure d'une autre manière à écrire. Je vais mettre l'intégralité du code de plug-in sont affichés pour tout le monde de se référer à la prochaine ~

BootStrapper Configurer

protected override void Configure() 
{ 
    var catalog = new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()); 
    var batch = new CompositionBatch(); 
    var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins"); 

    if (!Directory.Exists(path)) 
     Directory.CreateDirectory(path); 

    catalog.Catalogs.Add(new DirectoryCatalog(path)); 
    container = new CompositionContainer(catalog); 

    batch.AddExportedValue(container); 
    batch.AddExportedValue<IWindowManager>(new WindowManager()); 

    batch.AddExportedValue<IEventAggregator>(new EventAggregator()); 
    batch.AddExportedValue(catalog); 

    container.Compose(batch); 
} 

ShellViewModel

[Export(typeof(IShell))] 
public class ShellViewModel : Conductor<object>, IShell 
{ 
    [ImportMany] 
    IEnumerable<Lazy<IPlugin, IPluginMetaData>> plugins; 
} 

IPlugin

public interface IPlugin 
{ 
    void Do(); 
} 

public interface IPluginMetaData 
{ 
    string Name { get; } 

    string Code { get; } 

    //[... more attribute ...] 
} 

[MetadataAttribute] 
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] 
public class PluginExportAttribute : ExportAttribute, IPluginMetaData 
{ 
    public string Name { get; set; } 

    public string Code { get; set; } 

    //[... more attribute ...] 

    public PluginExportAttribute() : base(typeof(IPlugin)) { } 
} 

PluginOne

[PluginExport(Name = "PluginOne", Code = "Key")] 
public class PluginOne : IPlugin 
{ 
    public void Do() 
    { 
     Console.WriteLine("I'm PluginOne"); 
    } 
}