AddHandler对象

时间:2017-04-10 18:05:12

标签: vb.net object events observablecollection

我需要将collectionchanged的事件添加到集合对象:

Dim Coleccion As Object = New ObservableCollection(Of Entidad)

AddHandler Coleccion.CollectionChanged, AddressOf Coleccion_Cambiada

但它抛出:CollectionChanged不是对象的事件

所以,我试过了:

Dim Evento As EventInfo = Coleccion.GetType().GetEvent("CollectionChanged")

Evento.AddEventHandler(Coleccion, New EventHandler(AddressOf Coleccion_Cambiada))

但它抛出:无法将System.EventHandler转换为System.Collections.Specialized.NotifyCollectionChangedEventHandler

那么,我怎么能为一个我不知道的通用observablecollection添加一个事件呢?

感谢。

1 个答案:

答案 0 :(得分:0)

尝试使用反射添加事件处理程序时出现的错误是因为CollectionChanged事件不使用默认的事件处理程序。您可以使用CollectionChanged事件使用的特定事件处理程序来修复错误。

Evento.AddEventHandler(Coleccion, New System.Collections.Specialized.NotifyCollectionChangedEventHandler(AddressOf Coleccion_Cambiada))
相关问题