使用Caliburn.Micro系列1:新建项目并引入CM
一、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,最后,看一下完整的启动页面。
源码地址:https://pan.baidu.com/s/1eRYKyvK
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)