【WPF】弹窗定位、弹窗关闭后再打开的报错

需求:点击按钮,打开一个弹窗。

// 获得窗体实例
Window window = openDesignViewModel.View as Window; // 这是使用了WAF框架
//Window window = new Window();

// 设置弹出位置在屏幕中间
double screenHeight = SystemParameters.FullPrimaryScreenHeight;
double screenWidth = SystemParameters.FullPrimaryScreenWidth;
window.Top = (screenHeight - window.Height) / 2;
window.Left = (screenWidth - window.Width) / 2;

// 弹出弹窗
window.ShowDialog();

如果只写了以上代码,会发现弹窗正常弹出后,关闭该弹窗,再想打开时会报错。
说该窗体已被关闭时,无法调用ShowDialog()。

谷歌一下,解决方法:
http://stackoverflow.com/questions/8741632/how-to-re-open-the-closed-window

还要在这个窗体的后台代码添加窗体关闭时的一些操作:

protected override void OnClosing(CancelEventArgs e)
{
    e.Cancel = true;  // cancels the window close    
    this.Hide();      // Programmatically hides the window
}
posted @ 2016-12-26 10:42  霍莉雪特  阅读(535)  评论(0编辑  收藏  举报