我一直在努力掌握这个概念,即使经过多次实验,我仍然无法弄清楚WPF中的ObservableCollections和使用BindingOperations.EnableCollectionSynchronization的最佳实践.
如果我有一个带有可观察集合的viewmodel,我使用锁启用集合同步,如下所示:
m_obsverableCollection = new ObservableCollection<..>;
BindingOperations.EnableCollectionSynchronization(m_obsverableCollection,
m_obsverableCollectionLock);
Run Code Online (Sandbox Code Playgroud)
这是否意味着对该可观察集合的每次修改和枚举都将:
使用BindingOperations.EnableCollectionSynchronization时,我是否需要显式执行任何类型的锁定?
产生这一切的问题是,即使在使用BindingOperations.EnableCollectionSynchronization并使用我传入该方法的相同锁定锁定项目后,我偶尔会得到"这种类型的CollectionView不支持从不同于该线程的线程更改其SourceCollection调度员线程." 例外
我有两个WPF应用程序"UI","调试器"和一个ClassLibrary"BL".UI对Debugger和BL的引用.调试器引用BL.我在BL中收集了名为MyCollection的集合.UI应用程序启动调试器应用程序,调试器绑定到BL中的集合MyCollection.当我尝试从UI应用程序更改MyCollection集合时,我遇到异常.
A first chance exception of type 'System.NotSupportedException' occurred in PresentationFramework.dll
Additional information: This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.
Run Code Online (Sandbox Code Playgroud)
我在google搜索并发现:BindingOperations.EnableCollectionSynchronization 我无法弄清楚如何使用它.我不想引用我的BL项目中的任何UI dll.任何人都可以帮助我吗?
谢谢您的帮助!