Difference between Binding and x:Bind

后端 未结 2 1673
闹比i
闹比i 2020-11-29 04:04

What to use in UWP, Binding or x:Bind and what is the difference between them?

Because I see a lot of posts where people use Binding<

相关标签:
2条回答
  • 2020-11-29 04:21

    {x:Bind} executes special-purpose code, which it generates at compile-time. {Binding} uses general-purpose runtime object inspection. Consequently, {x:Bind} has great performance and provides compile-time validation of your binding expressions. It supports debugging by enabling you to set breakpoints in the code files that are generated as the partial class for your page.

    Because {x:Bind} uses generated code to achieve its benefits, it requires type information at compile time. This means that you cannot bind to properties where you do not know the type ahead of time. Because of this, you cannot use {x:Bind} with the DataContext property which is of type Object, and is also subject to change at run time. The {x:Bind} markup extension—new for Windows 10—is an alternative to {Binding}. {x:Bind} lacks some of the features of {Binding}, but it runs in less time and less memory than {Binding} and supports better debugging.

    0 讨论(0)
  • 2020-11-29 04:29

    The following is probably not complete, but some of the major differences are

    • Old style {Binding }

    • binds to the DataContext

    • binds to a Property Name, flexible about the actual source type


    • New style {x:Bind }
    • binds to the Framework element (code-behind class)
    • needs all types fixed at compile time
    • defaults to the more frugal OneTime mode

    And starting with build 14393, {x:Bind } supports:

    • direct BooleanToVisibility binding, without a ValueConverter
    • expanded Function binding
    • casting
    • dictionary indexers

    The newer {x:Bind } is a little faster at runtime but just as important it will give compiler errors for erroneous bindings. With {Binding } you would just see an empty Control in most cases.

    For in-depth comparison checkout: {x:Bind} and {Binding} feature comparison

    0 讨论(0)
提交回复
热议问题