高一一班

Caliburn.Micro CM框架最简洁入门教程

2019-03-20  本文已影响173人  psmyfish

Caliburn.Micro CM框架最简洁入门教程

新建WPF工程

首先nuget导入caliburn.micro ,然后reference 导入System.ComponentModel.Composition

删除MainWindow

添加Views文件夹,添加PView.xaml窗口或者用户控件

添加ViewModels文件夹,添加PViewModel.cs

namespace ERP_A2_p.ViewModels
{
    using System.ComponentModel.Composition;

    using Caliburn.Micro;
    
    public class PViewModel:Screen
    {
    }
}

配置App.xaml

<Application x:Class="ERP_A2_p.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:ERP_A2_p">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:AppBootstrapper x:Key="bootstrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

配置AppBootstrapper(此中带IOC配置,配置后可以自己建立文件夹放置Views和ViewModels,不然就只能把Views和Bootstrapper放在同一目录下了~)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ERP_A2_p
{
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.ComponentModel.Composition.Primitives;
    using System.Windows;

    using Caliburn.Micro;

    using ERP_A2_p.ViewModels;

    public class AppBootstrapper : BootstrapperBase
    {
        private CompositionContainer _container;
        public AppBootstrapper()
        {
            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)
        {
            //new Summary.Controls.AppBootstrapper().Initialize();
            DisplayRootViewFor<PViewModel>();
        }
    }
}

这种状态能够找到Views里面的View ,但是不能找到初始窗口的ViewModel

Could not locate any instances of contract xxx.ViewModel

此时在PViewModel中加上:

[Export(typeof(PViewModel))]
namespace ERP_A2_p.ViewModels
{
    using System.ComponentModel.Composition;

    using Caliburn.Micro;

    [Export(typeof(PViewModel))]
    public class PViewModel:Screen
    {
    }
}

搞定
需要windowManager和eventAggregator时候

private IWindowManager _windowManager;
        [ImportingConstructor]
        public MainViewModel(IWindowManager windowManager, IEventAggregator eventAggregator)
        {
            _windowManager = windowManager;
        }

关于库管理和引用
新建一个库项目ALibrary
建立AllReference资源词典来放置管理所有需要输出的引用

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Styles/AColor.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

然后在工作项目中引用它就可以啦

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:AppBootstrapper x:Key="bootstrapper" />
                </ResourceDictionary>
                <ResourceDictionary
                    Source="pack://application:,,,/ALibrary;component/AllReference.xaml">
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
上一篇下一篇

猜你喜欢

热点阅读