我不知道爲什麼,但是當我調用窗體上的close()方法時,它會自動調用Window_Closing()方法,但是爲什麼?爲什麼關閉表單調用Window_Closing()方法?
我追溯了代碼,實際上它會自動調用它,但我不確定。代碼
相關片段(從叫做Visual C#2010分步書):
namespace BellRingers
{
public partial class MainWindow : Window
{
private string[] towers = { "Great Shevington", "Little Mudford",
"Upper Gumtree", "Downley Hatch" };
private string[] ringingMethods = { "Plain Bob", "Reverse Canterbury",
"Grandsire", "Stedman", "Kent Treble Bob", "Old Oxford Delight",
"Winchendon Place", "Norwich Surprise", "Crayford Little Court"};
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult key = MessageBox.Show("Are you sure you want to quit",
"Confirm",
MessageBoxButton.YesNo,
MessageBoxImage.Question,
MessageBoxResult.No);
e.Cancel = (key == MessageBoxResult.No);
}
private void exit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
好吧,這是'Window_Closing'方法的用途。無論如何,在窗口關閉時調用。 –
當你調用'Close'方法時,它會增加'OnClosing'事件,然後'OnClosed'。這是打算。你可以做的是當用戶關閉或退出按鈕時,添加一些'bool'標誌來檢查'Closing'(或者看看'e.CloseReason')。 – Sinatr
你的期望是什麼?爲什麼? 「窗口」是......「關閉」。 –