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

来源:互联网 发布:橡树岛宝藏 知乎 编辑:程序博客网 时间:2024/05/01 04:30

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

// 获得窗体实例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}
0 0
原创粉丝点击