Data Binding method added in UWP x: Bind analysis summary, uwp new

Source: Internet
Author: User

Data Binding method added in UWP x: Bind analysis summary, uwp new
UWP data binding method x: Bind analysis summary 0x00 x: Bind in UWP

Since I had previous development experience in WPF, I skipped several very familiar topics such as XAML and data binding when I was learning UWP. It is not difficult to learn. Until I saw this article in the garden:

Http://www.cnblogs.com/gaoshang212/p/4534138.html

In the original UWP binding, a new x: Bind was added. From the article, we can see that x: Bind is very efficient. Find MSDN (Data Binding) to look at the (complete Learning directory can see: http://www.cnblogs.com/durow/p/4878822.html), and then try to write a few test programs, the x: Bind is also known, the following is my experience.

0x01 why is x: Bind created?

This is easy to answer, because x: Bind is efficient. After all, UWP covers mobile phones, while mobile phones have limited hardware performance and battery power. Therefore, saving performance overhead is of great significance. X: Why does Bind save the cost? Because the data binding of x: Bind occurs during compilation, that is, what data is bound to the View during compilation, the data type is clear, and the binding can be completed at compilation. Therefore, we need to specify a ViewModel with a clear type in the background code, so that the x: Bind is used in the View to Bind the data in the ViewModel. Everything is of a clear type. Binding determines the data source only when the program is running. It is a dynamic Binding, and the efficiency is naturally inferior to x: Bind. However, from the design point of View, x: Bind makes the coupling between the View and the ViewModel tighter, because a ViewModel of a specific type must be specified for the View. Using x: Bind is a compromise on design efficiency. Of course, if you do not like x: Bind and do not care about the performance, Binding can also be used normally.

0x02 x: Bind usage 1. Bind attributes

First, describe the Model used for testing:

 

X: The difference between Bind and Binding is that x: Bind must add the ViewModel Instance name when Binding. For example, create an instance VM in the background code.

 

In View, you need to bind it as follows:

Note that x: the default Bind binding method is OneTime, that is, only one binding. Remember to change it to OneWay or TwoWay as needed.

In addition, it should be noted that the binding of x: Bind is strongly typed. If the binding source is an Object (for example, the SelectedItem bound to ListView), forced type conversion is required. As shown in, local is the namespace of TestData.

 

2. Bind commands and events

The binding command is similar to the property binding command. You need to specify the ViewModel instance. In ViewModel, the command named test is as follows:

 

In View, you can bind it as follows:

In addition, it is very important that x: Bind supports binding events and the old blood is sprayed onto the screen. In WPF, a third-party dll is referenced and EventToCommand is written to bind an event to pass an event parameter (MVVM design mode and implementation in WPF (4) event binding ), if you can see that InvokeCommandAction can pass event parameters when you are new to UWP, you can also use excitement to describe it. You can see that the event binding of x: Bind can only vomit blood. We can Bind the event to the command as before. The Binding method can be Binding or x: Bind. In addition, we can directly use x: bind binds the event to a method of ViewModel. This method can be non-parameter or conform to the EventHandler signature.

For example, there are two methods in ViewModel: Loaded and Moved:

 

You can bind events to the two methods in View:

 

It runs like this:

 

0x03 Binding advantages

X: Bind provides an efficiency-first Binding method, but in some cases, Binding is still required. In addition to dynamic Binding, I can now think of the Binding UpdateSourceTrigger, which can be used to specify the method for updating the data source. For example, if the TextBox Text changes, update the data source immediately. This x: Bind cannot be used. In addition, you can write DataContext in XAML during design, and use Binding to view data during design. As the usage increases, you may find more cases where Binding is required.

0x04 written at the end

X: Bind basically solves common scenarios of Binding data, commands, and events. I personally accept the tight coupling between views and viewmodels. Therefore, my personal principle is to prioritize the use of x: Bind and x: Bind when x: Bind cannot meet the requirements and then use Binding.

Then let's take a look at the MVVM framework written in UWP. I believe that this kind of feeling will continue to emerge with the subsequent study ;)

The last thing I want to talk about is to learn things to be steadfast and not to be quick. I have read the concepts again. It is also good to learn new things and learn new things. What's more, there are new things in it.

Related Article

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.