public void LoadFromFileName(string fileName) { Stream stream = new FileStream(fileName, FileMode.Open); BinaryReader binaryReader = new BinaryReader(stream, System.Text.Encoding.Unicode); byte[] buffer = binaryReader.ReadBytes((int)stream.Length); //string xmlContent = File.ReadAllText(fileName); try { string xmlContent = System.Text.Encoding.Unicode.GetString(buffer); binaryReader.Close(); stream.Close(); if (xmlContent.IndexOf("<SnControl.Solution>") == -1) { xmlContent = File.ReadAllText(fileName); } string xmlSolutionContent = xmlContent.Substring(xmlContent.IndexOf("<SnControl.Solution>"), xmlContent.IndexOf("</SnControl.Solution>") - xmlContent.IndexOf("<SnControl.Solution>")); xmlSolutionContent += "</SnControl.Solution>"; string xmlFormContent = xmlContent.Substring(xmlContent.IndexOf("<System.Windows.Forms.Form"), xmlContent.IndexOf("</QuerySolution>") - xmlContent.IndexOf("<System.Windows.Forms.Form")); XmlSolutionReader xmlSolutionReader = new XmlSolutionReader(_solution); _solution = xmlSolutionReader.SetUpSolution(xmlSolutionContent); if (_solution == null) { Controls.Clear(); return; } SolutionInstance.GetInstance().Solution = _solution; IDesignerLoaderProvider loaderProvider = new XmlDesignerLoaderProvider(xmlFormContent); IDesignerGenerator generator = new XmlDesignerGenerator(); _designerViewContent = new FormsDesignerViewContent(this, loaderProvider, generator); //加载XML内容 _designerViewContent.Reload(); _designPanel = new DesignPanel(_designerViewContent.DesignSurface); _designPanel.Dock = DockStyle.Fill; Controls.Add(_designPanel); if (!_designPanel.SetRootDesigner()) { Controls.Clear(); return; } _designerViewContent.PropertyContainer.Host = _designPanel.Host; _designerViewContent.PropertyContainer.SelectableObjects = _designerViewContent.Host.Container.Components; } catch (ApplicationException e) { MessageBox.Show(e.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void Initialize() { CreateDesignerHost(); Form form = _designerViewContent.Host.CreateComponent(typeof(Form)) as Form; form.Width = 700; form.Height = 500; form.TopLevel = false; form.Text = _solution.SolutionName; Panel panel = _designerViewContent.Host.CreateComponent(typeof(Panel)) as Panel; TabControl tabControl = _designerViewContent.Host.CreateComponent(typeof(TabControl)) as TabControl; TabPage tabPage = _designerViewContent.Host.CreateComponent(typeof(TabPage)) as TabPage; DataGridView dataGridView = _designerViewContent.Host.CreateComponent(typeof(DataGridView)) as DataGridView; form.SuspendLayout(); panel.Dock = DockStyle.Top; panel.Height = 200; panel.TabIndex = 0; tabControl.Dock = DockStyle.Fill; tabControl.Name = "tabMain"; tabControl.TabIndex = 1; tabControl.Controls.Add(tabPage); tabPage.Name = "pageMain"; tabPage.Controls.Add(dataGridView); dataGridView.Dock = DockStyle.Fill; dataGridView.Name = "grdEntity"; form.Controls.Add(tabControl); form.Controls.Add(panel); form.ResumeLayout(false); _designPanel.SetRootDesigner(); _designerViewContent.PropertyContainer.Host = _designPanel.Host; _designerViewContent.PropertyContainer.SelectableObjects = _designerViewContent.Host.Container.Components; }