问题
How is the compiled binding, x:Bind, different from the classical Binding, put as concisely as possible?
回答1:
Compared with the classical binding you cannot use the following binding attributes with x:Bind: ElementName, RelativeSource, Source and UpdateSourceTrigger. Well, that almost sums up the limitations of x:Bind but x:Bind is powerful in its own right -- the notable one being compiled binding and hence the performance gain as a result. See last bullet point for another powerful thing in x:Bind's armory.
One important point to always remember: the data context of x:Bind is the code-behind class. To steer x:Bind to a ViewModel class, just prefix with the ViewModel object reference for page DataContext.
Another point to remember is that the default binding Mode is OneTime while OneWay and TwoWay modes are also available. If a binding doesn't appear to be working, perhaps the binding is in default mode and past the OneTime trigger. To avoid such mishaps, define the binding Mode explicitly.
My favorite -- binding for events using x:Bind -- now you can easily implement event-to-method pattern. In XAML, forget the
Tapped="MyTapHandlerInCodeBehind"
code and start using the x:Bind variantTapped="{x:Bind ViewModel.MyTapHandlerInViewModel}"
. One minor downside though is you cannot pass Parameter as with the classical event handler, but this is not common use case and an alternative exists.
回答2:
The classic binding uses reflection to get the properties values to bind. X:Bind creates a class at compile time for getting the values so the binding will be quicker
来源:https://stackoverflow.com/questions/33848334/how-does-xbind-compare-with-the-classical-binding-put-as-concisely-as-possible