本文共 5814 字,大约阅读时间需要 19 分钟。
使用WPF搭建小型计算器
在现代应用开发中,Windows Presentation Foundation(WPF)作为一款强大的UI框架,能够帮助开发者快速构建高效率的用户界面。以下将通过实际案例,讲述如何使用C#和WPF从零开始打造一个功能简单但实用的计算器应用。
此计算器应用主要包含以下功能:
在实际应用中,计算器需要处理许多潜在的错误场景。通过使用try...catch语句,可以有效地捕获并处理以下四类异常:
DivideByZeroException异常。double.Parse方法会抛出FormatException异常。double.Parse同样会抛出FormatException异常。OverflowException异常会被抛出。在代码逻辑中,所有这些异常都会被捕获,并通过textblock控件将错误信息显示给用户。
为了实现计算器的基本运算功能,可以定义一个名为MyMath的类,包含以下静态方法:
GetHe(double numA, double numB):加法运算GetCha(double numA, double numB):减法运算GetJi(double numA, double numB):乘法运算GetShang(double numA, double numB):除法运算每个方法都包含适当的参数检查和异常处理,确保程序的稳定运行。
计算器的主要界面由以下元素组成:
textbox控件用于输入数值radiobutton控件用于选择运算类型button控件用于触发计算textblock控件用于显示计算结果布局设计采用Grid和GroupBox控件,确保界面整洁美观。
以下是完整的C#代码实现:
using System;using System.Collections.Generic;using System.Data.SqlTypes;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace WpfApp3{ public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { double x, y; MyMath M = new MyMath(); textbox3.Clear(); try { x = double.Parse(textbox1.Text); y = double.Parse(textbox2.Text); } catch (Exception) { textbox3.Text += "错误:参数为空"; } if (jiafa.IsChecked == true) { try { textbox3.Text = M.GetHe(x, y).ToString(); } catch (Exception err) { textbox3.Text += err.ToString(); } } else if (jianfa.IsChecked == true) { try { textbox3.Text = M.GetCha(x, y).ToString(); } catch (Exception err) { textbox3.Text += err.ToString(); } } else if (chengfa.IsChecked == true) { try { textbox3.Text = M.GetJi(x, y).ToString(); } catch (Exception err) { textbox3.Text += err.ToString(); } } else if (chufa.IsChecked == true) { try { if (y == 0) { throw new DivideByZeroException(); } textbox3.Text = M.GetShang(x, y).ToString(); } catch (DivideByZeroException err) { textbox3.Text += err.ToString(); } catch (Exception err) { textbox3.Text += err.ToString(); } } } private void jiafa_Click(object sender, RoutedEventArgs e) { if (jiafa.IsChecked == true) { label1.Content = "+"; groupbox.Header = "加法"; } else { label1.Content = ""; groupbox.Header = ""; } textbox1.Text = ""; textbox2.Text = ""; textbox3.Text = ""; } private void jianfa_Click(object sender, RoutedEventArgs e) { if (jianfa.IsChecked == true) { label1.Content = "-"; groupbox.Header = "减法"; } else { label1.Content = ""; groupbox.Header = ""; } textbox1.Text = ""; textbox2.Text = ""; textbox3.Text = ""; } private void chengfa_Click(object sender, RoutedEventArgs e) { if (chengfa.IsChecked == true) { label1.Content = "*"; groupbox.Header = "乘法"; } else { label1.Content = ""; groupbox.Header = ""; } textbox1.Text = ""; textbox2.Text = ""; textbox3.Text = ""; } private void chufa_Click(object sender, RoutedEventArgs e) { if (chufa.IsChecked == true) { label1.Content = "/"; groupbox.Header = "除法"; } else { label1.Content = ""; groupbox.Header = ""; } textbox1.Text = ""; textbox2.Text = ""; textbox3.Text = ""; } } public class MyMath { public double GetHe(double numA, double numB) { return numA + numB; } public double GetCha(double numA, double numB) { return numA - numB; } public double GetShang(double numA, double numB) { if (numB == 0) { throw new DivideByZeroException(); } return numA / numB; } public double GetJi(double numA, double numB) { return numA * numB; } }} 通过以上代码实现,可以快速构建一个功能完善的小型计算器应用。整个开发过程注重异常处理和代码的可维护性,使得程序在处理各种异常场景时能够更加稳定和友好。
如果需要进一步优化,可以添加更多功能或改进用户界面设计,但对于当前需求而言,这个实现已经足够满足基本需求。
转载地址:http://fpil.baihongyu.com/