New data binding methods in UWP X:bind Analysis summary 0x00 x:bind in UWP
The experience of WPF development has been preceded by a few familiar topics such as XAML, data binding, and so on when learning the UWP. There was no trouble in the learning process. Until I saw this article in the garden:
Http://www.cnblogs.com/gaoshang212/p/4534138.html
The original UWP binding in the new add a x:bind, from the article can be seen x:bind efficiency is very high. Find MSDN (data binding) look (the complete learning directory can be found in: http://www.cnblogs.com/durow/p/4878822.html), and then try to write a few test procedures, X:bind also have some understanding of the Below write about your experience.
Why did 0x01 make a x:bind?
This question is still relatively easy to answer, because the efficiency of x:bind is high. After all, the UWP covers the mobile phone, while the mobile phone has limited performance on the one hand and limited battery power on the other, so saving performance overhead is significant. X:bind Why the cost savings, because x:bind data binding occurs at compile time, that is, when compiling the view of what data is bound, what type of data is already clear, the binding work can be completed at compile time. So we need to specify an explicit type of ViewModel in the backend code so that the data in the X:bind binding ViewModel in the view, everything is type-specific. And the binding is in the program run to determine the data source, is dynamic binding, efficiency is not as natural as x:bind. However, in design, X:bind makes the coupling between view and ViewModel tighter because the view must be given a type-specific viewmodel. The use of X:bind is the sacrifice of design efficiency bar, is a compromise way. Of course, if you do not like X:bind also do not care about that point of performance, binding can be used normally.
How to use 0x02 x:bind 1. Binding Properties
Let's start by explaining the model for testing:
The difference between x:bind and binding properties is that x:bind need to add the name of the ViewModel instance when binding, such as creating an instance VM in background code
This binding is required in view:
It is important to note that the default binding method for X:bind is onetime, which is to bind only once and remember to change to OneWay or TwoWay when used.
It is also necessary to note that the X:bind binding is strongly typed and requires a forced type conversion if it encounters a binding source of object (for example, SelectedItem that binds the ListView). As shown, where local is the namespace where TestData is located.
2. Binding Commands and Events
Binding commands are similar to property bindings, and you need to specify an instance of ViewModel. There are commands in ViewModel cmdtest as follows:
You can bind this in view:
It is also important that X:bind supports binding events and sees this one mouthful of old blood sprayed onto the screen. In WPF, it is also a reference to a third-party DLL and write Eventtocommand, just to bind event arguments (the MVVM design pattern and implementation in WPF (iv) event binding), If you see that invokecommandaction can pass event arguments as soon as you touch the UWP, you can also use excitement to describe the X:bind event binding only to vomit blood. We can bind events to commands as we used to, binding can be either binding or x:bind, and in addition, you can use X:bind to bind events to a ViewModel method. This method can be either parameterless or compliant with the EventHandler signature.
For example, there are two methods loaded and moved in ViewModel:
In view, you can bind events to both methods:
Run like this:
Advantages of 0X03 Binding
X:bind provides an efficiency-first binding method, but in some cases it needs to be treated with a binding. In addition to dynamic binding, what I can think of now is the binding updatesourcetrigger, which can specify how to update the data source. For example, if the text of the textbox changes when the data source is updated immediately, this x:bind should not be able to do it. There is also the design of the DataContext written in XAML, using the binding can be seen at design time data. With the increase in usage, you should also find more cases where you need to use binding.
0x04 written in the last
X:bind basically solves the common scenarios of data/command/event binding, and I am personally perfectly receptive to the relatively tight coupling of view and ViewModel. So my personal principle is that if I can use X:bind, the priority of using X:bind,x:bind is not enough to meet the demand and then use the binding.
Then go back and look at the MVVM framework that was written before the UWP, simply drawing. I believe that with the subsequent study will continue to produce this feeling;)
The last thing to say is to learn things to be steadfast, can not blindly seek fast, read the concept to see again, restudying is also good, not to mention there are new things inside.
New data binding methods in UWP X:bind analysis and summary