using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.ComponentModel.Composition.Primitives; using System.Linq; using System.Windows; using Caliburn.Micro; namespace AntdWpfDemo { internal class Bootstrapper : BootstrapperBase { private CompositionContainer container; public Bootstrapper() { Initialize(); } protected override void Configure() { var catalog = new AggregateCatalog( AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType() ); container = new CompositionContainer(catalog); var batch = new CompositionBatch(); batch.AddExportedValue(new WindowManager()); batch.AddExportedValue(new EventAggregator()); batch.AddExportedValue(container); container.Compose(batch); } protected override object GetInstance(Type service, string key) { string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(service) : key; var exports = container.GetExportedValues(contract); if (exports.Any()) { return exports.First(); } throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract)); } protected override IEnumerable GetAllInstances(Type service) { return container.GetExportedValues(AttributedModelServices.GetContractName(service)); } protected override void BuildUp(object instance) { container.SatisfyImportsOnce(instance); } protected override void OnStartup(object sender, StartupEventArgs e) { DisplayRootViewFor(); } } }