WPF MVVM从更新UI的BackgroundWorker绑定的属性 [英] WPF MVVM Updating UI Bound properties from BackgroundWorker

查看:609
本文介绍了WPF MVVM从更新UI的BackgroundWorker绑定的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有与更新视图模型从BackgroundWorker的UI的绑定属性的潜在问题?我试图更新虚拟机,而它被绑定到用户界面,以及潜在用户可能会键入..请问这里的同步工作(我不认为我可以使用在XAML锁语句)。

Are there any potential issues with updating UI bound properties of the ViewModel from the Backgroundworker? I am trying to update the VM while it is bound to the UI, and potentially users might be typing in.. how does the the synchronization work here (I don't think I can use Lock statements from XAML).

在此先感谢..

推荐答案

在更新标量的属性,你不需要担心这样做在UI线程上。在的PropertyChanged 事件被自动编组到UI线程。

When updating scalar properties, you don't need to worry about doing it on the UI thread. The PropertyChanged event is automatically marshalled to the UI thread.

然而,将无法正常工作为实现 INotifyCollectionChanged 集合。在 Col​​lectionChanged 事件不会被编组到UI线程,它会导致异常。所以,如果你修改绑定到一个集合的ItemsControl ,你需要使用 Dispatcher.Invoke (或者其他同步机制)做它在UI线程上。另一种选择是使用一个专门的集合了利用事件编组到正确的线程的照顾。请参见本文一个例子这样的集合。

However, it won't work for collections that implement INotifyCollectionChanged. The CollectionChanged event won't be marshalled to the UI thread, and it will cause an exception. So if you modify a collection that is bound to an ItemsControl, you need to use Dispatcher.Invoke (or another synchronization mechanism) to do it on the UI thread. Another option is to use a specialized collection that takes care of marshalling the event to the correct thread. See this article for an example of such a collection.

这篇关于WPF MVVM从更新UI的BackgroundWorker绑定的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆