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 mvvmlight in wpf

2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Brief introduction

WPF (Windows Presentation Foundation) is a Windows-based user interface framework launched by Microsoft, which is part of .NET Framework 3.0. It provides a unified programming model, language and framework, which really separates the work of interface designers and developers; at the same time, it provides a new multimedia interactive user graphical interface.

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 xaml code.

Mvvmlight (download the latest version of nuget)

4. Testable. Interfaces have always been difficult to test, and tests can be written for ViewModel.

Vs2015

I. forcibly close

I ran a new thread in my program.

Thread.Start ()

When you close the wpf window, the thread continues to run in the background if the program in the thread is not finished.

Then add it to the .cs file of the main form

Protected override void OnClosed (EventArgs e) {base.OnClosed (e); Environment.Exit (0);} II. Project import mvvmlight and problems

After adding mvvmlight to the type of work you want to use mvvmlight

There will be an extra ViewModel folder in the project

There are two files in it:

MainViewModel.cs

ViewModelLocator.cs

And there will be a few more lines in the original App.xaml:

In the above code

The contents of the tag

Forget about that.

If you run it directly, you will report an error

An error will be reported in the ViewModelLocator.cs under the ViewModel folder

The solution to this error was found by searching:

Comment or delete the reference that reported an error in this line, and add another reference

That is,

Using CommonServiceLocator

In this way, it will not be wrong.

III. ObservableObject of mvvmlight framework

Class inherits ObservableObject

Of course, refer to the corresponding package.

Using GalaSoft.MvvmLight

This, as I understand it, inherits this class, and the change in the property can be responded to and reflected in the UI.

That is, when the attribute changes, it will be updated in UI accordingly.

In the xaml.cs of the form, the context of the data binding is defined in the constructor

This.DataContext =...

Bind in a xaml file

Open a new thread and update the bound data at regular intervals, so that the content will be updated in the TextBlock in the window.

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

Internet Technology

Wechat

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

12
Report