本文介绍了如何在线程中可观察的集合中复制列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个背景工作人员填写/填写一个列表,并在填写和编辑列表后,将其列入可观察列表中:

  this.OBSMailCountList = new ObservableCollection< IMailCount>(_ allMailCounts); 

问题是Collection被绑定到Live Diagramm,在List中复制之后



错误



值不能为NULL / p>

我的问题是:



如何复制具有绑定的可观察集合线程?

解决方案

您的问题是您有 _allMailCounts == null 当时你调用可观察的收集构造函数。您可以检查 null 如下

  if(_allMailCounts!= null )
OBSMailCountList = new ObservableCollection< IMailCount>(_ allMailCounts);

以下是有关如何使用 ObservableCollection 从另一个步骤:






绑定到像往常一样定义的可观察集合

  ObservableCollection< IMailCount> _collection = new ObservableCollection< IMailCount>(); 
public ObservableCollection< IMailCount>集合
{
get {return _collection; }
set
{
_collection = value;
OnPropertyChanged();
}
}

在另一个线程中,以这种方式工作: p>

  //在UI线程中创建一个列表
列表< IMailCount> collection = null;
Dispatcher.Invoke(()=> collection = new List< IMailCount>(_ collections));

//在UI线程中完成工作集属性
Dispatcher.InvokeAsync(()=> Collection = new ObservableCollection< IMailCount>(collection));


I have a background worker who fills/refills a List and After refilling and editing the List I copy this list in an Observable List:

this.OBSMailCountList = new ObservableCollection<IMailCount>(_allMailCounts);

The Problem is that the Collection is bind to an Live Diagramm and after the Copy in the List I get the

Error:

"The Value can not be NULL".

My Question is:

How to Copy an Observable Collection with Bindings in a Thread ?

解决方案

Your problem is that you have _allMailCounts == null at the moment you call observable collection constructor. You can check for null like this

if(_allMailCounts != null)
    OBSMailCountList = new ObservableCollection<IMailCount>(_allMailCounts);

Below is the answer on question "how to work with ObservableCollection from another tread":


Bind to observable collection defined as usual

ObservableCollection<IMailCount> _collection = new ObservableCollection<IMailCount>();
public ObservableCollection<IMailCount> Collection
{
    get { return _collection; }
    set
    {
        _collection = value;
        OnPropertyChanged();
    }
}

In another thread do work in this manner:

// create a copy as list in UI thread
List<IMailCount> collection = null;
Dispatcher.Invoke(() => collection = new List<IMailCount>(_collection));

// when finished working set property in UI thread
Dispatcher.InvokeAsync(() => Collection = new ObservableCollection<IMailCount>(collection));

这篇关于如何在线程中可观察的集合中复制列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:55