一、运行效果:
二、Demo
ViewModel:
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using System;
namespace JiSuanQi.ViewModel
{
internal class Counter : ViewModelBase
{
public Counter()
{
}
private string temp = ""; // 中间变量
public string Temp
{
get { return temp; }
set { temp = value; RaisePropertyChanged("Temp"); }
}
private string showUp = ""; // 显示上
public string ShowUp
{
get { return showUp; }
set { showUp = value; RaisePropertyChanged("ShowUp"); }
}
private string show; // 显示下
public string Show
{
get { return show; }
set { show = value; RaisePropertyChanged("Show"); }
}
private int total = 0; // 运算
public int Total
{
get { return total; }
set { total = value; RaisePropertyChanged("Total"); }
}
private static int count1 = 0; // 计数器1
public int Count1
{
get { return count1; }
set { count1 = value; RaisePropertyChanged("Count1"); }
}
private int count2 = 0; // 计数器2
public int Count2
{
get { return count2; }
set { count2 = value; RaisePropertyChanged("Count2"); }
}
private int count3 = 0; // 计数器3
public int Count3
{
get { return count3; }
set { count3 = value; RaisePropertyChanged("Count3"); }
}
private string[] arrNum = new string[100]; // 保存数字
public string[] ArrNum
{
get { return arrNum; }
set { arrNum = value; RaisePropertyChanged("ArrNum"); }
}
private string[] arrChar = new string[100]; // 保存符号
public string[] ArrChar
{
get { return arrChar; }
set { arrChar = value; RaisePropertyChanged("ArrChar"); }
}
private string[] connect = new string[100]; // 连接
public string[] Connect
{
get { return connect; }
set { connect = value; RaisePropertyChanged("Connect"); }
}
public void Math()
{
if (Count1 > 0)
{
if (ArrChar[Count1 - 1] == "+")
{
Total += Convert.ToInt32(ArrNum[Count1]);
Show = Convert.ToString(Total);
}
if (ArrChar[Count1 - 1] == "-")
{
Total -= Convert.ToInt32(ArrNum[Count1]);
Show = Convert.ToString(Total);
}
if (ArrChar[Count1 - 1] == "*")
{
Total *= Convert.ToInt32(ArrNum[Count1]);
Show = Convert.ToString(Total);
}
if (ArrChar[Count1 - 1] == "/")
{
if (Convert.ToInt32(ArrNum[Count1]) != 0)
{
Total /= Convert.ToInt32(ArrNum[Count1]);
Show = Convert.ToString(Total);
}
if (Convert.ToInt32(ArrNum[Count1]) == 0)
{
Show = "除数不能为零";
}
}
if (ArrChar[Count1 - 1] == "=")
{
Total = (Total * 0) + Convert.ToInt32(ArrNum[Count1]);
}
if (ArrChar[Count1] == "=")
{
ShowUp = "";
}
}
else
{
Total = Convert.ToInt32(ArrNum[Count1]);
Show = Convert.ToString(Total);
}
}
private RelayCommand<string> countCommand; // 命令
public RelayCommand<string> CountCommand
{
get
{
if (countCommand == null)
{
countCommand = new RelayCommand<string>((str) =>
{
if (str == "0" || str == "1" || str == "2" || str == "3" || str == "4" || str == "5" || str == "6" || str == "7" || str == "8" || str == "9" || str == "@")
{
if (str != "@")
Temp += str;
if (Temp.StartsWith("0") == true && Temp.Length > 1)
{
Temp = Temp.Substring(1);
}
if (str == "@") // 退格
{
if (Temp.Length != 0)
{
Temp = Temp.Remove(Temp.Length - 1);
if (Temp.Length == 0)
Temp = "0";
}
}
if (Temp != "")
Show = Temp;
}
if (str == "+" || str == "-" || str == "*" || str == "/" || str == "=")
{
if (Temp != "")
{
ArrNum[Count1] = Temp;
Connect[Count3] = ArrNum[Count1];
ShowUp += Connect[Count3];
ArrChar[Count1] = str;
Connect[Count3] = ArrChar[Count1];
ShowUp += Connect[Count3];
Math();
++Count1;
++Count3;
Temp = "";
}
else
{
if (ShowUp.Length == 0)
{
if (Total == 0)
{
ArrNum[0] = "0";
ArrChar[0] = "+";
ShowUp = ArrNum[0] + ArrChar[0];
}
else
ShowUp = Convert.ToString(Total);
}
if (ShowUp.EndsWith("+") || ShowUp.EndsWith("-") || ShowUp.EndsWith("*") || ShowUp.EndsWith("/") || ShowUp.EndsWith("="))
{
ShowUp = ShowUp.Substring(0, ShowUp.Length - 1);
}
ArrChar[Count1] = str;
Connect[Count3] = ArrChar[Count1];
ShowUp += Connect[Count3];
if (ShowUp.EndsWith("="))
{
ShowUp = ShowUp.Substring(0, ShowUp.Length - 1);
}
else
ShowUp = ShowUp;
++Count1;
++Count3;
}
}
if (str == "#") // 清零
{
Total = 0;
ShowUp = "";
ArrChar = new string[100];
ArrNum = new string[100];
Connect = new string[100];
Count1 = 0;
Count3 = 0;
Temp = "";
Show = "";
}
});
}
return countCommand;
}
}
}
}
MainWindow.xaml:
<Window x:Class="JiSuanQi.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:JiSuanQi"
mc:Ignorable="d"
Title="MainWindow" Height="814" Width="658" WindowStyle="None">
<Window.Resources>
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Width" Value="161"/>
<Setter Property="Background" Value="#E5E5E5"/>
<Setter Property="Height" Value="110"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="#7F7F7F"/>
<Setter Property="FontSize" Value="35"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="250"/>
<RowDefinition Height="110"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel>
<TextBlock Text="{Binding ShowUp}" Height="80" Width="644" Foreground="Black" FontSize="27" TextAlignment="Right" Margin="0 40 0 0" TextWrapping="Wrap"/>
<TextBlock Text="{Binding Show}" Height="100" Foreground="Black" FontSize="50" TextAlignment="Right" FontWeight="DemiBold" TextWrapping="Wrap"/>
</StackPanel>
<Button Grid.Row="1" Width="644" Style="{StaticResource ButtonStyle}" Content="=" Command="{Binding CountCommand}" CommandParameter="="/>
<WrapPanel Grid.Row="2">
<Button Style="{StaticResource ButtonStyle}" Content="1" Command="{Binding CountCommand}" CommandParameter="1"/>
<Button Style="{StaticResource ButtonStyle}" Content="2" Command="{Binding CountCommand}" CommandParameter="2"/>
<Button Style="{StaticResource ButtonStyle}" Content="3" Command="{Binding CountCommand}" CommandParameter="3"/>
<Button Style="{StaticResource ButtonStyle}" Content="/" Command="{Binding CountCommand}" CommandParameter="/"/>
<Button Style="{StaticResource ButtonStyle}" Content="4" Command="{Binding CountCommand}" CommandParameter="4"/>
<Button Style="{StaticResource ButtonStyle}" Content="5" Command="{Binding CountCommand}" CommandParameter="5"/>
<Button Style="{StaticResource ButtonStyle}" Content="6" Command="{Binding CountCommand}" CommandParameter="6"/>
<Button Style="{StaticResource ButtonStyle}" Content="*" Command="{Binding CountCommand}" CommandParameter="*"/>
<Button Style="{StaticResource ButtonStyle}" Content="7" Command="{Binding CountCommand}" CommandParameter="7"/>
<Button Style="{StaticResource ButtonStyle}" Content="8" Command="{Binding CountCommand}" CommandParameter="8"/>
<Button Style="{StaticResource ButtonStyle}" Content="9" Command="{Binding CountCommand}" CommandParameter="9"/>
<Button Style="{StaticResource ButtonStyle}" Content="-" Command="{Binding CountCommand}" CommandParameter="-"/>
<Button Style="{StaticResource ButtonStyle}" Content="清零" Command="{Binding CountCommand}" CommandParameter="#"/>
<Button Style="{StaticResource ButtonStyle}" Content="0" Command="{Binding CountCommand}" CommandParameter="0"/>
<Button Style="{StaticResource ButtonStyle}" Content="退格" Command="{Binding CountCommand}" CommandParameter="@"/>
<Button Style="{StaticResource ButtonStyle}" Content="+" Command="{Binding CountCommand}" CommandParameter="+"/>
</WrapPanel>
</Grid>
</Window>
MainWindow.xaml.cs:
using JiSuanQi.ViewModel;
using System.Windows;
namespace JiSuanQi
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Counter viewModel = new Counter();
this.DataContext = viewModel;
}
}
}