天天看點

使用Caliburn.Micro系列1:建立項目并引入CM

一、WPF的幾個MVVM模式實作

MVVMLight:小衆的平民架構,實作簡單粗暴。  pass:最近更新在15年

     官網: http://www.mvvmlight.net/

     最近一篇内容全面的好文: http://www.cnblogs.com/wzh2010/p/6920706.html

Caliburn.Micro:Caliburn的精簡版本,化繁為簡。引用官網原話:

  A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need   to sacrifice code quality or testability.

  -- 用于建立各類型的XAML平台應用的精簡而又強大的架構。強力支援MV*類的模式,使你的項目更快的建立,并且不犧牲代碼品質以及可測試性。

     Github: https://github.com/Caliburn-Micro/Caliburn.Micro

Prism: 高大上的牛*架構,具體還沒使用過,暫不發表評論了。

     Github:https://github.com/PrismLibrary/Prism

二、WPF項目中使用Caliburn.Micro

1、建立項目 Caliburn.Micro.Demo,并使用Nuget管理,查詢到 Caliburn.Micro.Start(這是作者提供用于快速生成基本配置檔案的),安裝。

使用Caliburn.Micro系列1:建立項目并引入CM
2、安裝完成後,項目中會多出如下圖示記檔案及引用
使用Caliburn.Micro系列1:建立項目并引入CM
3、此時啟動還不會使用CM,運作的還是預設的MainView.xaml 窗體。需要做一些簡單的修改。如下

/// <summary>
    /// App.xaml 的互動邏輯
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }
    }      
<Application x:Class="Caliburn.Micro.Demo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Caliburn.Micro.Demo">
    <!-- StartupUrl="MainWindow.xaml 移除,否則啟動頁不變"-->
        <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:AppBootstrapper x:Key="bootstrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>      

4、再次運作時,啟動界面就會變為CM架構生成的預設窗體 ShellView.xaml 。

5、預設使用的Ioc模式 SimpleContainer具體如何使用,不甚了解。所有還是換成微軟提供大家熟知的MEF。具體修改代碼如下:

使用Caliburn.Micro系列1:建立項目并引入CM
使用Caliburn.Micro系列1:建立項目并引入CM
public class AppBootstrapper : BootstrapperBase
    {
        CompositionContainer container;
        public AppBootstrapper()
        {
            Initialize();
        }
        protected override void Configure()
        {
            var catalog = new AggregateCatalog(
                AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>());
            this.container = new CompositionContainer(catalog);

            var batch = new CompositionBatch();      //如果還有自己的部件都加在這個地方
            batch.AddExportedValue<IWindowManager>(new WindowManager());
            batch.AddExportedValue<IEventAggregator>(new EventAggregator());
            batch.AddExportedValue(this.container);

            this.container.Compose(batch);
        }
        protected override object GetInstance(Type service, string key)
        {
            string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(service) : 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 service)
        {
            return container.GetExportedValues<object>(AttributedModelServices.GetContractName(service));
        }
        protected override void BuildUp(object instance)
        {
            container.SatisfyImportsOnce(instance);
        }
        protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
        {
            DisplayRootViewFor<IShell>();
        }
    }      

View Code 

使用Caliburn.Micro系列1:建立項目并引入CM
使用Caliburn.Micro系列1:建立項目并引入CM
using System.ComponentModel.Composition;

namespace Caliburn.Micro.Demo {
    [Export(typeof(IShell))]
    public class ShellViewModel : Caliburn.Micro.PropertyChangedBase, IShell { }
}      

View Code

6、OK,最後,看一下完整的啟動頁面。

使用Caliburn.Micro系列1:建立項目并引入CM

源碼位址:https://pan.baidu.com/s/1eRYKyvK