事件,代表和接口-困惑 [英] Events, Delegates and Interfaces - Confused

查看:63
本文介绍了事件,代表和接口-困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义控件中得到了以下事件结构-这似乎是我在MSDN中的方案的推荐方法(尽管不容易遵循!)这是我使用的MSDN页面, MSDN-如何:在您的课堂中实现事件.. >

I have ended up with the following event structure in a custom control - it seemed to be the recommended way for my scenario in MSDN (though it wasn't easy to follow!) This is the MSDN page I used, MSDN - How to: Implement Events in Your Class.

Public Event CornerStyleChanged As CornerStyleChangedEventHandler
Public Delegate Sub CornerStyleChangedEventHandler(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)

Protected Overridable Sub OnCornerStyleChanged(ByVal e As PropertyChangedEventArgs)
    RaiseEvent CornerStyleChanged(Me, e)
End Sub

Public Overridable Property CornerStyle As CornerStyle Implements IProgressPainter.CornerStyle
    Get
        Return _CornerStyle
    End Get
    Set(ByVal value As CornerStyle)
        _CornerStyle = value
        OnCornerStyleChanged(New PropertyChangedEventArgs("CornerStyle"))
    End Set
End Property

现在我完全感到困惑的是(在设置上述模式时比我什至更多)在接口中声明什么?我想在接口中声明该事件,因为还会有其他几个类做类似的事情,并且它们 需要互换使用(因此首先要使用接口),如果我已经拒绝了接口路由,那么确保每个类必须在接口中声明的最小"内容似乎是明智的...

What now has me totally confused (even more so than I was about setting up the above pattern) is what to declare in the Interface? I wanted to declare the event in the interface since there will be several other classes that do similar things and they need to be used interchangeably (hence an Interface in the first place) and if I've gone down the Interface route it seems smart to make sure the 'minimum' content that each class will have to implement declared in the Interface...

迈克

推荐答案

要在界面中包含事件,只需添加"Event< eventname>((< eventargs> ;)".看 界面(Visual Basic) >声明接口.在您的情况下,应该是:

To include an event in an interface, you just include "Event <eventname>(<eventargs>)". See Interfaces (Visual Basic) > Declaring Interfaces. In your case, it would be:

Interface MyNiftyInterface    Event CornerStyleChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)    ' Other members ...'End Interface


就是说,您说您将需要几个"做类似事情"的类.如果此行为的实现也相同,则可以通过建立基础来使用继承 专门类继承的 class .如果需要,还可以将基类指定为MustInherit.看 必须继承(Visual Basic).


That said, you say you will need several class that "do similar things". If the implementation of this behavior is also the same, you may what to use inheritence by making a base class that the specialized classes inherit from. If you want, you can also specify the base class as MustInherit. See MustInherit (Visual Basic).


这篇关于事件,代表和接口-困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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