简体   繁体   English

如何将自定义事件从子窗口触发回到父窗口?

[英]How do I fire a custom event from a child window back to parent window?

I am looking to close the application when I click a cancel button in a login page, but I don't want to do it in such a way that Window 2 closes itself, but by sending some notification to Main Window, and Main Window closes the application. 我希望在单击登录页面中的取消按钮时关闭该应用程序,但是我不想以Window 2关闭自身的方式来执行此操作,而是通过向主窗口发送一些通知,然后关闭Main Window应用程序。 Here's the code that I have so far: 这是我到目前为止的代码:

(in loginPage) (在loginPage中)

Public Event CloseApp As EventHandler

Private Sub CancelButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles CancelButton.Click

    DialogResult = False
    RaiseEvent CloseApp(Me, New EventArgs)
End Sub

(In Main Window) (在主窗口中)

Public loginPage As New LoginPage

Public Sub New()
    InitializeComponent()
    AddHandler loginPage.CloseApp, AddressOf Me.ShutDownJobChangeWindow
End Sub

Private Sub ShutDownJobChangeWindow(ByVal sender As Object, ByVal e As EventArgs)
    Application.Current.Shutdown()
End Sub

This does not work. 这是行不通的。 I am new to handling events so if anyone could help explain what's wrong with this I would be very grateful to you! 我是处理事件的新手,所以如果有人可以帮助解释这是什么问题,我将非常感谢您!

P/S: I am not too sure about C# too would appreciate code in VB.NET. P / S:我不太确定C#是否会欣赏VB.NET中的代码。

You should have a look into your Project Settings / Application / Shutdown Mode, and have it set to 'On explicit shutdown'. 您应该查看项目设置/应用程序/关机模式,并将其设置为“显式关机时”。

(i assume this is the instance of LoginPage that is hooked to the event handler that is raising the event.) (我假设这是LoginPage的实例,该实例已挂接到引发事件的事件处理程序中。)
try also to set a breakpoint in event handler to see if it is reached. 还尝试在事件处理程序中设置一个断点,以查看是否达到断点。

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

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