使用BindingOperations.EnableCollectionSynchronization

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

我有两个WPF应用程序"UI","Debugger"和一个ClassLibrary"BL". UI对调试器和BL的引用.调试器对BL的引用. 我在BL中有一个名为MyCollection的集合. UI应用程序将启动Debugger应用程序,并且Debugger绑定到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); }

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

更多推荐

使用BindingOperations.EnableCollectionSynchronization

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

发布评论

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

>www.elefans.com

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