Caliburn.Micro MEF Bootstrapper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using Caliburn.Micro;
using CMApplication1.ViewModels;
using System.Windows;
namespace CMApplication1
{
    //public class MefBootstrapper : BootstrapperBase
    //{
    //    private CompositionContainer container;
    //    public MefBootstrapper()
    //    {
    //        Initialize();
    //    }
    //    protected override void Configure()
    //    {
    //        container = new CompositionContainer(
    //            new AggregateCatalog(
    //                AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
    //                )
    //            );
    //        var batch = new CompositionBatch();
    //        batch.AddExportedValue<IWindowManager>(new WindowManager());
    //        batch.AddExportedValue<IEventAggregator>(new EventAggregator());
    //        batch.AddExportedValue(container);
    //        container.Compose(batch);
    //    }
    //    protected override object GetInstance(Type serviceType, string key)
    //    {
    //        var contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
    //        var exports = 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 serviceType)
    //    {
    //        return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
    //    }
    //    protected override void BuildUp(object instance)
    //    {
    //        container.SatisfyImportsOnce(instance);
    //    }
    //    protected override void OnStartup(object sender, StartupEventArgs e)
    //    {
    //        DisplayRootViewFor<IShell>();
    //    }
    //}
    /// <summary>
    /// Model First
    /// </summary>
    public class MefBootstrapper : BootstrapperBase
    {
        /// <summary>
        /// 2.0 必须显式加上构造函数
        /// </summary>
        public MefBootstrapper()
        {
            Initialize();
        }
        private CompositionContainer _container;
        protected override void Configure()
        {
            _container = new CompositionContainer(new AggregateCatalog(
                AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
                ));
            var batch = new CompositionBatch();
            batch.AddExportedValue<IWindowManager>(new WindowManager());
            batch.AddExportedValue<IEventAggregator>(new EventAggregator());
            batch.AddExportedValue(_container);
            _container.Compose(batch);
            //AssemblyCatalog
            //var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            //DirectoryCatalog
            //var catalog = new DirectoryCatalog("Extensions");
            //AggregateCatalog
            //var catalog = new AggregateCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()), new DirectoryCatalog("Extensions"));
            //TypeCatalog
            //FilteredCatalog
        }
        protected override object GetInstance(Type serviceType, string key)
        {
            string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
            var exports = _container.GetExportedValues<object>(contract);
            if (exports.Count() > 0)
                return exports.First();
            throw new Exception(string.Format("无法创建契约 {0} 的实例", contract));
        }
        protected override IEnumerable<object> GetAllInstances(Type serviceType)
        {
            return _container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
        }
        protected override void BuildUp(object instance)
        {
            _container.SatisfyImportsOnce(instance);
        }
        protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
        {
            DisplayRootViewFor<IShell>();
        }
    }
    /// <summary>
    /// View First
    /// </summary>
    //public class MefBootstrapper : Bootstrapper<IShell>
    //{
    //    private CompositionContainer _container;
    //    protected override void Configure()
    //    {
    //        _container = new CompositionContainer(new AggregateCatalog(
    //            AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
    //            ));
    //        var batch = new CompositionBatch();
    //        batch.AddExportedValue<IWindowManager>(new WindowManager());
    //        batch.AddExportedValue<IEventAggregator>(new EventAggregator());
    //        batch.AddExportedValue(_container);
    //        _container.Compose(batch);
    //    }
    //    protected override object GetInstance(Type serviceType, string key)
    //    {
    //        string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
    //        var exports = _container.GetExportedValues<object>(contract);
    //        if (exports.Count() > 0)
    //            return exports.First();
    //        throw new Exception(string.Format("无法创建契约 {0} 的实例", contract));
    //    }
    //    protected override IEnumerable<object> GetAllInstances(Type serviceType)
    //    {
    //        return _container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
    //    }
    //    protected override void BuildUp(object instance)
    //    {
    //        _container.SatisfyImportsOnce(instance);
    //    }
    //    protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
    //    {
    //        var windowManager = IoC.Get<IWindowManager>();
    //        windowManager.ShowWindow(new ShellViewModel());
    //    }
    //}
}


知识共享许可协议
《Caliburn.Micro MEF Bootstrapper》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     167.95ms