需要帮助将C#转换为VB [英] Need help translating C# to VB
问题描述
我正在查看这个博客,我正在尝试将代码片段转换为VB。
I am looking at this blog, and I am trying to translate the snippet to VB.
我在此行遇到困难:
NotifyCollectionChangedEventHandler handlers = this.CollectionChanged;
注意:CollectionChanged是此事件( this是对 ObservableCollection< T>
)。
NOTE: CollectionChanged is an event of this ('this' is an override of ObservableCollection<T>
).
推荐答案
引发事件, OnCollectionChanged
应该可以正常工作。如果您想查询,则必须更加滥用并使用反射(对不起,示例是C#,但实际上应该是相同的-我在这里不使用任何特定于语言的功能):
To raise the event, OnCollectionChanged
should work fine. If you want to query it you have to get more abusive and use reflection (sorry, example is C# but should be virtually identical - I'm not using any language-specific features here):
NotifyCollectionChangedEventHandler handler = (NotifyCollectionChangedEventHandler)
typeof(ObservableCollection<T>)
.GetField("CollectionChanged", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(this);
et voila;处理程序(通过 GetInvocationList()
)。
et voila; the handler or handlers (via GetInvocationList()
).
基本上,在您的示例中(关于该帖子),使用:
So basically in your example (regarding that post), use:
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
这篇关于需要帮助将C#转换为VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!