2011-01-06 115 views
20

我知道這個問題非常類似於this one,但我會想到下面的代碼會取消關閉請求,但它不會?誰能解釋爲什麼?取消WPF中的窗口關閉

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);    

     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      Application.Current.Shutdown(); 
     } 

     void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
     { 
      e.Cancel = true; 
     }   
    } 
} 

回答

25

MSDN

重要:當關閉被調用時, 應用程序將不考慮任何打開的窗口關閉 事件是否取消關閉 。