【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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值