wpf中重写OnClosing窗口关闭事件报错

贴代码

  protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
            {
            
            //base.OnClosing(e);
            if (System.Windows.Forms.MessageBox.Show("确定是否关闭当前应用程序?", "提示", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                this.Close();  //重写父类的onClosing事件,通过this.closing关闭报错,
               }
            else
                {
                e.Cancel = true;
                this.Hide();
                }
           
            }

解决这个问题就是用base.Onclosing(e)代替 this.close()   用基类base关键词引用来关闭窗口,问题解决。


问题分析:(个人理解不一定准确,以供未来参考)system.windows.forms.messagebox.show() 弹出的是一个模态窗口,阻塞了当前的线程,当点击确定按钮时,模态窗口关闭(或隐藏回收),当前的this仍然引用的是已经消失的模态窗口,所以报错.


相关文章:

  • 2021-12-08
  • 2022-01-20
  • 2021-12-23
  • 2021-07-01
  • 2021-07-20
  • 2022-12-23
  • 2021-08-15
  • 2021-10-20
猜你喜欢
  • 2021-07-22
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案