How to change default mode of x:Bind?

2019-07-08 20:04发布

I don't know why they decided to set default value for Mode to OneTime But that's not what I want most of the time. It wasted my whole day in debugging.

Is there a way to set OneWay value as default for Mode of x:Bind ?

<!--This will not listen to future changes. it is OneTime by default-->
<TextBlock Text="{x:Bind Name}"/>

<!--We have to explicitly define Mode value-->
<TextBlock Text="{x:Bind Name, Mode=OneWay}"/>

3条回答
来,给爷笑一个
2楼-- · 2019-07-08 20:35

starting in Windows 10, version 1607 (Anniversary Update), SDK version 14393 you can set

x:DefaultBindMode="OneWay"

on any of the parent xaml elements and the default will persist for children of that node that haven't explicitly set the Mode property.

Here you can read more about it.

查看更多
男人必须洒脱
3楼-- · 2019-07-08 20:40

I think it's by design. If you want to use OneWay as as default value. You could use Binding

For now, the Binding should be stronger than X:Bind.

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.

查看更多
欢心
4楼-- · 2019-07-08 20:41

TL:DR: No it's not possible to change the binding mode on the built-in controls.

x:Bind together with a few other markup extensions like x:Phase were all added to increase performance. Keep in mind that UWP applications can run on desktops, but also on the smallest IoT devices, so performance is key.

First of all, x:Bind is a compiled binding. During compilation the XAML is converted to strongly typed code behind, which is faster than the runtime object inspection used by {Binding}.

Second, it's optimized for performance by itself, using OneTime binding. OneWay and TwoWay bindings requires infrastructure to watch and push back changes.

The binding object can optionally be configured to observe changes in the value of the data source property and refresh itself based on those changes. It can also optionally be configured to push changes in its own value back to the source property.

In the past everything used to be OneWay with {Binding}, implying a small performance hit on every single field, even those that had to be bound only once (because why would you bother changing to OneTime if it just works). Now you're forced to think which fields should be update-able and thus use more resources.

More info on x:Bind on MSDN.

查看更多
登录 后发表回答