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

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

Step 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"> <Grid MinHeight="200"> <Button Content="Show Calculator" x:Name="ShowCalculator" Grid.Row="0"></Button> <ContentControl x:Name="ActiveItem"></ContentControl> </Grid> </Window>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpyxsg.html