帮我写一个基于MVVM的中英文单词计算器
时间: 2023-08-28 09:37:57 浏览: 93
好的,我会帮您解答这个问题。首先,我们需要了解一下什么是MVVM模式。
MVVM是Model-View-ViewModel的缩写,是一种用于构建用户界面的架构模式,它将界面逻辑与业务逻辑分离,并且提供了一个双向绑定的机制,使得数据的修改可以自动更新到界面上。
接下来,我们可以开始编写中英文单词计算器了。首先,我们需要创建一个Model类,用于处理计算逻辑:
```C#
public class CalculatorModel
{
public double Calculate(double num1, double num2, string op)
{
switch (op)
{
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
return num1 / num2;
default:
throw new ArgumentException("Invalid operator");
}
}
}
```
然后,我们需要创建一个ViewModel类,用于处理界面逻辑和数据绑定:
```C#
public class CalculatorViewModel : INotifyPropertyChanged
{
private CalculatorModel model;
private double num1;
private double num2;
private string op;
private double result;
public CalculatorViewModel()
{
model = new CalculatorModel();
}
public double Num1
{
get { return num1; }
set
{
num1 = value;
OnPropertyChanged("Num1");
Calculate();
}
}
public double Num2
{
get { return num2; }
set
{
num2 = value;
OnPropertyChanged("Num2");
Calculate();
}
}
public string Op
{
get { return op; }
set
{
op = value;
OnPropertyChanged("Op");
Calculate();
}
}
public double Result
{
get { return result; }
set
{
result = value;
OnPropertyChanged("Result");
}
}
private void Calculate()
{
try
{
Result = model.Calculate(Num1, Num2, Op);
}
catch (Exception ex)
{
Result = 0;
MessageBox.Show(ex.Message);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
```
最后,我们需要创建一个View类,用于展示界面和数据绑定:
```C#
<Window x:Class="Calculator.View.CalculatorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Calculator" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.ColumnSpan="4" Content="Calculator" HorizontalAlignment="Center"/>
<Label Grid.Row="1" Grid.Column="0" Content="Num1:"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="5" Text="{Binding Num1, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Row="1" Grid.Column="2" Content="Op:"/>
<TextBox Grid.Row="1" Grid.Column="3" Margin="5" Text="{Binding Op, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Row="2" Grid.Column="0" Content="Num2:"/>
<TextBox Grid.Row="2" Grid.Column="1" Margin="5" Text="{Binding Num2, UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" Margin="5" Content="Calculate" Command="{Binding CalculateCommand}"/>
<Label Grid.Row="3" Grid.Column="0" Content="Result:"/>
<TextBox Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3" Margin="5" Text="{Binding Result}"/>
</Grid>
</Window>
```
现在,我们可以在代码中使用以下命令来显示我们的计算器界面:
```C#
CalculatorViewModel viewModel = new CalculatorViewModel();
CalculatorView view = new CalculatorView();
view.DataContext = viewModel;
view.ShowDialog();
```
好了,以上就是一个基于MVVM的中英文单词计算器的实现。希望能够对您有所帮助!
阅读全文