"Introduction to the reprint of the MVVM pattern"

Source: Internet
Author: User

Original link: http://www.cnblogs.com/sirkevin/archive/2012/11/28/2793471.html

Using WPF+MVVM for more than a year, during the period due to lack of understanding of the MVVM model, encountered a lot of problems, but also around a lot of bend; the example of MVVM available on the web is simple, and the requirements in the actual project are varied. However, after several projects, there are some understandings of the MVVM pattern:
1. What is MVVM, and how does MVVM come from? The MVVM pattern is widely used in WPF project development, where the UI and business logic can be separated from each other, enabling UI designers and business logic personnel to have a clear division of labor.

The MVVM pattern is based on the MVP model, and it's easy to say that the MVVM pattern is the WPF version of the MVP model. The MVP mode, the MVC pattern, are all designed to pull out the UI logic and the business logic.


2. Use the MVVM pattern to refer to the mainstream framework and brief introduction. Mainstream Open source framework: Simple MVVM,MVVM light and prism. Simple MVVM and MVVM light are basically the same, are the basic encapsulation of the MVVM pattern, and add some good features, message transmission mechanism, dependency injection and so on. The development of small and medium-sized projects is recommended to use one of these two. Prism is an MVVM framework given by Microsoft, but its focus is not just on the MVVM pattern, I think it's mostly about modules. This framework is recommended for large projects, such as a system that needs to be divided into N teams to develop different modules and then organize them into a single system.
3. Use the MVVM pattern to analyze the problem. Now there is a feature: Users fill in the name, age, choose Favorite Color, and then click OK button to submit. This is a simple questionnaire. A. The first design Ui,ui may look like this:

B. Designing the corresponding ViewModel layer according to the UI requires those attributes, data, and commands:

Name:string Age :int favoritecolor:string submit:command     

C. In the ViewModel layer of the Submitcommand command, call the model layer's submit method for logical processing. The model layer might look like this:

 public string Name { get; setpublic int Age {get; Span style= "color: #0000ff;" >setpublic string FavoriteColor { get; setpublic void Submit () {...}     

4. Some of the views on the MVVM pattern are not quite right. a.code-behind file to ensure absolute cleanliness. This is a bit too absolute, ideally, the view layer's Code-behind file only calls the initial UI element's line of code in the constructor and sets the DataContext of the view layer to the ViewModel layer. In fact, Code-behind can also be some of the logic of the UI, such as some rich animation effect, or directly set the style of an element, and so on. However, it is true that business logic cannot be written in Code-behind. The B.viewmodel layer cannot be a simple package of the model layer, and the ViewModel layer cannot be a simple mapping of the view layer. The ViewModel layer and the view layer are absolutely separated, and the ViewModel layer needs to be designed in the same way as the view layer. I think it's a little too much to do. The purpose of MVVM is simply to separate the UI logic from the business logic, which layer do you say the business logic and data are written to? Yes, the model layer. The main responsibility of the ViewModel layer is to express the logic and state, that is, the ViewModel layer is connected to the view layer and the model layer. Some of the business logic states of the model layer need to be exposed to the view layer through the ViewModel layer to reflect to the software consumer.
5. I have some experience in WPF+MVVM model development. A.WPF Several important concepts to be proficient in understanding and applying: templates, dependency properties, data binding. Combined with the MVVM pattern, it works well. B. Members of the team are best able to have a unified understanding and understanding of the MVVM pattern. The C.MVVM mode is just a pattern, not having the MVVM pattern to write beautiful code, involving specific business logic, or the need for you and your team to be strong enough to design.

"Introduction to the reprint of the MVVM pattern"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.