我在WPF中有一个应用程序,我想在另一个线程设置setter时清除ObservableCollection。
这就是setter。
public List<Model.MasterReview> SelectedMasterReviews
{
get { return selectedMasterReviews; }
set
{
selectedChildReview = null;
selectedMasterReviews = value;
Application.Current.Dispatcher.Invoke((Action)delegate
{
GetChildReviews();
});
}
}
private void GetChildReviews()
{
Application.Current.Dispatcher.Invoke((Action)delegate
{
ChildReviews.Clear();
});
}
我得到了一个错误。
This type of CollectionView does not support changes to its SourceCollection from a thread
different from the Dispatcher thread
这段代码给了我同样的错误。
var uiContext = SynchronizationContext.Current;
uiContext.Send(x => ChildReviews.Clear(), null);
我已经解决了这个问题.第一次ChildReviews在另一个线程中被清除。
Task.Run(() => Application.Current.Dispatcher.Invoke((Action)delegate
{
GetChildReviews();
}); )
然后又在另一个线程中被清除了
Application.Current.Dispatcher.Invoke((Action)delegate
{
GetChildReviews();
});
我不得不删除 Task.Run()
现在可以了