I. View
View view layer
That is, the XAML file mainly refers to the design of the interface. The XAML. CS file is only generated by default.CodeThe mvvm mode generally does not require page data initialization. The event processing code is written on XAML. CS.
Data initialization can be implemented through data binding to handle events. It is also implemented by binding commands.
First, add resources. Here, they are loaded on App. XAML.
<! -- Put the viewmodel you have writtenProgramResources, the next binding will be used -->
<Application. Resources>
<VM: globalviewmodellocator xmlns: Vm = "CLR-namespace: myproject. viewmodels"
X: Key = "locator"/>
</Application. Resources>
Myproject. viewmodels is the namespace globalviewmodellocator. The key of the viewmodel class resource you have written is represented by locator.
Of course, you can also load viewmodel as a resource on a specific page.
You can bind viewmodel To The XAML page.
For example
<Phone: phoneapplicationpage
......
Datacontext = "{binding mainviewmodel, source = {staticresource locator }}"
......
">
In this way, the previously defined viewmodel resource is bound.
Then you can bind the page control to the defined data and Command commands of viewmodel. (in some projects, the command and viewmodel are completely separated. In my opinion, the command is placed in viewmodel for initial development.
Initialization and calling are better, that is, all bindings, whether data or Command commands, are placed in viewmodel)
For example
<Textblock grid. Row = "1"
TEXT = "{binding item. averagerating }"
Margin = "0, 0, 8, 0"
Fontsize = "24"
Verticalalignment = "center"/>
This is the data bound to the viewmodel.
<I: interaction. triggers>
<Li: taptrigger>
<Cmd: eventtocommand command = "{binding tapcommand}" passeventargstocommand = "true"/>
</Li: taptrigger>
</I: interaction. triggers>
This is the command bound to viewmodel. This is written using the mvvm light Toolkit framework.
2. viewmodel
viewmodel is the view model layer. It is very important to display the view layer and handle various events.
there are many ways to implement the viewmodel layer.
You can inherit the inotifypropertychanged interface to inherit the base class of viewmodelbase to inherit the ienumerable interface to implement the base class implementation of a third-party framework.
BR> the syntax for inheriting the inotifypropertychanged interface is as follows
public class personviewmodel: inotifypropertychanged
{< br> private string firstnamevalue;
Public String firstname {
get {return firstnamevalue ;}
set
{< br> firstnamevalue = value;
// notification of a change to the firstname attribute
policypropertychanged ("firstname ");
}< BR >}
// Define the propertychanged event
Public event propertychangedeventhandler propertychanged;
Public void policypropertychanged (string propertyname)
{
If (propertychanged! = NULL)
{
Propertychanged (this, new propertychangedeventargs (propertyname ));
}
}
}
In fact, the principle of the viewmodel class is similar. Simply put, its attributes can be dynamically changed because it needs to interact with the view layer.
Command commands generally need to be defined as independent classes for implementation, and then instantiated on viewmodel
There are also several methods to implement the command class.
For example, the class that inherits the Command commands of the icommand using third-party components
The syntax for inheriting icommand is as follows:
Public class mycommand <t>: icommand
{
Action <t> excuted;
Func <bool> canexcute;
Public showmessagecommand (Action <t> excuted)
{
This. excuted = excuted;
}
Public bool canexecute (object parameter)
{
Return true;
}
Public event eventhandler canexecutechanged;
Public void execute (object parameter)
{
// Your code to be executed
}
}
The parameters to be passed in the view layer can be obtained through data binding, then processed in the Command command, and finally presented on the view layer through data binding.
Iii. Model
The model layer is a simple object-oriented entity class.
For example
Public class person
{
Public int age {Get; set ;}
Public string name {Get; set ;}
}
Public class persons
{
Public list <person> person;
Public list <person> getperson ()
{
Person = new list <person> ()
{
New person {name = "Tom", age = 21 },
New person {name = "Jack", age = 22 },
New person {name = "Rose", age = 23 },
};
Return person;
}
}
The model layer is mainly used to encapsulate information in an object-oriented method and then use it for the viewmodel layer.