使用单例模式的窗口

有时候我们需要使用单例模式的窗口,如监控窗口等。

写成如下形式:

 if (ucsInstance == null || ucsInstance.IsDisposed)
            {
                ucsInstance = new ucTongXun();
            }
         
            return ucsInstance;

但是这样还没有完全搞定,因为这个窗口关闭的话,相当于isDisposed等于true,就是释放了,那么达不到实例化的目的,因此需要在释放函数中,就是在designer.cs中找到

protected override void Dispose(bool disposing)        

{            

  if (disposing && (components != null))            

  {            

     components.Dispose();            

  }            

  base.Dispose(disposing);     

}

将其改为

protected override void Dispose(bool disposing)        

{            

  base.Dispose(disposing);     

}

这样的话,即使窗口关闭,他也不会被释放,马上就能实例化出来。

posted @ 2013-12-04 16:37  苗苗她爹  阅读(370)  评论(0编辑  收藏  举报