相关疑难解决方法(0)

如何通过工作线程更新ObservableCollection?

我有一个ObservableCollection<A> a_collection;该集合包含'n'个项目.每个项目A看起来像这样:

public class A : INotifyPropertyChanged
{

    public ObservableCollection<B> b_subcollection;
    Thread m_worker;
}
Run Code Online (Sandbox Code Playgroud)

基本上,它都连接到WPF列表视图+详细信息视图控件,它在单独的列表视图中显示所选项目的b_subcollection(双向绑定,propertychanged上的更新等).当我开始实现线程时,问题出现了.整个想法是让整个a_collection使用它的工作线程来"工作",然后更新它们各自的b_subcollections并让gui实时显示结果.

当我尝试它时,我得到一个例外,说只有Dispatcher线程可以修改ObservableCollection,并且工作停止了.

任何人都可以解释这个问题,以及如何解决它?

干杯

c# wpf multithreading observablecollection

71
推荐指数
4
解决办法
6万
查看次数

无法创建应用程序存档。这种类型的 CollectionView 不支持从与该 CollectionView 不同的线程更改其 SourceCollection

在 Visual Studio 2017 中,尝试存档 Xamarin android 项目时,成功构建后出现以下错误:

无法创建应用程序存档“MyArchive”。这种类型的 CollectionView 不支持从与 Dispatcher 线程不同的线程更改其 SourceCollection。

还有其他人看到过这个错误吗?

c# archiving visual-studio xamarin

5
推荐指数
1
解决办法
1899
查看次数

CollectionView 不支持从与调度程序线程不同的线程更改其源集合 - 由调度程序线程引起

我有一个 ObservableCollection 和一个使用 OC 作为源的 ICollectionView:

private ObservableCollection<Comment> _Comments = new ObservableCollection<Comment>();
/// <summary>
/// Comments on the account
/// </summary>
[BsonElement("comments")]
public ObservableCollection<Comment> Comments
{
    get
    {
        return _Comments;
    }
    set
    {
        _Comments = value;
        OnPropertyChanged("Comments");
        OnPropertyChanged("CommentsSorted");
    }
}
private ICollectionView _CommentsSorted;
/// <summary>
/// Sorted list (reverse order) of the comments
/// </summary>
[BsonIgnore]
public ICollectionView CommentsSorted
{
    get
    {
        return _CommentsSorted;
    }
    set
    {
        _CommentsSorted = value;
        OnPropertyChanged("CommentsSorted");
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一个命令,运行:

obj.Comments.Add(new Comment(Message));
Run Code Online (Sandbox Code Playgroud)

其中 obj 是包含可观察集合的类的实例。

调用此行时,我遇到以下异常: …

c# multithreading observablecollection collectionview

1
推荐指数
1
解决办法
4882
查看次数