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仍然引用的是已经消失的模态窗口,所以报错.


猜你喜欢

转载自blog.csdn.net/ilovetheworld/article/details/79260302