我的场景:
具有基本主 (mdi) 窗体的 Windows 窗体应用程序。
具有事件的接口:
Public Interface IDoSomething
Event AddFilter()
模态弹窗实现接口和贴花事件:
Public Class frmPopup Implements IDoSomething
Public Event AddFilter() Implements IDoSomething.AddFilter
弹出窗口还包含触发事件的代码:
RaiseEvent AddFilter()
基本主表单包含发现和启动实现指定接口的弹出表单的代码。
应用程序中的表单启动弹出窗口(实现接口)并处理它触发的任何事件。所以我在表单中有以下代码:
Public Class frmMyForm
Public WithEvents m_Popup As IDoSomething
Public Sub m_Popup_AddFilter() Handles m_Popup.AddFilter
MsgBox("I'm in")
End Sub
代码一切正常,直到触发事件的阶段。弹出窗口加载没有任何问题,但是当事件触发时,它似乎从地球表面掉了下来,并且没有被主窗体-frmMyForm 拾取。我怀疑这可能与通过发现接口从基本主表单启动弹出表单的方式有关。
附加代码 - 扩展“基本主表单包含发现和启动实现指定接口的弹出表单的代码”:
正在使用的弹出表单的想法是将业务对象返回到使用事件打开它的表单。弹出表单接口 (IDoSomething) 继承了另一个接口 - IBusinessObjectSelector,它指定表单将返回一个业务对象。
所以基本主表单中的功能是:
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
和弹出代码:
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
注 - GetInterfaces(Of F)() 只是扫描程序集并返回实现所需接口的表单列表。如果找到实现该接口的多个表单,则某些验证已被删除,这些验证将返回消息。