我是 Reactive Extension 的新手,不幸的是我们使用的是 VB.NET,所以我在将 C# 示例转换为 VB.NET 时遇到了一些麻烦。
我正在尝试创建一个简单的 ObservableCollection(of String) 并订阅它。每当将字符串添加到集合中时,我都想将其写出到控制台。
我正在尝试将此示例翻译为 VB.NET,但遇到了麻烦。
var x = Observable.FromEventPattern<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
handler => (sender, e) => handler(sender, e),
handler => theList.CollectionChanged += handler,
handler => theList.CollectionChanged -= handler);
}
我已经走了这么远,但无法让它发挥作用,我敢肯定这很简单,但我是新手。
Dim changes = Observable.FromEventPattern(Of System.Collections.Specialized.NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs) _
(Function(handler) Function(sender, args) handler(sender, args), _
Function(handler) AddHandler theList.CollectionChanged, AddressOf handler, _
Function(handler) RemoveHandler theList.CollectionChanged, AddressOf handler)
感谢@Gideon Engelberth 帮助我删除了不需要的AddressOf
这编译得很好:
Dim changes = Observable.FromEventPattern(Of
System.Collections.Specialized.NotifyCollectionChangedEventHandler, _
NotifyCollectionChangedEventArgs) _
(Function(handler) Sub(sender, args) handler(sender, args), _
Sub(handler) AddHandler theList.CollectionChanged, handler, _
Sub(handler) RemoveHandler theList.CollectionChanged, handler)