【C#】关闭 Window 之后,无法设置 Visibility,也无法调用 Show、ShowDialogor 或 WindowInteropHelper.EnsureHandle

问题:

在做WPF项目时,点击一个按钮弹出一个自定义的窗体,然后点击X关闭该窗体,然后再点击按钮想弹出该窗体时,报错:关闭 Window 之后,无法设置 Visibility,也无法调用 Show、ShowDialogor 或 WindowInteropHelper.EnsureHandle。 
这里写图片描述


解决办法:

需要重写它的OnClosing()方法。

protected override void OnClosing(CancelEventArgs e)
{
    e.Cancel = true;  // cancels the window close    
    this.Hide();      // Programmatically hides the window
}

猜你喜欢

转载自blog.csdn.net/xionglifei2014/article/details/81068686
今日推荐