Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use MVVM and MVVMLight framework

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to use the MVVM and MVVMLight framework. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

I. introduction of MVVM and MVVMLight

MVVM is the abbreviation of Model-View-ViewModel. Similar to the popular MVC and MVP design patterns, the main purpose is to separate the coupling of View and Model.

It is an excellent design pattern, but not at the framework level. It is a new type of architecture developed from the combination of MVP (Model-View-Presenter) pattern and WPF.

Based on the original MVP framework and blending the new features of WPF, to cope with the increasingly complex requirements of PC-side development.

The structure is shown in the figure:

Compared with the way the logical structure was written in Code Behind before, MVVM almost completely decoupled the relationship between view and logical business, and handled UI properties through data binding and commands.

And event driven. Similarly, the property changes caused by ViewModel's view interaction business logic processing are also notified to the View front end, allowing the View front end to update in real time. Data model, data logic and business logic are stored in Model.

In fact, I personally think that the data and business interaction layer should be separate, Model is completely a physical model, so it is more clear.

This type of bi-directional binding is excellent, and changes in View are automatically reflected on ViewModel, and vice versa. At present, the JS front-end framework AngularJS also adopts this design pattern.

MVVM pattern is the same as MVC schema, the main purpose is to separate view (View) and model (Model), which has several advantages.

1. Low coupling: views (View) can be changed and modified independently of Model, a ViewModel can be bound to different View, Model can not change when View changes, and View can not change when Model changes.

two。 Reusability: you can put some view logic in a ViewModel and let many View reuse this view logic.

3. Independent development: developers can focus on business logic and data development (ViewModel), designers can focus on page design, and Expression Blend can easily design interfaces and generate xml code.

4. Testable: interfaces have always been difficult to test, but now tests can be written for ViewModel.

If you are currently working on Microsoft's WPF, Windows Phone, Windows 8, SilverLight related projects, it is necessary for me to understand the MVVM Light framework first.

It is lightweight (compared to Prism), focused (only responsible for the implementation of MVVM architecture patterns, no other redundant components), efficient (easy to use, time-saving and labor-saving), and can help you better develop your applications.

MVVMLight's author Laurent Bugnion is a Microsoft MVP, and this is MVVMLight's official website: http://www.mvvmlight.net/, with detailed introductions and video documents.

Interestingly, MVVMLight's product Logo is a feather that implies the advantages we mentioned above: lightweight, professional and efficient. I don't know if that's what the author means.

Second, how to use MVMLight in your WPF application

There are two main ways of use.

One is to download MVVMLight Toolkit on the official website. After installation, the MvvmLight project template will appear in the VS template, and you can use the project template directly to create the project.

It will include the default ViewModelLocator and MainViewModel,MainViewModel, which is the ViewModel of your Main view, and ViewModelLocator is a global ViewModel load registry, which will be discussed in detail in this second article.

Another way is to install it on NuGet, which is undoubtedly the most convenient thing. Search for MVVM Light on the NuGet toolbox and you can see two items, one is MVVM Light and the other is MVVM Light libraries only.

The current version is 5.3, which means that the update speed of MVVM Light is quite fast, and the ALAPHA version of V5.4 is also available in Prerelease.

The difference between the two projects on Nuget is that MVVMLight adds related hierarchical file directories to form a simple MVVMLight molding framework in addition to the necessary GalaSoft components.

As shown in the picture, it will help you generate the ViewModel folder, which contains MainViewModel and ViewModelLocator files.

These are all the contents of the article "how to use MVVM and MVVMLight frameworks". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report