从 CollectionChanged 事件更改集合
我想在更改后更新集合,但我似乎无法“摆脱”此异常:
在 CollectionChanged 或 PropertyChanged 事件期间无法更改 ObservableCollection。
在事件处理程序内,我取消订阅 Collection Changed 事件在更改任何内容以防止无限循环之前以及进行更改之后,我再次订阅同一事件。
private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
data.CollectionChanged -= CollectionChanged;
data.Add("Item");
data.CollectionChanged += CollectionChanged;
}
我尝试使用 Dispatcher 调用 data.Add("Item"),但没有运气:(
I want to update collection after it was changed but I can't seem to get "away" from this exception:
Cannot change ObservableCollection during a CollectionChanged or PropertyChanged event.
Inside event handler I unsubscribe from Collection changed event before changing anything to prevent infinite loops and after changes are made i subscribe again to same event.
private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
data.CollectionChanged -= CollectionChanged;
data.Add("Item");
data.CollectionChanged += CollectionChanged;
}
I tried using Dispatcher to call data.Add("Item"), but no luck :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您正在取消订阅尚未完成的活动中的活动。回过头来重新评估添加到集合中的原因,并确定是否有其他方法可以完成您的需求。
The problem is that you are unsubscribing from the event within the event which has yet to complete. Drop back and re-evaluate why you are adding to the collection and determine if there is another way to accomplish what you need.