是否需要调度程序来更改WPF

是否需要调度程序来更改WPF

本文介绍了是否需要调度程序来更改WPF .Net Core 3或更高版本中的数据绑定属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多示例(甚至来自MS)都使用调度程序来更新数据绑定属性,并且可以找到许多不同的答案.是否存在官方"声明?目前,我一直使用调度程序,并且只有在可以确定这是一项正式功能并且在将来的.Net版本中仍然可以使用时,才会更改此设置.

A lot of examples (even from MS) use a dispatcher to update data-bound properties and a bunch of different answers could be found. Does an 'official' statement exist?Currently, I always use a dispatcher and I would only change this if I can be sure that this is an official feature and it will still work on future .Net versions.

推荐答案

不.早期版本和.NET Framework版本都不需要.

No. It's not needed in earlier versions either, nor on .NET Framework versions.

对于通过常规数据绑定机制(例如XAML中的{Binding}标记)绑定到依赖项属性的任何属性,WPF始终会根据需要自动将属性更改更新编组到UI线程.

WPF has always automatically marshalled property-change updates to the UI thread as needed, for any properties bound to a dependency property via the normal data binding mechanisms (e.g. {Binding} markup in XAML).

我不知道这是明确记录的.我环顾了Microsoft Docs网站上最有可能出现的主题,例如WPF的数据绑定和INotifyPropertyChanged页面,但没有看到任何内容.但是,这是WPF的一项众所周知的功能,在MSDN的2014年这篇文章中提到: MVVM:MVVM应用程序中的多线程和调度:

I don't know that this is documented explicitly. I looked around on the Microsoft Docs site, on the most likely topics, such as the data-binding and INotifyPropertyChanged pages for WPF, but didn't see anything. However, it's a well-known feature of WPF, and is mentioned in this 2014 article from MSDN: MVVM : Multithreading and Dispatching in MVVM Applications:

另请参见任务并行库INotifyPropertyChanged是否不会引发异常?

请注意,这仅适用于简单的单值属性.收集是另一回事,尽管WPF从4.5开始也提供了对自动更改收集更改事件的封送的支持.参见例如如何通过辅助线程更新ObservableCollection?

Note that this is only for simple single-value properties. Collections are a different matter, though WPF since 4.5 has also included some support for automatic marshaling of collection-changed events. See e.g. How do I update an ObservableCollection via a worker thread?

这篇关于是否需要调度程序来更改WPF .Net Core 3或更高版本中的数据绑定属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:34