在我编写的 C# 测试/学习应用程序中,我使用隐藏/显示属性来打开和关闭窗口。这是一个 WPF 应用程序。
在主窗口中,我有一个“关闭”按钮,触发此方法:
我通过这种方式确保所有的窗口都被关闭,包括隐藏的窗口。但现在有一个问题;当用户按下(在主窗口中)工具栏中的右上角红色X来关闭窗口时,只有主窗口会关闭,但背景中的隐藏窗口仍然存在。
因此,实际上有两个问题:
1.
在主窗口中,我有一个“关闭”按钮,触发此方法:
public void buttonQuit_Click(object sender, RoutedEventArgs e)
{
var message = exitmessage;
var title = exitTitle;
var result = MessageBox.Show(
message, // the message to show
title, // the title for the dialog box
MessageBoxButton.YesNo, // show two buttons: Yes and No
MessageBoxImage.Question); // show a question mark icon
// lets see what has been pressed
switch (result)
{
case System.Windows.MessageBoxResult.Yes: // Yes button pressed
CloseAllWindows();
break;
case System.Windows.MessageBoxResult.No: // No button pressed
break;
default: // Neither Yes nor No pressed (just in case)
MessageBox.Show("Oh noes! What did you press?!?!");
break;
}
}
我通过这种方式确保所有的窗口都被关闭,包括隐藏的窗口。但现在有一个问题;当用户按下(在主窗口中)工具栏中的右上角红色X来关闭窗口时,只有主窗口会关闭,但背景中的隐藏窗口仍然存在。
因此,实际上有两个问题:
1.
CloseAllWindows();
是否真的足以完全关闭应用程序?
2. 我如何“捕捉”用户按下工具栏中的红色X事件,并使其触发正确的关闭事件?