帮助我从vb.net中的类调用事件 [英] help me call an event from a class in vb.net

查看:215
本文介绍了帮助我从vb.net中的类调用事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将我的代码从c#转换到vb.net后,此代码给我一个错误


after convering my code from c# to vb.net, this code gives me an error


Private Sub VoiceRoom_Load(sender As Object, e As EventArgs)
            sound = New DirectSoundHelper()
            sound.OnBufferFulfill += New EventHandler(SendVoiceBuffer)
        End Sub




OnBufferFulfill是一个事件




OnBufferFulfill is an event

推荐答案

事件未调用,而是调用了它.调用是事件实例在事件调用列表中列出所有项目并调用每个项目,并且该项目是单例委托实例时的机制.这样,事件的所有处理程序都会被调用.

这种机制的重要局限性在于,只能在声明了even的类中进行调用,而在其他任何地方都不能进行调用,甚至在派生类中也不能进行调用.这是通过调用事件实例的Invoke方法来完成的.

您的代码没有尝试调用该事件;而且您可能永远都不需要它.您只需要调用在自己的类或结构中声明的事件即可.在您的代码中,您试图将处理程序添加到事件实例.首先,处理程序类型应与适当的委托类型匹配.我不知道什么是DirectSoundHelper类型,因此您需要为事件找到合适的委托类型.首先,学习如何在VB.NET中处理事件:
http://msdn.microsoft.com/en-us/library/ms973905.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/ms172877%28v = vs.80%29.aspx [ ^ ].

这是一篇简短的CodeProject文章:逐步指南: VB.NET [ ^ ].

另外,我怀疑您是否真的需要任何直接声音包装器?通常.NET就足够了.但这取决于您的目的,您没有与我们分享.这就是为什么从解释您的最终目标开始您的问题非常重要.

—SA
Event is not called, its is invoked. Invocation is the mechanism when the event instance lists all the item in the event invocation list and invokes each of the items, and the item is a single-case delegate instance; this way, all the handlers of the event get called.

The important limitation of this mechanism is that the invocation can only be done in the class where the even is declared, nowhere else, not even in a derived class. This is done via a call to event instance''s Invoke method.

Your code is not trying to invoke the event; and you probably never need it. You only need to invoke event you declare in your own class or structure. In your code, you are trying to add a handler to an event instance. First of all, the handler type should match appropriate delegate type. I don''t know what is the type DirectSoundHelper, so you need to find out appropriate delegate type for the event. First of all, learn how to work with events in VB.NET:
http://msdn.microsoft.com/en-us/library/ms973905.aspx[^],
http://msdn.microsoft.com/en-us/library/ms172877%28v=vs.80%29.aspx[^].

This is a short CodeProject article: Step by Step: Event handling in VB.NET[^].

Also, I doubt you really need any Direct Sound wrapper; usually .NET along is enough. But it depends on your purpose you did not share with us. That''s why it''s very important to start your questions with explanations of your ultimate goals.

—SA


这篇关于帮助我从vb.net中的类调用事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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