[英]VB.NET equivalent of C# event hook up
This is regarding interop toolkit so UC inside another UC is a given. 这是关于互操作性工具包的,因此给定了另一个UC内部的UC。
I have a user control that fires up an event on button. 我有一个用户控件,可以触发按钮上的事件。
I've added a handler to handle it in container user control 我在容器用户控件中添加了一个处理程序来处理它
Dim uc As New LoginUC.Login
AddHandler uc.OkClick, AddressOf InteropUserControl_UserControlOk
Now I need to raise this event in the parent user control to send it over to VB6 container form. 现在,我需要在父用户控件中引发此事件,以将其发送到VB6容器表单。
Public Event UserControlOk()
Private Sub InteropUserControl_UserControlOk(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.UserControlOk
RaiseEvent UserControlOk()
End Sub
The problem that i see is that once the button is clicked this event keeps firing off non stop. 我看到的问题是,一旦单击按钮,此事件就会不断触发。 In c# this would be handled by 在C#中,这将由
if (this.event != null)
this.event(sender,e);
type of code. 代码类型。 Any advice how that can be done is VB.NET? VB.NET有什么建议可以做到吗?
UPDATE: 更新:
Private Sub XamlForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' Create the ElementHost control for hosting the
' WPF UserControl.
Dim host As New ElementHost()
host.Dock = DockStyle.Fill
' Create the WPF UserControl.
Dim uc As New LoginUC.Login
AddHandler uc.OkClick, AddressOf InteropUserControl_UserControlOk
' Assign the WPF UserControl to the ElementHost
' control's Child property.
host.Child = uc
' Add the ElementHost control to the form's
' collection of child controls.
Me.Panel1.Controls.Add(host)
End Sub
This is the code I use to add UC to vb.net user control. 这是我用来将UC添加到vb.net用户控件的代码。 Why would addhandler fire up multiple times? 为什么addhandler会多次启动?
Maybe rising the event re-triggers the UC's event again, somehow. 也许上升事件会以某种方式再次触发UC的事件。 Prevent it by adding using a guard variable 通过使用保护变量添加来防止它
Dim raisingEvent As Boolean = False
Private Sub InteropUserControl_UserControlOk(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.UserControlOk
If Not raisingEvent Then
raisingEvent = True
RaiseEvent UserControlOk()
raisingEvent = False
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.