我正在看this https://web.archive.org/web/20200815114302/http://geekswithblogs.net/NewThingsILearned/archive/2008/01/16/listcollectionviewcollectionview-doesnt-support-notifycollectionchanged-with-multiple-items.aspx博客,我正在尝试将代码片段翻译为 VB。
我在这条线上遇到困难:
NotifyCollectionChangedEventHandler handlers = this.CollectionChanged;
注意:CollectionChanged 是 this 的一个事件(“this”是对ObservableCollection<T>
).
To raise事件,OnCollectionChanged
应该可以正常工作。如果你想query如果你必须更加粗暴地使用反射(抱歉,示例是 C#,但实际上应该是相同的 - 我在这里没有使用任何特定于语言的功能):
NotifyCollectionChangedEventHandler handler = (NotifyCollectionChangedEventHandler)
typeof(ObservableCollection<T>)
.GetField("CollectionChanged", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(this);
等等瞧;一个或多个处理程序(通过GetInvocationList()
).
所以基本上在你的例子中(关于那篇文章),使用:
Protected Overrides Sub OnCollectionChanged(e As NotifyCollectionChangedEventArgs)
If e.Action = NotifyCollectionChangedAction.Add AndAlso e.NewItems.Count > 1 Then
Dim handler As NotifyCollectionChangedEventHandler = GetType(ObservableCollection(Of T)).GetField("CollectionChanged", BindingFlags.Instance Or BindingFlags.NonPublic).GetValue(Me)
For Each invocation In handler.GetInvocationList
If TypeOf invocation.Target Is ICollectionView Then
DirectCast(invocation.Target, ICollectionView).Refresh()
Else
MyBase.OnCollectionChanged(e)
End If
Next
Else
MyBase.OnCollectionChanged(e)
End If
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)