Winforms -MVP-在表单接口中形式事件



我正在尝试在我正在开发的小型winforms应用中实现MVP。

已知,我需要定义一个IView接口。

IView接口应具有混凝土View加载时发射的事件。

此事件已经在Winforms中作为Load()事件植入。

现在在一些指南/教程中,我看到他们像这样实现了

Public Interface IView
    Event OnPrepareView()
    .....
End Interface
Public Class MainForm
Inherits Form
Implements IView
    Private Sub Main_Load(sender As Object, e As EventArgs) Handles Me.Load
        RaiseEvent OnPrepareView()
    End Sub
    Public Event OnPrepareView()
End Class

现在,我想知道是否可以直接在接口中直接暴露表单Load事件,因此IView看起来像:

Public Interface IView
    Event Load()
    .....
End Interface

和加载事件以其load事件的形式实现。

如果可能的话,我该如何在VB中进行?

update

例如,对于Icon或诸如Show之类的属性,该属性在System.Windows.Forms.Form中定义。

我将有一个IView

Public Interface IView
    Property Icon as Icon
    Sub Show()
End Interface

,然后在我的具体实施中

Public Class FooForm
Inherits Form
Implements IView
    Public Overloads Property Icon as Icon Implementes IView.Icon
    Get
        return MyBase.Icon
    End Get
    Set
        MyBase.Icon = value
    End Set
    Public Overloads Sub Show()
        MyBase.Show()
    End Sub
End Class

但是我该如何处理表单事件?

我应该在IView中声明新事件,例如:

Public Interface IView
    Property Icon as Icon
    Sub Show()
    Event OnLoad()
End Interface
Public Class FooForm
Inherits Form
Implements IView
....
....
    Public Event OnLoad() Implements IView.OnLoad
    Private Sub FooForm_Load() Handles Me.Load
        RaiseEvent OnLoad()
    End Sub
End Class

您应该在接口中声明事件,然后在Form中实现接口,然后覆盖OnLoad并提高事件。例如:

Public Interface IView
    Event Load As EventHandler
End Interface
Public Class Form1
    Implements IView
    Public Shadows Event Load As EventHandler Implements IView.Load
    Protected Overrides Sub OnLoad(e As EventArgs)
        MyBase.OnLoad(e)
        RaiseEvent Load(Me, e)
    End Sub
End Class

最新更新