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()); } } /// <summary> 重写Close,窗口关闭时设置为隐藏。 /// </summary> protected override void OnClosing(CancelEventArgs e) { Hide(); e.Cancel = true; } private GHXTestControl() { InitializeComponent(); } |
调用:
代码如下 | 复制代码 |
private WindowPages.GanHuaxue.GHXTestControl _GHXControl; public WindowPages.GanHuaxue.GHXTestControl GHXControl { get { if (_GHXControl == null) _GHXControl = WindowPages.GanHuaxue.GHXTestControl.Instance(); return _GHXControl; } } private void Click() //WindowPages.GanHuaxue.GHXTestControl t = new WindowPages.GanHuaxue.GHXTestControl(); } |
时间: 2024-10-30 20:21:23