在VB.NET中订阅事件 [英] Subscribing events in VB.NET

查看:133
本文介绍了在VB.NET中订阅事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些C#代码转换为VB.NET。



我在C#中有以下内容(其工作原理)

  m_switchImageTimer = new DispatcherTimer(); 
m_switchImageTimer.Interval = Interval;
m_switchImageTimer.Tick + =(s,e)=> LoadNextImage();

我正在努力转换成功的关键行是:

  m_switchImageTimer.Tick + =(s,e)=> LoadNextImage(); 

我尝试过的是:

  m_switchImageTimer.Tick + =函数(s,e)LoadNextImage()
m_switchImageTimer.Tick + =新的EventHandler(AddressOf LoadNextImage)
/ pre>

这两个都不工作。



第一次尝试产生两个错误,第一个VS2010亮点在m_switchImageTimer.Tick:


错误1'公共事件勾号(发件人作为对象,作为System.EventArgs)'是一个事件,不能直接叫使用RaiseEvent语句来引发事件


并在LoadNextImage()下突出显示会产生以下错误:


错误3表达式不会产生一个值。


第二个尝试产生相同的错误:


错误1'公共事件Tick(发件人作为对象,e As System.EventArgs)'是一个事件,并且不能直接调用。使用RaiseEvent语句来引发事件。


如何将C#代码转换为VB.NET?



感谢

解决方案

在VB.NET中订阅事件需要AddHandler或Handles关键字。您还将对lambda有问题,Function关键字需要一个返回值的表达式。在VS2010中,您可以这样写:

  AddHandler m_switchImageTimer.Tick,Sub(s,e)LoadNextImage()

在早期版本中,您需要更改LoadNextImage()方法声明或使用一些具有正确签名的私有助手方法:

  AddHandler m_switchImageTimer.Tick,AddressOf SwitchImageTimer_Tick 
...
Private Sub SwitchImageTimer_Tick(sender As Object ,e As EventArgs)
LoadNextImage()
End Sub


I'm trying to convert some C# code to VB.NET.

I have the following in C# (which works)

m_switchImageTimer = new DispatcherTimer();
m_switchImageTimer.Interval = Interval;
m_switchImageTimer.Tick += (s, e) => LoadNextImage();

The key line I'm struggling to convert successfully is:

m_switchImageTimer.Tick += (s, e) => LoadNextImage();

What I have tried is:

m_switchImageTimer.Tick += Function(s, e) LoadNextImage()
m_switchImageTimer.Tick += New EventHandler(AddressOf LoadNextImage)

Neither of these are working.

The first attempt produces two errors, the first VS2010 highlights under m_switchImageTimer.Tick:

Error 1 'Public Event Tick(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event

and highlighted under LoadNextImage() produces the following error:

Error 3 Expression does not produce a value.

The second attempt produces the same error:

Error 1 'Public Event Tick(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

How can I convert the C# code to VB.NET?

Thanks Ben

解决方案

Subscribing to events in VB.NET requires either the AddHandler or Handles keyword. You will also have a problem with the lambda, the Function keyword requires an expression that returns a value. In VS2010 you can write this:

    AddHandler m_switchImageTimer.Tick, Sub(s, e) LoadNextImage()

In earlier versions, you need to either change the LoadNextImage() method declaration or use a little private helper method with the correct signature:

    AddHandler m_switchImageTimer.Tick, AddressOf SwitchImageTimer_Tick
...
Private Sub SwitchImageTimer_Tick(sender As Object, e As EventArgs)
    LoadNextImage()
End Sub

这篇关于在VB.NET中订阅事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆