MVVM pattern parsing and implementation in WPF (i)

Source: Internet
Author: User

Introduction to the MVVM pattern

MVVM is a shorthand for model, view, and ViewModel, and the introduction of this pattern is to use ViewModel to reduce the coupling between view and model, which is said to reduce the coupling between view and model. It can also be said to reduce the coupling between the interface and logic, ideally the interface and logic are completely separate, unilaterally changing the interface without the need for logical code changes, the same logic code changes do not need to change the interface. The same viewmodel can be displayed with a completely unused view, and the same view can use different viewmodel to provide different actions.

1.Model

Model is a class, is the abstract of the reality of things, the development process involved in the things can be abstracted as model, such as customer, customer's name, number, phone, address and other attributes also correspond to the property in class, the customer's order, The payment behavior corresponds to the method in class.

2. View

View is a good understanding, is the interface.

3. ViewModel

The model abstraction is mentioned above, then ViewModel is the abstraction of the view. The displayed data corresponds to the property in the ViewMode, and the commands executed correspond to the command in the ViewModel.

How MVVM is decoupled in WPF

In the MVVM mode of WPF, the data and command associations between view and ViewModel are implemented through bindings, and after binding view and ViewModel do not produce a direct dependency. Specifically, when data changes occur in view, attempts to modify the target of the binding are attempted. The same view executes the command when it searches for the binding. In turn, ViewModel in the property changes will be issued a notice that "the name of XXX property changed, you these view who bound XXX also to change Ah!", as to whether the view received is not to make changes and do not care. ViewModel command out of view is simpler, because the command does not need to manipulate the data in the view when it is operating, and only the property in the ViewModel can be manipulated. Changes to the property can be reflected on the view by binding. This also does not require view participation when testing the command. This is the so-called data-driven approach that I couldn't understand at the beginning of my engagement with WPF.

This allows the ViewMode to be tested without a view at all, and the view can be tested without ViewModel (of course, just testing the business-agnostic aspects of the interface layout and animation).

Issues that the MVVM framework needs to address

You can see that if you want to implement a set of MVVM frameworks, the most basic problem to solve is data binding and command binding. Also, because a large number of events are generated in the UI, you need to bind the events to commands in MVVM. Later articles will attempt to resolve these issues in turn. Finally, it is necessary to say that any design mode is reference, there are use scenes, avoid mechanically. In actual development, we should adopt a more adaptable model according to the characteristics of the project.

MVVM pattern parsing and implementation in WPF (i)

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.