如何覆盖WPF上的onclose事件?

我试图覆盖WPF上的onclose事件,这是我的代码到目前为止:

protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { base.OnClosing(e); e.Cancel = true; //do my stuff before closing } 

它执行代码但应用程序永远不会关闭。 任何想法如何解决这一问题?

应用程序永远不会关闭,因为您将e.Cancel设置为true

尝试

 protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { //do my stuff before closing base.OnClosing(e); } 

您要求不要通过设置e.Cancel = true来关闭它。 只是不要这样做。