Here we will only introduce the theoretical part. Here is a simple example of mvvm. Here is an example I wrote, which is very simple.
1. mvvm: Model-View-view Model)
The mvvm design mode is based on the MVC structure that separates UI from logic.
View: interface layer;
Viewmodel: it is the abstract of the view and passes the view command to the model;
Mode: data access layer
2. The mvvm architecture in Silverlight or WPF provides the following benefits:
1. projects can be tested more easily to execute unit tests.
2. Completely separate the UI and business design. View and unittest are only two different forms of consumers of viewmodel.
3. It helps us differentiate and what are UI operations and what are business operations, rather than mixing them all in codebehind.
3. Role of viewmodel:
Viewmodel is the most important part in the mvvm architecture. viewmodel contains attributes, commands, methods, events, attribute verification, and other logic.
Viewmodel attributes: The attribute of viewmodel is the source of View data. These attributes can be composed of three parts:
Part is the copy attribute of the model.
The other part is used to control the UI status.
The third part is the parameters of some methods.
Viewmodel command: The command in viewmodel is used to accept the user input of the view and process it accordingly. We can also implement the same functions through methods.
Viewmodel events: Events in viewmodel are mainly used to notify the view to perform corresponding UI changes.
Viewmodel Method: Some events do not directly provide command calls, such as custom events. At this time, we can call the method in viewmodel through callmethodaction to complete the corresponding operation.
References: http://msdn.microsoft.com/zh-cn/magazine/dd419663.aspx