CollectionView 不支持从不同于调度程序线程的线程更改其源集合 - 由调度程序线程引起
CollectionView does not support changes to its source collection from a thread different from the dispatcher thread - caused from dispatcher thread
我有一个 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");
}
}
我有一个命令,运行s:
obj.Comments.Add(new Comment(Message));
其中 obj 是包含可观察集合的 class 的一个实例。
调用此行时,我遇到了以下异常:
System.NotSupportedException: 'This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.'
我打开了调试 > Windows > 线程面板,运行正在主线程上。我试过把它放在 App.Current.Dispatcher.Invoke(...) 里面,没有运气。
我不明白为什么会这样。更奇怪的是,我能够 运行 这很好,完全没有问题,在同一 class 的另一个实例上,它是同时创建的(返回并从我的数据库中一起创建同一个电话)。第一个我加评论没问题,每次都可以,但是其他的我都试过了。
在我的例子中,问题是在任务中刷新了集合视图。然后后来从 Main UI 线程添加到集合导致异常。
构建视图模型时,集合在延迟任务中刷新。
public MainVM()
{
//other code...
Task.Delay(100).ContinueWith(_ => UpdatePreferences());
}
public void UpdatePreferences()
{
//other code..
CollectionViewSource.GetDefaultView(Data.Customers).Refresh();
}
我能够通过调用调度程序解决问题。
Task.Delay(100).ContinueWith(_ => App.Current.Dispatcher.Invoke(()=> UpdatePreferences()));
我有一个 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");
}
}
我有一个命令,运行s:
obj.Comments.Add(new Comment(Message));
其中 obj 是包含可观察集合的 class 的一个实例。
调用此行时,我遇到了以下异常:
System.NotSupportedException: 'This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.'
我打开了调试 > Windows > 线程面板,运行正在主线程上。我试过把它放在 App.Current.Dispatcher.Invoke(...) 里面,没有运气。
我不明白为什么会这样。更奇怪的是,我能够 运行 这很好,完全没有问题,在同一 class 的另一个实例上,它是同时创建的(返回并从我的数据库中一起创建同一个电话)。第一个我加评论没问题,每次都可以,但是其他的我都试过了。
在我的例子中,问题是在任务中刷新了集合视图。然后后来从 Main UI 线程添加到集合导致异常。
构建视图模型时,集合在延迟任务中刷新。
public MainVM()
{
//other code...
Task.Delay(100).ContinueWith(_ => UpdatePreferences());
}
public void UpdatePreferences()
{
//other code..
CollectionViewSource.GetDefaultView(Data.Customers).Refresh();
}
我能够通过调用调度程序解决问题。
Task.Delay(100).ContinueWith(_ => App.Current.Dispatcher.Invoke(()=> UpdatePreferences()));