简体   繁体   English

从界面引发事件(获胜表格)

[英]Raising Events From Interface (win forms)

My scenario: 我的情况:

Windows Forms Application with a base master (mdi) form. 具有基本主控(mdi)表单的Windows窗体应用程序。

An Interface that has an event: 具有事件的接口:

Public Interface IDoSomething

Event AddFilter()

Modal popup window implements the interface and decalres event: 模态弹出窗口实现interface和decalres事件:

Public Class frmPopup Implements IDoSomething

Public Event AddFilter() Implements IDoSomething.AddFilter

Popup also contains code to fire the event: 弹出窗口还包含触发事件的代码:

RaiseEvent AddFilter()

Base master form contains code that discovers and launches popup forms that implement a specified interface. 基本主表单包含用于发现并启动实现指定接口的弹出表单的代码。

A form in the application launches the popup (that implements the interface) and handle any events that it fires. 应用程序中的表单将启动弹出式窗口(实现该接口)并处理其触发的所有事件。 So I have the following code in the form: 所以我有以下形式的代码:

Public Class frmMyForm

Public WithEvents m_Popup As IDoSomething

Public Sub m_Popup_AddFilter() Handles m_Popup.AddFilter

    MsgBox("I'm in")

End Sub

The code is all working, up until the stage where the event is fired. 该代码一直有效,直到触发事件为止。 The popup loads without any issues but when the event fires it seems to drop off the face of the earth and is not being picked up by the main form - frmMyForm. 弹出窗口加载时没有任何问题,但是当事件触发时,弹出窗口似乎掉落了地球,并且没有被主要窗体frmMyForm拾取。 I suspect it may have something to do with the way the popup form is being launched from the base master form via the discovery of the interface. 我怀疑这可能与通过发现接口从基本主表单启动弹出表单的方式有关。

ADDITIONAL CODE - to expand on "Base master form contains code that discovers and launches popup forms that implement a specified interface": 附加代码-扩展为“基本主表单包含发现并启动实现指定接口的弹出表单的代码”:

The idea of the popup forms that are being used is to return a business object to the form that opened it using events. 正在使用的弹出表单的想法是使用事件将业务对象返回到打开它的表单。 The popup form Interface (IDoSomething) inherits another interface - IBusinessObjectSelector which specifies that the form will return a business object. 弹出表单接口(IDoSomething)继承了另一个接口-IBusinessObjectSelector,它指定表单将返回业务对象。

So the function in the base master form is: 因此,基本主表单中的函数为:

Public Function GetBusinessObjectUsingPopup(Of O, F As IBusinessObjectSelector)
               (ByRef dicPropertyValues As Dictionary(Of String, Object), 
                Optional ByVal titleText As String = "") 
                As O Implements IBaseMasterForm.GetBusinessObjectUsingPopup

Dim objBusinessObjectSelector As IBusinessObjectSelector = GetPopup(Of F)(False)       
    objBusinessObjectSelector.InitialiseForm()

    ' Activate and show the dialog
    If objBusinessObjectSelector.ShowPopup() <> Windows.Forms.DialogResult.OK Then
        ' The user cancelled the load, so just exit
        Return Nothing
    End If

    GetBusinessObjectUsingPopup = CType(objBusinessObjectSelector.SelectedBusinessObject, O)

End Function

And Popup Code: 和弹出代码:

Public Function GetPopup(Of F As IBasePopupChildForm)
            (Optional ByVal initialisePopupPriorToReturn As Boolean = True) As F 
             Implements IBaseMasterForm.GetPopup

    Dim lstIBasePopupChildForm As List(Of F) = GetInterfaces(Of F)()
            lstIBasePopupChildForm(0).MyIBaseMasterForm = Me
    If initialisePopupPriorToReturn Then
        lstIBasePopupChildForm(0).InitialiseForm()
    End If
    Return lstIBasePopupChildForm(0)
End Function

Note - GetInterfaces(Of F)() simply scans the assembly and returns a list of forms that implement the required interface. 注意-GetInterfaces(Of F)()仅扫描程序集并返回实现所需接口的表单列表。 Some validation has been chopped out that returns messages if multiple forms that implement the interface are found. 如果找到了实现该接口的多种形式,则会截断一些验证,这些验证会返回消息。

The critical part is initializing m_Popup correctly. 关键部分是正确初始化m_Popup。 You haven't said anything about that. 你什么都没说。 Some sample code: 一些示例代码:

Form2: 窗体2:

Public Class Form2
    Implements IDoSomething
    Public Event AddFilter() Implements IDoSomething.AddFilter

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent AddFilter()
    End Sub
End Class

Public Interface IDoSomething
    Event AddFilter()
End Interface

Form1: Form1中:

Public Class Form1
    Private WithEvents mPopup As IDoSomething

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim f2 As New Form2
        f2.Show(Me)
        mPopup = f2
    End Sub

    Private Sub mPopup_AddFilter() Handles mPopup.AddFilter
        MsgBox("yada")
    End Sub
End Class

The mPopup = f2 statement in this code is key. 此代码中的mPopup = f2语句是关键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM