System Structure Diagram: This example is a simplified version of the mvvm mode of the calculator, which is very simple.
This article opens the door. For more information about the mvvm theory, see the mvvm design mode.
1. Code in the app:
public App() { CalculatorView view = new CalculatorView(); view.DataContext = new CalculatorViewModel(); view.Show(); }
2. cauculatormodel code in the model layer
class CauculatorModel { public string FirstOperand { get; set; } public string SecondOperand { get; set; } public string Operation { get; set; } public string Result { get; set; } }
3. Command
Public class deletgatecommand <t>: icommand {private readonly action <t> _ executemethod = NULL; private readonly func <t, bool> _ canexecutemethod = NULL; public deletgatecommand (Action <t> executemethod): This (executemethod, null) {} public deletgategatecommand (Action <t> executemethod, func <t, bool> canexecutemethod) {If (executemethod = NULL) throw new argumentnullexception ("executemetnod"); _ executemet Hod = executemethod; _ canexecutemethod = canexecutemethod ;} # region icommand member /// <summary> // method to determine if the command can be executed /// </Summary> Public bool canexecute (t parameter) {If (_ canexecutemethod! = NULL) {return _ canexecutemethod (parameter);} return true ;} /// <summary> // execution of the command // </Summary> Public void execute (t parameter) {If (_ executemethod! = NULL) {_ executemethod (parameter) ;}# endregion event eventhandler icommand. canexecutechanged {Add {commandmanager. requerysuggested + = value;} remove {commandmanager. requerysuggested-= value ;}# region icommand member public bool canexecute (object parameter) {If (parameter = NULL & typeof (t ). isvaluetype) {return (_ canexecutemethod = NULL);} return canexecute (t) parameter);} public void execute (object parameter) {execute (t) parameter );} # endregion}
4. viewmodwl layer. To simplify the process, the add method adopts hard encoding.
public class CalculatorViewModel { CauculatorModel calculatorModel; private DeletgateCommand<string> addCommand; public CalculatorViewModel() { calculatorModel = new CauculatorModel(); } #region Public Properties public string FirstOperand { get { return calculatorModel.FirstOperand; } set { calculatorModel.FirstOperand = value; } } public string SecondOperand { get { return calculatorModel.SecondOperand; } set { calculatorModel.SecondOperand = value; } } public string Operation { get { return calculatorModel.Operation; } set { calculatorModel.Operation = value; } } public string Result { get { return calculatorModel.Result; } set { calculatorModel.Result = value; } } #endregion public ICommand AddCommand { get { if (addCommand == null) { addCommand = new DeletgateCommand<string>(Add, CanAdd); } return addCommand; } } public void Add(string x) { FirstOperand = x; SecondOperand = x; Result = (double.Parse(FirstOperand) + double.Parse(SecondOperand)).ToString(); Operation = "+"; MessageBox.Show( FirstOperand+ Operation +SecondOperand +"=" + Result); } private static bool CanAdd(string num) { return true; } }
Code in viewmodelbase:
public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } }
5. View Layer
<Grid> <textbox Height = "23" margin = "12,63, 120 "name =" textbox1 "verticalignment =" TOP "horizontalalignment =" Left "width =" "/> <label margin =, 95, 0 "name =" label2 "Height =" 32 "verticalalignment =" TOP "> enter the value of X! X + x =? </Label> <button Height = "23" command = "{binding addcommand}" commandparameter = "{binding elementname = textbox1, path = text} "horizontalalignment =" Left "margin =" 12,102, 0, 0 "name =" button1 "verticalignment =" TOP "width =" 75 "> OK </button> </GRID>
A parameter is passed in commandparameter. Of course, multiple parameters can be passed.
6 ,:
Reference: An Example of mvvm