VB.net 中的响应式

发布于 2024-11-17 23:20:27 字数 385 浏览 1 评论 0原文

任何人都可以帮助我吗!

我尝试在 C# 中实现一些内容:

var keysIO =   Observable.FromEvent<KeyDownEventHandler, RoutedEventArgs>(
                                h => new KeyDownEventHandler(h),
                                h => btn.KeyDown += h,
                                h => btn.KeyDown -= h));

How to write that in VB?

谢谢你, 右

Can anyone help me please!

I tried to implement something that in C# is:

var keysIO =   Observable.FromEvent<KeyDownEventHandler, RoutedEventArgs>(
                                h => new KeyDownEventHandler(h),
                                h => btn.KeyDown += h,
                                h => btn.KeyDown -= h));

How to write that in VB?

Thank you,
R

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心不设防 2024-11-24 23:20:27

我首先让你的代码在我的机器上编译:

var keysIO = Observable.FromEvent<KeyEventHandler,KeyEventArgs>(
                                    h => btn.KeyDown += h, 
                                    h => btn.KeyDown -= h);

然后我在 Reflector 中打开它并修改为

Dim keysIO As IObservable(Of KeyEventArgs) = _
        Observable.FromEvent(Of KeyEventHandler, KeyEventArgs)( _
            (Sub(h) AddHandler btn.KeyDown, h), _
            (Sub(h) RemoveHandler btn.KeyDown, h))

I first got your code to compile on my machine:

var keysIO = Observable.FromEvent<KeyEventHandler,KeyEventArgs>(
                                    h => btn.KeyDown += h, 
                                    h => btn.KeyDown -= h);

Then I opened it in Reflector and modfied to

Dim keysIO As IObservable(Of KeyEventArgs) = _
        Observable.FromEvent(Of KeyEventHandler, KeyEventArgs)( _
            (Sub(h) AddHandler btn.KeyDown, h), _
            (Sub(h) RemoveHandler btn.KeyDown, h))
北陌 2024-11-24 23:20:27

C# btn.KeyDown += h 对应于VB 中的以下内容:

AddHandler btn.KeyDown, AddressOf h

同样,-= 对应于RemoveHandler。

但是,当使用不直接是通用 EventHandler(Of T) 的自定义 EventHandler 类型时,VB 编译器会出现问题。对于您的示例,我不确定自定义 KeyDownEventHandler 类型在转换参数中正在做什么。如果您不需要它,您可以使用弱类型版本:

Dim keysIO = Observable.FromEventPattern(Of KeyDownEventHandler, RotedEventArgs)(btn, "KeyDown")

The C# btn.KeyDown += h corresponds to the following in VB:

AddHandler btn.KeyDown, AddressOf h

Similarly, -= corresponds to RemoveHandler.

However, the VB compiler has issues when using custom EventHandler types that are not directly the generic EventHandler(Of T). In the case of your sample, I"m not sure what the custom KeyDownEventHandler type is doing, in the conversion parameter. If you don't need it, you may be able to use the weakly typed version:

Dim keysIO = Observable.FromEventPattern(Of KeyDownEventHandler, RotedEventArgs)(btn, "KeyDown")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文