【发布时间】:2017-09-13 15:43:41
【问题描述】:
我正在制作一个应用程序,用户可以在其中根据需要动态创建更多窗口。应用程序始终有 一个 MainWindow 实例,并且将有 0 个或多个 AuxWindow 实例。
添加更多 AuxWindow 实例可以正常工作。但是我在以编程方式再次关闭它们时遇到问题(即,如果用户想要减少辅助窗口的数量)。从 AuxWindow VM 调用 AuxWindow.Close() 也会触发 Window_Closing() 方法,该方法开始要求用户确认。
那么我如何区分用户关闭窗口和应用程序关闭窗口呢?如果用户关闭窗口,我想要求确认,然后关闭整个应用程序。但如果应用程序正在关闭窗口,我只想关闭它。
这是 AuxWindow 中的 Window_Closing() 方法:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
_logger.Info("Exit application initiated - closing window");
var result = _auxWindowVM.Controller.ExitApplication(this);
if (result == false) {
_logger.Info("Closing application confirmed");
e.Cancel = false;
}
else {
_logger.Info("Closing application canceled");
e.Cancel = true;
}
}
这是从 MainWindow 和所有 AuxWindow 实例调用的 ExitApplication 方法:
public bool ExitApplication(Window initWindow) {
if (_exitConfirmed == false) {
if (System.Windows.MessageBox.Show("Are you sure you want to exit?", "", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Question, System.Windows.MessageBoxResult.No) == System.Windows.MessageBoxResult.Yes) {
MainWindowVM.CurrentDateTimeUpdateTimer.Stop();
SerialPortHandler.SerialPortCts.Cancel();
ArgusMk2CommProtocol.QueueConsumerCts.Cancel();
PollingHandler.PollingTimer.Stop();
_exitConfirmed = true;
Application.Current.Shutdown();
return false;
}
else {
return true;
}
}
else {
return false;
}
}
【问题讨论】: