/// <summary> /// 向缓存中添加一个新面板 /// </summary> /// <param name="PanelType">面板类型对象</param> /// <param name="ID">面板ID</param> /// <param name="Panel">面板对象</param> private int AddPanel(object ID, IPanelCommonSet Panel) { if (PanelBuffer.ContainsKey(ID)) { return(-1); } Panel.ID = ID; Panel.OwnerPage = NewTabPage(Panel as UserControl); this.PanelBuffer.Add(ID, Panel); return(1); }
/// <summary> /// 获取指定ID的面板,若不存在,则创建一个新面板,新面板将加入缓存 /// </summary> public T GetPanel <T>(object ID) where T : UserControl, IPanelCommonSet, new() { IPanelCommonSet Result = null; do { if (ID == null) { ErrMes = "面板ID参数不允许为空!"; break; } Result = PanelBuffer[ID] as IPanelCommonSet; if (!(Result is T)) { ErrMes = string.Format("已存在一个ID为<{0}>,类型为<{1}>的面板!", ID, typeof(T).Name); } if (Result != null) { break; } #if DEBUG try #endif { Result = new T(); } #if DEBUG catch (Exception ex) { this.ErrMes = "面板初始化出现异常!"; } #endif }while (false); if (Result == null) { MessageBox.Show(this.ErrMes); } else { AddPanel(ID, Result); } return(Result as T); }
/// <summary> /// 获取指定的面板。若不存在,则使用指定类型对象创建一个新面板,新面板将加入缓存 /// </summary> /// <param name="ID">面板ID</param> /// <param name="PanelType">面板类型</param> /// <returns>面板对象</returns> public IPanelCommonSet this[object ID, Type PanelType] { get { IPanelCommonSet Result = null; do { if (ID == null) { ErrMes = "面板ID参数不允许为空!"; break; } Result = PanelBuffer[ID] as IPanelCommonSet; if (Result != null) { if (!(Result.GetType() == PanelType)) { ErrMes = string.Format("已存在一个ID为<{0}>,类型为<{1}>的面板!", ID, PanelType.Name); Result = null; } break; } if (PanelType == null) { this.ErrMes = "PanelType参数不允许为空!"; break; } if (!PanelType.IsSubclassOf(typeof(UserControl)) || PanelType.GetInterface("IPanelCommonSet", false) == null) { this.ErrMes = "PanelType参数指定的类型应继承自UserControl并实现IPanelCommonSet接口!"; break; } #if !DEBUG try #endif { Result = System.Activator.CreateInstance(PanelType) as IPanelCommonSet;; } #if !DEBUG catch (Exception ex) { this.ErrMes = "面板初始化出现异常,请确保面板类型有一个公共无参构造函数!"; } #endif }while (false); if (Result == null) { MessageBox.Show(this.ErrMes); } else { AddPanel(ID, Result); } return(Result); } }