WPF MVVM在后台工作程序上更新视图模型 [英] WPF MVVM updating the View Model on a Background Worker

查看:182
本文介绍了WPF MVVM在后台工作程序上更新视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此senerio有效吗?

Is this senerio valid?

我有一个维护物品的视图.

I have a View to maintain an Item.

我有一个View模型,该模型将Item对象(实现INotifyPropertyChanged)公开为与View绑定的属性.

I have View Model which exposes the Item Object (implements INotifyPropertyChanged) as a Property to which is View is bound.

将Item对象传递给经过修改的Backgroundworker,然后在BackgroundWorking完成后引发PropertyChanged事件对我来说是否有效?

Is it valid for me to pass the Item object to a Backgroundworker where it is modified, then raise the PropertyChanged event when the BackgroundWorking is complete?

或者BackgroundWorker绝不应修改Item对象.我将使用BackgroundWorker返回的结果更新现有的Item对象.这将在RunWorkerCompleted事件中完成.但这是否锁定了UI线程并击败了拥有backgound worker的对象?

Or should the BackgroundWorker in no way modify the Item object. I would update the existing Item object with the results passed back by the BackgroundWorker. This would be done in the RunWorkerCompleted event. But does this lock the UI thread and defeat the object of having a backgound worker?

困惑吗?

我会解释.

用户可以选择创建一个项目.我创建了视图和视图模型.在视图模型中,将创建一个空的Item对象.向他显示了用于维护物料的视图.在选择项目类型"属性时,这会引发一个复杂的过程,以创建要由用户输入的属性列表.创建列表时,我可以阻止UI线程,但这会带来糟糕的用户体验.我想将处理传递到后台线程,同时保持UI处于活动状态.目前,我设置了一个标志来指示View上的部分正在加载,将Item对象传递给BackgroundWorker,后者会更新可观察到的Properties集合.当BackgroundWorking完成时,我调用PropertyChanged事件,该事件将更新绑定到列表的View部分,并关闭标志以指示该部分正在加载.这似乎没有问题.但是我有一种直觉,我不应该在后台线程中从View Model更新绑定对象.

The User takes an option to create an Item. I create the View and View Model. In the View model an empty Item object is created. He is presented with a View to maintain the Item. On selecting the Item Type property, this instigates a complex process to create a list of proerties to be entered by the User. I could block the UI thread whilst the list is created but this gives a poor user experience. I want to pass the processing to a background thread while keeping the UI alive. At present, I set a flag to indicate the section on the View is loading, pass the Item object to the BackgroundWorker which updates the observable collection of Properties. When the BackgroundWorking is complete I call the PropertyChanged event which updates the section of the View which is bound to the list and turn off the flag to indicate the section is loading. This seems to work with no issues. But I have a gut feeling that I should not be updating bound onjects from the View Model in a background thread.

感谢蒂姆

推荐答案

听起来不错.只要您的item-object是 DependencyObject,您就可以在后台工作程序中更改它们的属性.
将数据绑定到对象的属性将起作用,绑定引擎将为您自动进行线程切换.
但是,不要在不分派操作的情况下在后台工作进程中填充数据绑定的集合或操作DependencyObjects的属性(例如UI-Controls).这将导致异常.

This sounds ok. As long as your item-object is no DependencyObject, you can change the properties of them in the background worker.
DataBinding to properties of your object will work, the binding engine will do the thread-switching automatically for you.
However, dont't fill data-bound collections or manipulate properties of DependencyObjects (such as UI-Controls) in the background worker without dispatching the manipulations. This would lead to an exception.

修改:
仅作澄清:真正的问题不是,如果item-object是DependencyObject,而是属性是CLR-propertyDependencyProperty.因为DependencyProperties绑定到DependencyObjects,所以我经常使用上面的简化,但并不是全部.
这意味着,如果您具有CLR属性,则可以从外来线程设置其值,而不管您的类是否为DepenendencyObject.这与我的第一句话略有不同.


Only for clarification: The real question is not, if the item-object is a DependencyObject but if the property is a CLR-property or a DependencyProperty. Because DependencyProperties are bound to DependencyObjects, I often use the above simplification, but its not the full truth.
This means that if you have a CLR-property, you can set its value from a foreign thread, regardless of your class is a DepenendencyObject or not. This is a slight difference to my first statement.

这篇关于WPF MVVM在后台工作程序上更新视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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