WPF 单例窗口Close()后再Show()会报错,Close会调用Dispose,资源会马上被释放,ShowDialog()的情形是一样的。
重载OnClosing(CancelEventArgs e)方法,使用Hide()方法。
代码如下 | 复制代码 |
private static GHXTestControl _instance; private static readonly object ObjLok = new object(); public static GHXTestControl Instance() { lock (ObjLok) { return _instance ?? (_instance = new GHXTestControl()); } } /// /// protected override void OnClosing(CancelEventArgs e) { Hide(); e.Cancel = true; } private GHXTestControl() { InitializeComponent(); } |
调用:
代码如下 | 复制代码 |
private WindowPages.GanHuaxue.GHXTestControl _GHXControl; private void Click() //WindowPages.GanHuaxue.GHXTestControl t = new WindowPages.GanHuaxue.GHXTestControl(); } |