使用 BindingOperations.EnableCollectionSynchronization

编程入门 行业动态 更新时间:2024-10-28 08:26:35
本文介绍了使用 BindingOperations.EnableCollectionSynchronization的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 WPF 应用程序UI"、Debugger"和一个 ClassLibraryBL".对调试器和 BL 的 UI 引用.对 BL 的调试器引用.我在 BL 中有一个名为 MyCollection 的集合.UI 应用程序启动调试器应用程序,调试器绑定到 BL 中的集合 MyCollection.当我尝试从 UI 应用程序更改 MyCollection 集合时,出现异常.

I have two WPF applications "UI", "Debugger" and one ClassLibrary "BL". UI references to Debugger and BL. Debugger references to BL. I have collection in BL called MyCollection. UI app starts the Debugger app and Debugger binds to a collection MyCollection in BL. When I try changing the MyCollection collection from UI app I am getting exception.

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.

我在谷歌上搜索并发现了这个:BindingOperations.EnableCollectionSynchronization我不知道如何使用它.我不想从我的 BL 项目中引用任何 UI dll.有人可以帮助我吗?

I was googling around and found this: BindingOperations.EnableCollectionSynchronization I can't figure out how to use it. I don't want to reference to any UI dlls from my BL project. Can anybody assist me on that?

感谢您的帮助!

推荐答案

我在 Stack Overflow 上看到的所有例子都是错误的.从另一个线程修改集合时,必须锁定集合.

All the examples I've seen on Stack Overflow for this get it wrong. You must lock the collection when modifying it from another thread.

在调度程序 (UI) 线程上:

On dispatcher (UI) thread:

_itemsLock = new object();
Items = new ObservableCollection<Item>();
BindingOperations.EnableCollectionSynchronization(Items, _itemsLock);

然后来自另一个线程:

lock (_itemsLock)
{
    // Once locked, you can manipulate the collection safely from another thread
    Items.Add(new Item());
    Items.RemoveAt(0);
}

本文的更多信息:http://10rem/blog/2012/01/20/wpf-45-cross-thread-collection-synchronization-redux

这篇关于使用 BindingOperations.EnableCollectionSynchronization的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 00:02:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/791799.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:BindingOperations   EnableCollectionSynchronization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!