Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon 一次性结合与MVVMCross中的单向结合有何不同


一次性结合与MVVMCross中的单向结合有何不同


在MVVMCROSS中,一次性结合和单向结合是两个不同的数据结合模式,具有不同的目的。

###一次性绑定

当最初设置绑定源时,一次性绑定将数据从视图模型传输到视图一次。在此初始传输之后,绑定不会监视ViewModel中的更改,也不会更新视图,除非绑定源本身已重置。此模式对于可配置但设置后不经常更改的字段很有用。例如,在MVVMCross中,一次性绑定用于从语言文件中设置静态文本,因为用户通常会选择一种语言,并且此后不经常更改它[1]。

###单向绑定

另一方面,每当ViewModel中的相应属性更改时,单向绑定会不断更新视图。这意味着对ViewModel属性的任何更改都会自动反映在视图中。但是,直接对视图进行的更改不会影响ViewModel。当显示来自动态源的数据时,单向绑定是有用的,例如传感器数据或网络feed,在此视图应反映ViewModel的最新状态,但不需要将更新发送给它[1]。

###密钥差异

- 更新频率:一次性绑定在设置时仅更新一次视图,而单向绑定每当视图模型更改时会连续更新视图。
- 更改监视:一次性绑定不会在初始设置后监视ViewModel中的更改,而单向绑定会积极地倾听ViewModel中的更改。
- 用例:一次性是静态或不经常更改数据的理想选择,而单向更适合需要实时显示的动态数据,而不会影响ViewModel。

总而言之,尽管两种模式都将数据从视图模式传输到视图,但一次性绑定只能进行一次,并且不监视更改,而单向绑定不断地基于ViewModel的更改来连续更新视图。

引用:
[1] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[2] https://blog.ostebaronen.dk/2018/01/mvvmcross-binding-target.html
[3] https://stackoverflow.com/questions/41768962/mvvmcross-default-binding-mode-used-sused-in-a-project
[4] https://www.mvvmcross.com/documentation/fundamentals/viewmodel-lifecycle
[5] https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/binding-mode?view=net-maui-9.0
[6] https://yanxiaodi.gitbook.io/xamarin-mvvmcrosshandbook/ususion-mvvmcross-to-create-your-first-first-first-first-first-first-first-creating-the-the-android-the-android-project/underc----------------sy-and-handroid-project/underting-the-data-data-inda-binding-syntax-syntax
[7] https://www.mvvmcross.com/documentation/advanced/custom-data-binding
[8] https://github.com/mvvmcross/mvvmcross/issues/29
[9] https://stackoverflow.com/questions/27803448/data-binding-one-ves-two-way
[10] https://stackoverflow.com/questions/48910503/binding-mode-onetime-intime-intime-inmetime-in-mvxrecyclerview
[11] https://www.reddit.com/r/javascript/comments/comments/v993r4/askjs_why_is_is_oneway_binding_better_better_better_than_twowoway/