一、WPF的几个MVVM模式实现
MVVMLight:小众的平民框架,实现简单粗暴。 pass:最近更新在15年
最近一篇内容全面的好文: 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>(); } }
using System.ComponentModel.Composition; namespace Caliburn.Micro.Demo { [Export(typeof(IShell))] public class ShellViewModel : Caliburn.Micro.PropertyChangedBase, IShell { } }
6、OK,最后,看一下完整的启动页面。