Winfrom统一单例窗口

//调用方式 

var frm = new MyForm().Instance();


public static class ExFrm
{
    static Dictionary<string, Form> forms = new Dictionary<string, Form>();

     

    /// <summary>
    /// 单例模式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="frm"></param>
    /// <returns></returns>
    public static T Instance<T>(this T frm) where T : Form
    {
        frm.FormClosed += (s, e) => frm.Dispose();
        if (forms.ContainsKey(frm.Name) == false)
        {
            forms.Add(frm.Name, frm);
            return frm;
        }
        if (forms[frm.Name].IsDisposed)
        {
            forms[frm.Name] = frm;
            return frm;
        }
        frm.Close();
        return forms[frm.Name] as T;
    }

}

 

上一篇:资源管理


下一篇:netty篇-数据传输方式