用VS制作简易计算器(WPF)
时间: 2023-11-26 19:06:08 浏览: 25
好的,我可以为您提供一份简单的WPF计算器代码示例。以下是步骤:
1. 打开Visual Studio,选择“新建项目”,然后选择“WPF应用程序”模板。
2. 在“MainWindow.xaml”文件中,添加以下XAML代码:
```
<Window x:Class="Calculator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Calculator" Height="350" 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="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Row="0" Grid.ColumnSpan="4" Text="{Binding Path=Display, UpdateSourceTrigger=PropertyChanged}" FontSize="20" HorizontalContentAlignment="Right"/>
<Button Grid.Row="1" Grid.Column="0" Content="7" Command="{Binding Path=NumberInputCommand}" CommandParameter="7" FontSize="20"/>
<Button Grid.Row="1" Grid.Column="1" Content="8" Command="{Binding Path=NumberInputCommand}" CommandParameter="8" FontSize="20"/>
<Button Grid.Row="1" Grid.Column="2" Content="9" Command="{Binding Path=NumberInputCommand}" CommandParameter="9" FontSize="20"/>
<Button Grid.Row="1" Grid.Column="3" Content="/" Command="{Binding Path=OperatorInputCommand}" CommandParameter="/" FontSize="20"/>
<Button Grid.Row="2" Grid.Column="0" Content="4" Command="{Binding Path=NumberInputCommand}" CommandParameter="4" FontSize="20"/>
<Button Grid.Row="2" Grid.Column="1" Content="5" Command="{Binding Path=NumberInputCommand}" CommandParameter="5" FontSize="20"/>
<Button Grid.Row="2" Grid.Column="2" Content="6" Command="{Binding Path=NumberInputCommand}" CommandParameter="6" FontSize="20"/>
<Button Grid.Row="2" Grid.Column="3" Content="*" Command="{Binding Path=OperatorInputCommand}" CommandParameter="*" FontSize="20"/>
<Button Grid.Row="3" Grid.Column="0" Content="1" Command="{Binding Path=NumberInputCommand}" CommandParameter="1" FontSize="20"/>
<Button Grid.Row="3" Grid.Column="1" Content="2" Command="{Binding Path=NumberInputCommand}" CommandParameter="2" FontSize="20"/>
<Button Grid.Row="3" Grid.Column="2" Content="3" Command="{Binding Path=NumberInputCommand}" CommandParameter="3" FontSize="20"/>
<Button Grid.Row="3" Grid.Column="3" Content="-" Command="{Binding Path=OperatorInputCommand}" CommandParameter="-" FontSize="20"/>
<Button Grid.Row="4" Grid.Column="0" Content="0" Command="{Binding Path=NumberInputCommand}" CommandParameter="0" FontSize="20"/>
<Button Grid.Row="4" Grid.Column="1" Content="." Command="{Binding Path=NumberInputCommand}" CommandParameter="." FontSize="20"/>
<Button Grid.Row="4" Grid.Column="2" Content="=" Command="{Binding Path=EqualsCommand}" FontSize="20"/>
<Button Grid.Row="4" Grid.Column="3" Content="+" Command="{Binding Path=OperatorInputCommand}" CommandParameter="+" FontSize="20"/>
</Grid>
</Window>
```
3. 在“MainWindow.xaml.cs”文件中,添加以下C#代码:
```
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
namespace Calculator
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _display;
private string _currentValue;
private string _previousValue;
private string _currentOperator;
public ICommand NumberInputCommand { get; set; }
public ICommand OperatorInputCommand { get; set; }
public ICommand EqualsCommand { get; set; }
public string Display
{
get { return _display; }
set
{
_display = value;
OnPropertyChanged("Display");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
NumberInputCommand = new RelayCommand<string>(NumberInput);
OperatorInputCommand = new RelayCommand<string>(OperatorInput);
EqualsCommand = new RelayCommand<object>(EqualsInput);
_currentValue = "";
_previousValue = "";
_currentOperator = "";
}
private void NumberInput(string parameter)
{
_currentValue += parameter;
Display = _currentValue;
}
private void OperatorInput(string parameter)
{
if (_currentValue != "")
{
if (_previousValue != "" && _currentOperator != "")
{
EqualsInput(null);
}
_previousValue = _currentValue;
_currentValue = "";
_currentOperator = parameter;
}
}
private void EqualsInput(object parameter)
{
if (_previousValue != "" && _currentOperator != "" && _currentValue != "")
{
double prev = double.Parse(_previousValue);
double curr = double.Parse(_currentValue);
switch (_currentOperator)
{
case "+":
_previousValue = (prev + curr).ToString();
break;
case "-":
_previousValue = (prev - curr).ToString();
break;
case "*":
_previousValue = (prev * curr).ToString();
break;
case "/":
_previousValue = (prev / curr).ToString();
break;
}
_currentValue = "";
_currentOperator = "";
}
Display = _previousValue;
}
}
public class RelayCommand<T> : ICommand
{
private Action<T> _execute;
public RelayCommand(Action<T> execute)
{
_execute = execute;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_execute((T)parameter);
}
public event EventHandler CanExecuteChanged;
}
}
```
这个简单的计算器可以执行加、减、乘、除四个基本运算,您可以根据需要对代码进行修改和扩展。