https://www.youtube.com/watch?v=zIKBA8RQvqM&list=PLxU-iZCqT52A0CK4ij1pVIL-cqTd0yfNo&index=3&t=0s
/mnt/1T-5e7/mycodehtml/Mobile/Xamarin/JongChulLee/001_Create_calculator/main.html
================================================================================
================================================================================
================================================================================
xaml example at developer.xamarin.com
================================================================================
App.xaml.cs
public App()
{
InitializeComponent();
MainPage = new Calc.MainPage();
}
================================================================================
MainPage.xaml
================================================================================
On common project, right click, add, new, class
Name: CalcViewModel.cs
================================================================================
using System;
using System.ComponentModel;
using System.Windows.Input;
using Xamarin.Forms;
namespace Calc
{
class CalcViewModel : INotifyPropertyChanged
{
// c inputString: property, store value which user inputs
string inputString = "";
// c displayText: Output textbox
string displayText = "";
// When property changes, event should happen
public event PropertyChangedEventHandler PropertyChanged;
// ================================================================================
// Constructor
public CalcViewModel()
{
// Define event handler
// Execute following when "number button" is clicked
this.AddCharCommand = new Command((key) =>
{
this.InputString += key;
});
// ================================================================================
// When "backspace button" is clicked, execute following, to clear one character
this.DeleteCharCommand = new Command((nothing) =>
{
this.InputString = this.InputString.Substring(0,this.InputString.Length - 1);
},
(nothing) =>
{
// Return true if there's something to delete.
return this.InputString.Length > 0;
});
// ================================================================================
// When "clear button" is clicked, execute following, to clear all text in output box
this.ClearCommand = new Command((nothing) =>
{
// Clear
this.InputString = "";
});
// ================================================================================
// When +,-,*,/ buttons are clicked, execute following
// Store number (which is in current output box) into Op1 property
// Store "clicked operator" into Op property
// Clear outputbox of calculator
this.OperationCommand = new Command((key) =>
{
this.Op = key;
this.Op1 = Convert.ToDouble(this.InputString);
this.InputString = "";
});
================================================================================
// When "= button" is clicked, execute this
this.CalcCommand = new Command((nothing) =>
{
this.Op2 = Convert.ToDouble(this.InputString);
switch (this.Op)
{
case "+": this.InputString = (this.Op1 + this.Op2).ToString(); break;
case "-": this.InputString = (this.Op1 - this.Op2).ToString(); break;
case "*": this.InputString = (this.Op1 * this.Op2).ToString(); break;
case "/": this.InputString = (this.Op1 / this.Op2).ToString(); break;
}
});
}
// ================================================================================
// c InputString: Public property, corresponded to inputString
public string InputString
{
protected set
{
if (inputString != value)
{
// Store value into inputString
inputString = value;
// Fire event
OnPropertyChanged("InputString");
// Store inputString into this.DisplayText
this.DisplayText = inputString;
// Activate "backspace button" when "number button" is clicked
((Command)this.DeleteCharCommand).ChangeCanExecute();
}
}
get { return inputString; }
}
// ================================================================================
// c DisplayText: property, binding between outputbox
public string DisplayText
{
protected set
{
if (displayText != value)
{
displayText = value;
OnPropertyChanged("DisplayText");
}
}
get { return displayText; }
}
// ================================================================================
// Op: operator
public string Op { get; set; }
public double Op1 { get; set; }
public double Op2 { get; set; }
// ================================================================================
// When "number" is clicked
public ICommand AddCharCommand { protected set; get; }
// ================================================================================
// When "backspace" is clicked
public ICommand DeleteCharCommand { protected set; get; }
// ================================================================================
// When "C" is clicked, to clear all text
public ICommand ClearCommand { protected set; get; }
// ================================================================================
// When +, -, *, / are clicked
public ICommand OperationCommand { protected set; get; }
// ================================================================================
// When = is clicked
public ICommand CalcCommand { protected set; get; }
// ================================================================================
protected void OnPropertyChanged(string propertyName)
{
// Fire event
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
================================================================================