太原网站制作哪里便宜,那曲做网站,怎么制作古装视频,网站用户维度之前时间一直在使用Caliburn.Micro这种应用了MVVM模式的WPF框架做开发#xff0c;是时候总结一下了。Caliburn.Micro(https://blog.csdn.net/lzuacm/article/details/78886436)是一个轻量级的WPF框架#xff0c;简化了WPF中的不少用法#xff0c;推荐做WPF开发时优先使用。真… 之前时间一直在使用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.框架搭建 删除项目根目录下的MainWindow.xaml按下图调整 App.xaml删除语句StartupUriMainWindow.xmal。填充Application.Resources Application.ResourcesResourceDictionaryResourceDictionary.MergedDictionariesResourceDictionarylocal:Bootstrapper x:Keybootstrapper//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){ DisplayRootViewForShellViewModel(); } }} 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:ClassCaliburnMicro_Calculator.Views.ShellViewxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:CaliburnMicro_Calculator.Viewsxmlns:calhttp://www.caliburnproject.orgmc:IgnorabledTitleCalculator SizeToContentHeight Width240StackPanel BackgroundBeigeStackPanel OrientationHorizontalLabel Margin10Target{Binding ElementNameleft} Operand _1:/LabelTextBox Margin10Width72x:Nameleft//StackPanelStackPanel OrientationHorizontalLabel Margin10Target{Binding ElementNameright} Operand _2:/LabelTextBox Margin10Width72x:Nameright//StackPanelStackPanel OrientationHorizontalButton Margin10x:NamebtnPlus cal:Message.Attach[Event Click][Action Plus(left.Text, right.Text):result.Text]Image SourceImages/op1.ICO//ButtonButton Margin10x:NamebtnMinus cal:Message.Attach[Event Click][Action Minus(left.Text, right.Text):result.Text]Image SourceImages/op2.ICO//ButtonButton Margin10x:NamebtnMultiply cal:Message.Attach[Event Click][Action Multipy(left.Text, right.Text):result.Text]Image SourceImages/op3.ICO//ButtonButton Margin10x:NamebtnDivide IsEnabled{Binding PathCanDivide}cal:Message.Attach[Event Click][Action Divide(left.Text, right.Text):result.Text]Image SourceImages/op4.ICO//Button/StackPanelStackPanel OrientationHorizontalLabel Margin10 Answer:/LabelTextBox Margin10Width72Text {Binding PathResult, StringFormat{}{0:F4}} IsReadOnlyTrue //StackPanel/StackPanel/Window说明对操作数Operand _1和Operand _2按Alt键数字可以选中该处这是WPF的一个特殊用法。由于计算结果不希望被修改于是加上了属性IsReadOnlyTrue。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继承ConductorObject于是ShellViewModel拥有了管理Screen实例的功能(ViewModel中使用ActivateItem函数而View中使用X:NameActivateItem标签)其具体代码为:using System.ComponentModel;using System.Threading;using System.Windows;using System.Windows.Controls;using Caliburn.Micro;namespace CaliburnMicro_Calculator.ViewModels{public class ShellViewModel : Conductorobject {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:ClassCaliburnMicro_Calculator.Views.ShellViewxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:CaliburnMicro_Calculator.Viewsxmlns:calhttp://www.caliburnproject.orgmc:IgnorabledTitleCalculator SizeToContentHeight Width240Grid MinHeight200Button ContentShow Calculator x:NameShowCalculator Grid.Row0/ButtonContentControl x:NameActiveItem/ContentControl /Grid/Window另外提一点向ViewModel A中嵌入ViewModel B一般来说需要做的操作是在A的view中使用ContentControl绑定B的ViewModel只需使用语句cal:View.Model{Binding BViewModel}即可而B的view是UserControl就可以啦。此时CalculatorView是一个UserControl其代码为:UserControl x:ClassCaliburnMicro_Calculator.Views.CalculatorViewxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:localclr-namespace:CaliburnMicro_Calculator.Viewsxmlns:calhttp://www.caliburnproject.orgmc:IgnorabledWidth240StackPanel BackgroundBeigeStackPanel OrientationHorizontalLabel Margin10Target{Binding ElementNameleft} Operand _1:/LabelTextBox Margin10Width72x:Nameleft//StackPanelStackPanel OrientationHorizontalLabel Margin10Target{Binding ElementNameright} Operand _2:/LabelTextBox Margin10Width72x:Nameright//StackPanelStackPanel OrientationHorizontal HorizontalAlignmentCenterButton Margin10x:NamebtnPlus cal:Message.Attach[Event Click][Action Plus(left.Text, right.Text):result.Text]Image SourceImages/op1.ICO//ButtonButton Margin10x:NamebtnMinus cal:Message.Attach[Event Click][Action Minus(left.Text, right.Text):result.Text]Image SourceImages/op2.ICO//ButtonButton Margin10x:NamebtnMultiply cal:Message.Attach[Event Click][Action Multipy(left.Text, right.Text):result.Text]Image SourceImages/op3.ICO//ButtonButton Margin10x:NamebtnDivide IsEnabled{Binding PathCanDivide}cal:Message.Attach[Event Click][Action Divide(left.Text, right.Text):result.Text]Image SourceImages/op4.ICO//Button/StackPanelStackPanel OrientationHorizontalLabel Margin10 Answer:/LabelTextBox Margin10Width72Text {Binding PathResult, StringFormat{}{0:F4}, UpdateSourceTriggerPropertyChanged} IsReadOnlyTrue //StackPanel/StackPanel/UserControl好啦就酱由于本例中逻辑并不复杂Model暂时用不上对于复杂一点的项目Model主要负责数据的读取如文件操作、数据库操作、service调用等以后有机会举例具体来说。如果需要持久化(persistent)则还需给给每对M-VM(Model和ViewModel)加入State这个实际工程中也用得特别多。6.功能举例 Calculator主页点击按钮“ShowCalculator”即可看到具体的计算器~乘法举例除法举例最后附上代码CaliburnMicro-Calculator: A simple Calculator using Caliburn.Microhttps://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都会变得特别好看