forked from scriptcs/scriptcs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewmodels.csx
More file actions
124 lines (97 loc) · 2.6 KB
/
viewmodels.csx
File metadata and controls
124 lines (97 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Windows.Input;
public class CalculatorViewModel : ViewModelBase
{
private string _display;
public CalculatorViewModel()
{
Display = "0";
NumberCommand = new RelayCommand(param => AddNumber(Convert.ToInt32(param)), param => CanAddNumber);
AddCommand = CreateOperatorCommand('+');
SubtractCommand = CreateOperatorCommand('-');
MultiplyCommand = CreateOperatorCommand('*');
DivideCommand = CreateOperatorCommand('/');
CalculateCommand = new RelayCommand(param => Calculate(), param => CanCalculate);
ClearCommand = new RelayCommand(param => Clear(), param => true);
}
public string Display
{
get { return _display; }
set
{
_display = value;
NotifyPropertyChanged("Display");
CommandManager.InvalidateRequerySuggested();
}
}
public RelayCommand NumberCommand { get; private set; }
public RelayCommand AddCommand { get; private set; }
public RelayCommand SubtractCommand { get; private set; }
public RelayCommand MultiplyCommand { get; private set; }
public RelayCommand DivideCommand { get; private set; }
public RelayCommand CalculateCommand { get; private set; }
public RelayCommand ClearCommand { get; private set; }
private int? Operand1 { get; set; }
private char? Operator { get; set; }
private int? Operand2 { get; set; }
private int? Result { get; set; }
private bool CanApplyOperator
{
get { return Operand1.HasValue && !Operator.HasValue; }
}
private void ApplyOperator(char @operator)
{
Operator = @operator;
Display = string.Format("{0} {1} ", Display, Operator);
}
private bool CanAddNumber
{
get { return !Result.HasValue; }
}
private void AddNumber(int number)
{
if (Operator.HasValue)
{
Operand2 = !Operand2.HasValue ? number : (Operand2 * 10) + number;
Display += number;
return;
}
Operand1 = !Operand1.HasValue ? number : (Operand1 * 10) + number;
Display += number;
}
private bool CanCalculate
{
get { return Operand1.HasValue && Operator.HasValue && Operand2.HasValue && !Result.HasValue; }
}
private void Calculate()
{
switch (Operator)
{
case '+':
Result = Operand1 + Operand2;
break;
case '-':
Result = Operand1 - Operand2;
break;
case '*':
Result = Operand1 * Operand2;
break;
case '/':
Result = Operand1 / Operand2;
break;
}
Display = Result.ToString();
}
private void Clear()
{
Operand1 = null;
Operator = null;
Operand2 = null;
Result = null;
Display = "0";
}
private RelayCommand CreateOperatorCommand(char @operator)
{
return new RelayCommand(param => ApplyOperator(@operator), param => CanApplyOperator);
}
}