使用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(這是作者提供用於快速生成基本配置文件的),安裝。

 

 

2、安裝完成后,項目中會多出如下圖標記文件及引用

 

 

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。具體修改代碼如下:

 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 

 

using System.ComponentModel.Composition;

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

 

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

 

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM