【发布时间】:2012-01-14 12:41:27
【问题描述】:
在我为测试/学习 C# 编写的应用程序中,我使用 hidden/visible 属性来打开和关闭窗口。 它是一个 WPF 应用程序。
在主窗口中,我有一个触发此方法的“关闭”按钮:
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 关闭时,只有该主窗口被关闭,但在后台隐藏的窗口仍然存在。
所以其实是2个问题:
CloseAllWindows();真的足以让应用 100% 关闭吗?当用户按下工具栏中的那个红色 X 时,我如何“捕捉”该事件,并使其也触发正确的关闭事件?
【问题讨论】: