C#:从 ObservableCollection 中删除事件?
我有一个包含 Items 的 ObserableCollection,它实现了 INotifyPropertyChanged 接口。 这就是我创建它的方式:
var myCollection = new ObservableCollection<MyViewModel>();
myCollection.CollectionChanged += OnCollectionChanged;
_
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (ViewModelBase item in e.NewItems)
{
item.PropertyChanged -= myViewModelPropertyChanged;
}
}
else if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (ViewModelBase item in e.NewItems)
{
item.PropertyChanged += myViewModelPropertyChanged;
}
}
}
}
现在我的问题是,我是否需要循环遍历所有项目并在执行另一个项目之前删除“myViewModelPropertyChanged”,
myCollection = new ObservableCollection<MyViewModel>();
还是会自动发生?怎么样一个 myCollection.Clear();
感谢您的任何意见。
干杯 约瑟夫
I've a ObserableCollection with Items in it, which implement the INotifyPropertyChanged Interface.
This is how I create it:
var myCollection = new ObservableCollection<MyViewModel>();
myCollection.CollectionChanged += OnCollectionChanged;
_
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (ViewModelBase item in e.NewItems)
{
item.PropertyChanged -= myViewModelPropertyChanged;
}
}
else if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (ViewModelBase item in e.NewItems)
{
item.PropertyChanged += myViewModelPropertyChanged;
}
}
}
}
Now my question is, doI need to loop trough all items and remove the "myViewModelPropertyChanged" before doing another
myCollection = new ObservableCollection<MyViewModel>();
or does this happen automatically? What about a
myCollection.Clear();
Thanks for any input.
Cheers
Joseph
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话
你有一个对象集合。对于每个对象,您都订阅了一些成员事件。现在您的对象拥有对处理程序函数的引用。
当您清除集合时,如果没有其他人引用该成员对象,则无需显式取消订阅。这些对象将被垃圾收集。
当发布者比订阅者寿命更长时,就会出现问题,在这种情况下,由于事件处理程序/委托引用,它使订阅者保持活动状态。例如,如果您将发布者存储在静态字段中,如果您不显式取消订阅,则 GC 不会收集订阅者。
另请参阅:1198076 和其他相关问题。
If I understand correctly,
you have a Collection of objects. For each object, you have subscribed to some member events. Now your object has a reference to your handler functions.
When you clear the collection, if no one else references the member objects, you don't need to unsubscribe explicitly. The objects will get garbage collected.
The problem occurs when the publisher outlives the subscriber, in which case it keeps the subscriber alive due to the event handler/delegate references. e.g. if you store the publisher in a static field, if you don't unsubscribe explicitly - the subscriber will not be collected by the GC.
See also: 1198076 and other related questions.
我认为 myCollection.Clear();或者在旧的 myCollection 上创建一个新的 myCollection 不会取消事件集合的对象的链接,然后我将进行迭代
I think that a myCollection.Clear(); or create a new myCollection over the old one won't unlink the objects of your collection of the event, and then I would do the iteration