之前时间一直在使用Caliburn.Micro这种应用了MVVM模式的WPF框架做开发,是时候总结一下了。

Caliburn.Micro(https://blog.csdn.net/lzuacm/article/details/78886436)是一个轻量级的WPF框架,简化了WPF中的不少用法,推荐做WPF开发时优先使用。

真正快速而熟练地掌握一门技术就可以尝试着用最快的速度去构建一个玩具项目(Toy project),然后不断地优化、重构之。比如本文将介绍如何使用Caliburn.Micro v3.2开发出一个简单的计算器,里面用到了C#中的async异步技术,Caliburn.Micro中的Conductor等等~

>>>1.在VS中创建WPF项目<<<

>>>2.使用NuGet包管理工具为当前项目安装Caliburn.Micro <<<

对于Caliburn.Micro 1.x和2.x版,只能使用.dll,需手动给项目加Reference。而3.0以后的版本可使用NuGet包管理工具来管理,安装和卸载既方便又彻底,推荐使用。(ps: NuGet之于Visual Studio(C++, C#等), 犹pip之于Python, npm之于node, maven之于Java, gem之于Ruby等等)

>>>3.框架搭建  <  <  <

  1. 删除项目根目录下的MainWindow.xaml

  2. 按下图调整 App.xaml
    删除语句

    StartupUri="MainWindow.xmal"。

  3. 填充Application.Resources

    <Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary><local:Bootstrapper x:Key="bootstrapper"/></ResourceDictionary></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>

4 . 创建Bootstrapper类
然后让其继承自BootstrapperBase类,并加上构造函数,另外再重写函数OnStartup即可。


using System.Windows;
using Caliburn.Micro;
using CaliburnMicro_Calculator.ViewModels;

namespace CaliburnMicro_Calculator
{
public class Bootstrapper : BootstrapperBase
{
public Bootstrapper()
{
Initialize();
}

protected override void OnStartup(object obj, StartupEventArgs e)
{
DisplayRootViewFor<ShellViewModel>();
}
}
}

5 . 在项目目录下新建Models, ViewModels, Views这3个文件夹
在ViewModel文件夹中添加ShellViewModel.cs,并创建Left, Right和Result这3个属性。

需要注意的是 ShellViewModel.cs需要继承类

Screen 和 INotifyPropertyChanged(用于感知并同步所绑定属性的变化),ShellViewModel具体代码为:

using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using Caliburn.Micro;

namespace CaliburnMicro_Calculator.ViewModels
{
public class ShellViewModel : Screen, INotifyPropertyChanged
{
private double _left;
private double _right;
private double _result;

public double Left
{
get { return _left; }
set
{
_left = value;
NotifyOfPropertyChange();
}
}

public double Right
{
get { return _right; }
set
{
_right = value;
NotifyOfPropertyChange();
}
}

public double Result
{
get { return _result; }
set
{
_result = value;
NotifyOfPropertyChange();
}
}
}

说明: 最开始布局xaml时,设计位置时采用的是左(operand 1), 中(operand 2), 右(result),于是属性值使用了Left, Right和Result。

>>>4.设计XAML并绑定属性 <   <  <

在Views文件夹中创建Window,命名为ShellView.xaml,在Views文件夹下创建子文件夹Images,用于存放+,-,*,/这4种操作对应的小图标,其具体代码如下:

<Window x:Class="CaliburnMicro_Calculator.Views.ShellView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:CaliburnMicro_Calculator.Views"xmlns:cal="http://www.caliburnproject.org"mc:Ignorable="d"Title="Calculator" SizeToContent="Height" Width="240">

<StackPanel Background="Beige"><StackPanel Orientation="Horizontal"><Label Margin="10"Target="{Binding ElementName=left}">                Operand _1:</Label><TextBox Margin="10"Width="72"x:Name="left"/></StackPanel><StackPanel Orientation="Horizontal"><Label Margin="10"Target="{Binding ElementName=right}">                Operand _2:</Label><TextBox Margin="10"Width="72"x:Name="right"/></StackPanel><StackPanel Orientation="Horizontal"><Button Margin="10"x:Name="btnPlus" cal:Message.Attach="[Event Click]=[Action Plus(left.Text, right.Text):result.Text]"><Image Source="Images/op1.ICO"/></Button>

<Button Margin="10"x:Name="btnMinus" cal:Message.Attach="[Event Click]=[Action Minus(left.Text, right.Text):result.Text]"><Image Source="Images/op2.ICO"/></Button>

<Button Margin="10"x:Name="btnMultiply" cal:Message.Attach="[Event Click]=[Action Multipy(left.Text, right.Text):result.Text]"><Image Source="Images/op3.ICO"/></Button>

<Button Margin="10"x:Name="btnDivide" IsEnabled="{Binding Path=CanDivide}"cal:Message.Attach="[Event Click]=[Action Divide(left.Text, right.Text):result.Text]"><Image Source="Images/op4.ICO"/></Button>

</StackPanel><StackPanel Orientation="Horizontal"><Label Margin="10">                Answer:</Label><TextBox Margin="10"Width="72"Text ="{Binding Path=Result, StringFormat={}{0:F4}}" IsReadOnly="True" /></StackPanel></StackPanel></Window>

说明:对操作数Operand _1和Operand _2,按Alt键+数字可以选中该处,这是WPF的一个特殊用法。由于计算结果不希望被修改,于是加上了属性IsReadOnly="True"

>>>5.设计并绑定事件  <  <  <

由于暂时只打算实现+, -, *, /四种操作,于是我们只需创建相应的4个函数即可,由于除数是0这个操作不允许,于是需再加个判断函数CanDivide。

Caliburn.Micro中绑定事件的写法是:
cal:Message.Attach="[Event E]=[Action A]"

(E是操作,比如Click, MouseDown, KeyDown等等,A是ViewModel中具体的函数。)

向ShellViewModel中加入事件中要做的事,此时ShellViewModel为:

using System.ComponentModel;using System.Threading;using System.Windows;using System.Windows.Controls;using Caliburn.Micro;

namespace CaliburnMicro_Calculator.ViewModels{public class ShellViewModel : Screen, INotifyPropertyChanged    {private double _left;private double _right;private double _result;

public double Left        {get { return _left; }set            {                _left = value;                NotifyOfPropertyChange();            }        }

public double Right        {get { return _right; }set            {                _right = value;                NotifyOfPropertyChange();            }        }

public double Result        {get { return _result; }set            {                _result = value;                NotifyOfPropertyChange();            }        }public bool CanDivide(double left, double right){return right != 0;        }

public async void Divide(double left, double right){            Thread.Sleep(600);if (CanDivide(left, right) == true)                Result = left / right;else MessageBox.Show("Divider cannot be zero.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);        }

public async void Plus(double left, double right){            Result = left + right;        }

public async void Minus(double left, double right){            Result = left - right;        }

public async void Multipy(double left, double right){            Result = left * right;        }    }}

此时计算器的功能已基本完成,但我们可以对ViewModel进行适当的调整:
1.创建新的ViewModel - CalculatorViewModel,将原来的ShellViewModel中具体的计算逻辑移入到CalculatorViewModel中;
2.此时让ShellViewModel继承Conductor<Object>,于是ShellViewModel拥有了管理Screen实例的功能(ViewModel中使用ActivateItem函数,而View中使用X:Name="ActivateItem"标签),其具体代码为:

using System.ComponentModel;using System.Threading;using System.Windows;using System.Windows.Controls;using Caliburn.Micro;

namespace CaliburnMicro_Calculator.ViewModels{public class ShellViewModel : Conductor<object>    {public ShellViewModel(){        }public void ShowCalculator(){            ActivateItem(new CalculatorViewModel());        }    }}

此时,CalculatorViewModel的具体代码为:

using System.ComponentModel;using System.Threading;using System.Windows;using Caliburn.Micro;

namespace CaliburnMicro_Calculator.ViewModels{public class CalculatorViewModel: Screen, INotifyPropertyChanged    {private double _left;private double _right;private double _result;

public double Left        {get { return _left; }set            {                _left = value;                NotifyOfPropertyChange();            }        }

public double Right        {get { return _right; }set            {                _right = value;                NotifyOfPropertyChange();            }        }

public double Result        {get { return _result; }set            {                _result = value;                NotifyOfPropertyChange();            }        }

public CalculatorViewModel(){        }

public bool CanDivide(double left, double right){return right != 0;        }

public async void Divide(double left, double right){            Thread.Sleep(600);if (CanDivide(left, right) == true)                Result = left / right;else MessageBox.Show("Divider cannot be zero.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);        }

public async void Plus(double left, double right){            Result = left + right;        }

public async void Minus(double left, double right){            Result = left - right;        }

public async void Multipy(double left, double right){            Result = left * right;        }    }}

3 . 对于View,只需把CalculatorViewModel对应的CalculatorView作为ContentControl控件嵌入ShellView即可。此时ShellView的代码调整为:

<Window x:Class="CaliburnMicro_Calculator.Views.ShellView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:CaliburnMicro_Calculator.Views"xmlns:cal="http://www.caliburnproject.org"mc:Ignorable="d"Title="Calculator" SizeToContent="Height" Width="240">

<Grid MinHeight="200"><Button Content="Show Calculator" x:Name="ShowCalculator" Grid.Row="0"></Button><ContentControl x:Name="ActiveItem"></ContentControl>        </Grid></Window>

另外提一点,向ViewModel A中嵌入ViewModel B,一般来说需要做的操作是:
在A的view中使用ContentControl,绑定B的ViewModel只需使用语句cal:View.Model="{Binding BViewModel}"即可,而B的view是UserControl就可以啦。

此时CalculatorView是一个UserControl,其代码为:

<UserControl x:Class="CaliburnMicro_Calculator.Views.CalculatorView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="clr-namespace:CaliburnMicro_Calculator.Views"xmlns:cal="http://www.caliburnproject.org"mc:Ignorable="d"Width="240">

<StackPanel Background="Beige"><StackPanel Orientation="Horizontal"><Label Margin="10"Target="{Binding ElementName=left}">                Operand _1:</Label><TextBox Margin="10"Width="72"x:Name="left"/></StackPanel><StackPanel Orientation="Horizontal"><Label Margin="10"Target="{Binding ElementName=right}">                Operand _2:</Label><TextBox Margin="10"Width="72"x:Name="right"/></StackPanel><StackPanel Orientation="Horizontal" HorizontalAlignment="Center"><Button Margin="10"x:Name="btnPlus" cal:Message.Attach="[Event Click]=[Action Plus(left.Text, right.Text):result.Text]"><Image Source="Images/op1.ICO"/></Button>

<Button Margin="10"x:Name="btnMinus" cal:Message.Attach="[Event Click]=[Action Minus(left.Text, right.Text):result.Text]"><Image Source="Images/op2.ICO"/></Button>

<Button Margin="10"x:Name="btnMultiply" cal:Message.Attach="[Event Click]=[Action Multipy(left.Text, right.Text):result.Text]"><Image Source="Images/op3.ICO"/></Button>

<Button Margin="10"x:Name="btnDivide" IsEnabled="{Binding Path=CanDivide}"cal:Message.Attach="[Event Click]=[Action Divide(left.Text, right.Text):result.Text]"><Image Source="Images/op4.ICO"/></Button>

</StackPanel><StackPanel Orientation="Horizontal"><Label Margin="10">                Answer:</Label><TextBox Margin="10"Width="72"Text ="{Binding Path=Result, StringFormat={}{0:F4}, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" /></StackPanel></StackPanel></UserControl>

好啦,就酱,由于本例中逻辑并不复杂,Model暂时用不上,对于复杂一点的项目,Model主要负责数据的读取,如文件操作、数据库操作、service调用等,以后有机会举例具体来说。

如果需要持久化(persistent),则还需给给每对M-VM(Model和ViewModel)加入State,这个实际工程中也用得特别多。

>>>6.功能举例  <  <  <

Calculator主页:

点击按钮“ShowCalculator”即可看到具体的计算器~

乘法举例:

除法举例:

最后附上代码:
CaliburnMicro-Calculator: A simple Calculator using Caliburn.Micro
https://github.com/yanglr/CaliburnMicro-Calculator,
欢迎fork和star,如有改进意见欢迎提交pull request~

原文地址:

https://blog.csdn.net/lzuacm/article/details/80559517

更多精彩文章,欢迎访问本人博客https://enjoy233.cnblogs.com 或 知乎搜索Bravo Yeung.

欢迎转发到朋友圈,公众号转载请后台联系本人申请授权~

推荐阅读

中英文电子书下载网站大搜罗

英语语法工具 | 那些可以纠正英语文章中语法的神器们

开发者见闻 | ASP.NET Core开发者路线图

点击在看的人,

2019都会变得特别好看

WPF框架教程 | 从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器相关推荐

  1. 从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器

    从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器 这段时间一直在使用Caliburn.Micro这种应用了MVVM模式的WPF框架做开发,是时候总结一下了. Calibu ...

  2. WPF入门教程系列(一) 创建你的第一个WPF项目

    WPF基础知识 快速学习绝不是从零学起的,良好的基础是快速入手的关键,下面先为大家摞列以下自己总结的学习WPF的几点基础知识: 1) C#基础语法知识(或者其他.NET支持的语言):这个是当然的了,虽 ...

  3. WPF入门教程-转载

    最近为了做炫酷的UI,了解了WPF,之前一直是使用winform的,界面也是古老的不行. 在园里找到了一个大佬以前写的教程,备注一下.按照系列教程走下来,可以直接上手了. 备忘传送门>>& ...

  4. C# WPF MVVM开发框架Caliburn.Micro 关于Conventions⑧

    01 - 关于Conventions Caliburn.Micro的一个主要特性是,它能够通过一系列约定消除对锅炉铭牌代码的需求.有些人喜欢习俗,有些人讨厌习俗.这就是为什么CM的约定是完全可定制的, ...

  5. C# WPF MVVM开发框架Caliburn.Micro自定义引导程序④

    01 - 自定义引导程序 在上一部分中,我们讨论了Caliburn.Micro WPF应用程序的最基本配置,并演示了与操作和约定相关的两个简单功能.在这一部分中,我想进一步探讨Bootstrapper ...

  6. 【愚公系列】2023年02月 .NET CORE工具案例-Caliburn.Micro的使用基于WPF的改造的MVVM案例

    文章目录 前言 1.Caliburn.Micro是什么 2.Caliburn.Micro的主要功能 一.Caliburn.Micro的使用基于WPF的改造 1.项目介绍 2.安装软件包 3.改造App ...

  7. Caliburn.Micro 杰的入门教程3,事件和参数

    Caliburn.Micro 杰的入门教程1(翻译) Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译) Caliburn.Micro 杰的入门教程 ...

  8. C#/WPF入门到多项目实战开发教程1——Grid、自定义按钮模板、WPF框架中的动画

    视频地址:https://www.bilibili.com/video/BV13U4y1e7fx?p=8&spm_id_from=pageDriver&vd_source=5dc01f ...

  9. WPF系列教程——(一)仿TIM QQ界面 - 简书

    WPF系列教程--(一)仿TIM QQ界面 - 简书 原文: WPF系列教程--(一)仿TIM QQ界面 - 简书 TIM QQ 我们先来看一下TIM QQ长什么样,整体可以将界面分为三个部分 TIM ...

最新文章

  1. 赠书 | 熵的实际应用,赌场和金融圈最著名的一个数学公式
  2. NDoc –NET 代码文档生成器快速度上手
  3. 来自一年的程序员的困惑
  4. TCP和UDP的区别(Socket)
  5. Spring Cloud微服务之模块依赖修改(六)
  6. 愿望满足系统 1020 分支与循环控制
  7. 在ubuntu下面安装glew
  8. NYOJ最长公共子序列(dp)
  9. [转载] python的__del__()方法
  10. 编写程序对给定的有向图(不一定连通)进行深度优先遍历_从零开始学习数据结构gt;图的非连通遍历...
  11. win定时关机_电脑快速关机的8种方法,很多人都不知道!
  12. 如何安装husky_Ubuntu 14.04 编译安装 husky
  13. 如何在电脑表格中用计算机,如何制表(如何使用电脑制作表格)
  14. 【梳理】离散数学 第10章 群与环 10.2 子群与群的陪集分解
  15. 分类,等级,或者有序变量如何进行多因素Cox回归 变量的类型决定了最终结果的reference
  16. 2019年DNS服务器速度排行榜
  17. .bss段和.data段引起的文件大小增加
  18. 阿里面试——机器学习岗四个面试案例
  19. 物联网大赛 - Android学习笔记(三)Android 事件处理
  20. python 爬网页通知_用Python实现一个爬取XX大学电费通知的小脚本

热门文章

  1. 书评:Just the Computer Essentials(Vista)
  2. Linux学习笔记之一————什么是Linux及其应用领域
  3. 苹果、联想及华硕均看准美国电脑运输的增长
  4. Mac怎么不能拷贝文件到U盘
  5. jquery mobile页面切换效果(Flip toggle switch)(注:jQuery移动使用的数据属性的列表。 )...
  6. linux视频教程之vsftp_B
  7. 《The C++ Standard Library》第50页 关于传递auto_ptr的问题
  8. 星跃计划 | 新项目持续招募中!MSR Asia-MSR Redmond 联合科研计划邀你申请!
  9. Dapr + .NET 实战(十四)虚拟机集群部署 mDNS + Consul
  10. Flurl使用Polly实现重试Policy