wpf mvvm threading.Timer和TimerCallback问题 [英] wpf mvvm threading.Timer and TimerCallback problems
问题描述
我有一个针对MVVM框架开发的WPF应用程序,其中ViewModel构造函数设置一个TimerCallback的定时器。
I have a WPF application developed against the MVVM framework in which the ViewModel constructor set a Timer with a TimerCallback.
TimerCallback检索一个ObservableCollection,字段。
The TimerCallback retrieves an ObservableCollection and passes it to a field on the VM.
我有一个CollectionViewSource,其Source属性设置为ObservableCollection。我使用一个CollectionViewSource,因为我想对集合启用过滤。
I have a CollectionViewSource which has its Source property set to the ObservableCollection. I am using a CollectionViewSource because I want to enable filtering on the Collection.
我发现当TimerCallback尝试将ObservableCollection传递到本地字段Source属性CollectionViewSource有一个异常{调用线程不能访问此对象,因为不同的线程拥有它。}。
I have found that when the TimerCallback attempts to pass the ObservableCollection into the local field the Source property for the CollectionViewSource has an exception {"The calling thread cannot access this object because a different thread owns it."}.
我理解异常,但我有两个问题: / p>
I understand the exception but I have two problems:
- 如何解决这个问题?
- 更重要的是,使用CollectionViewSource?如果我删除CollectionViewSource并使得ObservableCollection是一个公共属性,那么我没有这样的异常。
任何帮助谢谢!谢谢,
Drammy
Any help appreciated! Thanks, Drammy
推荐答案
- 使用
DispatcherTimer <
- 可能只需要一个
ObservableCollection code>您的UI未更新。
- Use the
DispatcherTimer
instead of the timer class you are using now, and make sure you create it on the UI thread. - Probably with just an
ObservableCollection
your UI is not updated.
这篇关于wpf mvvm threading.Timer和TimerCallback问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!