概述
对于如何分层还不太明确,目前每层功能如下:
- view层:用来显示操作数1和2以及结果
- viewmodel层:将string类型的操作数转换为double类型
- model层:进行四则运算
每层详细代码如下:
view
using System;
using System.Collections.Generic;
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;
using calculator.viewmodel;
using calculator.model;
namespace calculator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string op1 = null;
private string op2 = null;
private string op = null;
private string show = null;
private string result = null;
public MainWindow()
{
InitializeComponent();
textname.Text = "0";
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button B = (Button)sender;
string Value = (string)B.Content;
if (op == null)
{
op1 = op1 + Value;
show = show + Value;
textname.Text = show;
}
else
{
op2 = op2 + Value;
show = show + Value;
textname.Text = show;
}
}
private void Button_Click_op(object sender, RoutedEventArgs e)
{
Button B = (Button)sender;
string Value = (string)B.Content;
op = Value;
show = null;
}
private void Button_Click_cal(object sender, RoutedEventArgs e)
{
trans t = new trans();
cal c = new cal();
string newop = op;
double num1 = t.transnum(op1);
double num2 = t.transnum(op2);
result = Convert.ToString(c.ops(num1, num2, newop));
show = result;
textname.Text = show;
op1 = null;
op2 = null;
op = null;
show = null;
}
private void Button_Click_14(object sender, RoutedEventArgs e)
{
show = null;
textname.Text = "0";
op1 = null;
op2 = null;
op = null;
}
}
}
viewmodel
using System;
using System.Collections.Generic;
using System.Text;
namespace calculator.viewmodel
{
class trans
{
public double transnum(string oldnumber)
{
double newnumber = Convert.ToDouble(oldnumber);
return newnumber;
}
}
}
model
using System;
using System.Collections.Generic;
using System.Text;
namespace calculator.model
{
class cal
{
public double ops(double number1, double number2, string op)
{
double result = 0;
switch (op)
{
case "+":
result = number1 + number2;
break;
case "-":
result = number1 - number2;
break;
case "*":
result = number1 * number2;
break;
case "/":
result = number1 / number2;
break;
}
return result;
}
}
}
主窗口设置
<Window x:Class="calculator.MainWindow"
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:calculator"
mc:Ignorable="d"
Title="计算器" Height="500" Width="400">
<Grid >
<Button Content="2" HorizontalAlignment="Left" Margin="221,231,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click"/>
<Button Content="1" HorizontalAlignment="Left" Margin="139,231,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click"/>
<Button Content="3" HorizontalAlignment="Left" Margin="303,231,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click"/>
<Button Content="5" HorizontalAlignment="Left" Margin="221,274,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click"/>
<Button Content="4" HorizontalAlignment="Left" Margin="139,274,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click"/>
<Button Content="6" HorizontalAlignment="Left" Margin="303,274,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click"/>
<Button Content="8" HorizontalAlignment="Left" Margin="221,317,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click"/>
<Button Content="7" HorizontalAlignment="Left" Margin="139,317,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click"/>
<Button Content="9" HorizontalAlignment="Left" Margin="303,317,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click"/>
<Button Content="0" HorizontalAlignment="Left" Margin="221,360,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click"/>
<Button Content="." HorizontalAlignment="Left" Margin="139,360,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click"/>
<Button Content="=" HorizontalAlignment="Left" Margin="303,360,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click_cal"/>
<Button Content="+" HorizontalAlignment="Left" Margin="57,231,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click_op"/>
<Button Content="-" HorizontalAlignment="Left" Margin="57,274,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click_op"/>
<Button Content="*" HorizontalAlignment="Left" Margin="57,317,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click_op"/>
<Button Content="/" HorizontalAlignment="Left" Margin="57,360,0,0" VerticalAlignment="Top" Height="30" Width="50" Click="Button_Click_op"/>
<Button Content="CE" HorizontalAlignment="Left" Margin="293,177,0,0" VerticalAlignment="Top" Height="30" Width="60" Click="Button_Click_14"/>
<TextBox HorizontalAlignment="Left" Name="textname" Margin="57,75,0,0" Text="0" TextWrapping="Wrap" VerticalAlignment="Top" Width="296" Height="50"/>
</Grid>
</Window>
第一次做开发,感觉幼稚的很,欢迎大佬们给出改进建议
最后
以上就是和谐大炮为你收集整理的C#编程入门,实现简单计算器(使用MVVM结构)的全部内容,希望文章能够帮你解决C#编程入门,实现简单计算器(使用MVVM结构)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复