zoukankan      html  css  js  c++  java
  • C# WinCE项目 VS2008 单例窗体实现

    项目现有主界面FormMain,模板界面FormModel,其余5个子界面皆继承自模板。

    现在想要实现在主界面下可以打开任意子界面,并且可以随时关闭。当打开的子窗体未执行Close事件时,要保证每次显示的是同一个子窗体的实例化。

    在Windows项目中基本代码如下:

    private static FrmSub1 instance;  
    public static FrmSub1 GetFrmSub1()  
    {  
      //判断是否存在该窗体,或时候该字窗体是否被释放过,如果不存在该窗体,则 new 一个子窗体  
      if (instance == null || instance.IsDisposed)  
        {  
          instance = new FrmSub1();  
        }  
      return instance;  
    }  

    但是在WinCE项目中没有IsDisposed属性,所以当Get到的窗体执行Close()事件释放后,instance不为null,进而得不到新的实例化赋值给instance。所以WinCE中代码当如下:

    public static Form_FileManage file;
    public
    static Form_FileManage GetFile() {   if (file == null)     {       file = new Form_FileManage();     }   return file; }

    调用时要注意,每次调用都要注册一个Closed事件,因为每次file窗体Close后,执行Get方法时得到的都是一个新的实例化,就要重新绑定一次Closed事件,否则下次file关闭后,Get方法就再也打不开新的子窗体了。调用时代码如下:

    GetFile().Show();
    GetFile().Closed += new EventHandler(FormFile_Closed);

    FormFile_Closed事件代码如下:

    public static void FormFile_Closed(object sender, EventArgs e)
    {
         file = null;
    }
  • 相关阅读:
    rsync 安装使用详解
    shell全备份脚本(借鉴别人的,在其基础上修复完善了bug)
    完全备份、差异备份以及增量备份的区别
    云主机格式化和挂载数据盘
    JSONP跨域
    php的多线程使用
    tp其他功能
    Zend Guard Loader和Zend Optimizer的安装(更新中)
    前端编码规范
    前端优化
  • 原文地址:https://www.cnblogs.com/npucloud/p/5709282.html
Copyright © 2011-2022 走看看