/// <summary> /// 生成一个新的HostDesign,并且进行加载。 /// </summary> /// <param name="formName">所创建的设计页面的名称</param> /// <returns> 返回一个加载了设计器的HostControl对象</returns> public HostControl GetNewHost(string formName) { try { HostDesign hostSurface = (HostDesign)this.CreateDesignSurface(this.ServiceContainer); CodeDomHostLoader codeDomHostLoader = new CodeDomHostLoader(formName); hostSurface.BeginLoad(codeDomHostLoader); //使用给定的设计器加载程序开始加载过程。 hostSurface.Initialize(); return(new HostControl(hostSurface)); } catch (Exception ex) { return(null); } }
/// <summary> /// 读入新的Xml文件时创建新的设计器, /// 对读入的xml文档进行处理,如果xml文档的名称和设计器Form的名称不一致,则将设计器的名称设为和xml文档的名称一致。 /// 同时,判断输入的xml文档的格式是否为正确的描述文件; /// 对于描述文件中有控件叠加的信息,则将需叠加的控件的信息添加到设计器中。 /// </summary> /// <param name="fileName">要读入的xml文件的名称/param> /// <returns></returns> public HostControl GetNewDesignHost(string fileName) { HostDesign hostSurface = (HostDesign)this.CreateDesignSurface(this.ServiceContainer); //判断文件名称和文件中的CassView对象是否一致 string designName = Path.GetFileNameWithoutExtension(fileName); string documentDesignName = designName; XmlDocument document = new XmlDocument(); try { document.Load(fileName); XmlNode node = document.FirstChild; for (int i = 0; i < node.ChildNodes.Count; i++) // 对读入的xml文档进行处理,如果xml文档的名称和设计器Form的名称不一致,则将设计器的名称设为和xml文档的名称一致。 { if (node.ChildNodes[i].Name.Equals("Object")) { if (node.ChildNodes[i].Attributes["type"] == null) { //CassMessageBox.Warning("描述文件格式错误!"); return(null); } else { string[] type = node.ChildNodes[i].Attributes["type"].Value.Split(','); if (type.Length > 0 && type[0].Equals(PublicVariable.viewName)) { if (node.ChildNodes[i].Attributes["name"] != null) { documentDesignName = node.ChildNodes[i].Attributes["name"].Value; if (!documentDesignName.Equals(designName))//如果名字不相同,则进行修改 { node.ChildNodes[i].Attributes["name"].Value = designName; for (int j = 0; j < node.ChildNodes[i].ChildNodes.Count; j++) //修改属性节点中的名称值 { if (node.ChildNodes[i].ChildNodes[j].Name.Equals("Property") && node.ChildNodes[i].ChildNodes[j].Attributes["name"] != null && node.ChildNodes[i].ChildNodes[j].Attributes["name"].Value.Equals("Name")) { node.ChildNodes[i].ChildNodes[j].InnerText = designName; break; } } } } break; }//cassView节点 else { return(null); } } //type != null } //object } //修改名称问题 if (document != null) { CodeDomHostLoader codeDomHostLoader = new CodeDomHostLoader(designName); hostSurface.BeginLoad(codeDomHostLoader); if (!codeDomHostLoader.MyPerformLoad(document)) //false(加载失败),跳出 { return(null); } hostSurface.Initialize(); //已加载后才可进行初始化操作 IDesignerHost host = (IDesignerHost)hostSurface.GetService(typeof(IDesignerHost)); }//xml文档不为空 return(new HostControl(hostSurface)); } catch (Exception ex) { } return(null); }