用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; } } ``` 这个简单的计算器可以执行加、减、乘、除四个基本运算,您可以根据需要对代码进行修改和扩展。

相关推荐

最新推荐

recommend-type

WPF 在image控件用鼠标拖拽出矩形的实现方法

主要介绍了WPF 在image控件用鼠标拖拽出矩形的实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

C# 使用WPF 用MediaElement控件实现视频循环播放

在WPF里用MediaElement控件,实现一个循环播放单一视频的程序,同时可以控制视频的播放、暂停、停止。这篇文章给大家介绍了C# 使用WPF 用MediaElement控件实现视频循环播放,需要的朋友参考下吧
recommend-type

WPF实现窗体中的悬浮按钮

主要为大家详细介绍了WPF实现窗体中的悬浮按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

wpf经典入门教程.pdf

wpf入门教程。在学校了解过wpf但是并没有项目经验,拿来学,非常适合想要入门wpf开发并且有C#编程基础的童鞋
recommend-type

C# WPF新手入门之串口Modbus通讯

C#WPF制作的上位机和SmartGas气体分析模块通讯 需要完整项目文件的可在我的资源中另外下载
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。