因此,这可能符合您的需求,也可能不符合您的需求。我有一个解决方案,但它需要您调整 XML -> WinForms 渲染。
这在很大程度上依赖于这个项目的使用:http: //ivanz.com/files/docs/designerhosting/create-and-host-custom-designers-dot-net.html
下载 EXE(它只是带有 EULA 的源代码的压缩版本)并构建解决方案,我在构建时遇到了一些问题,直到我删除了对图形库的引用(以及从主机库中对它的相关调用)。为什么主机库需要绘制图表我不太确定......
然后我建立了一个新的 WinForms 项目(不能是控制台,因为 DesignSurface 无法挂钩拖放事件)。在新的 winforms 项目中引用上述项目中的 Host 和 Loader 项目。
毫无疑问,您需要在此处调整现有流程。合并您现有的流程,使其适合下面我的标签所示的这种类型的表单构建:
HostSurfaceManager hsm = new HostSurfaceManager();
HostControl hc = hsm.GetNewHost(typeof(Form), LoaderType.CodeDomDesignerLoader);
var l = new Label() { Text = "TEST!!!!" };
hc.DesignerHost.Container.Add(l);
richTextBox1.Text = ((CodeDomHostLoader)hc.HostSurface.Loader).GetCode("C#");
这会生成您的 form.cs 文件内容(请参阅下面的生成代码)。这是一个多合一文件,您无需创建单独的 form.cs.designer 文件即可获得设计器支持。我复制了上面生成的代码,将其保存到 .cs 文件中,Visual Studio 将其识别为 winform 并为我提供了设计支持。
您的副本可能会包含一个子 Main。我进入 Loader -> CodeGen.cs 文件并注释掉与 Main 相关的部分,我建议你也这样做。
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.5448
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace DesignerHostSample
{
using System;
using System.ComponentModel;
using System.Windows.Forms;
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
public Form1()
{
this.InitializeComponent();
}
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 0;
this.label1.Text = "TEST!!!!";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "Form1";
this.ResumeLayout(false);
}
}
}
由 FastAl 编辑
彼得,你摇滚!!!这几乎正是我想要的。好吧,我还不能直接把物体扔给它;仅使用上述方法,没有填充 .Controls 属性,所以我得到了一个空白表格。此外,表单似乎必须由 GetNewHost 创建。幸运的是,我的 XMl-to-screen 例程实际上并没有创建容器,它只是返回一个平面的控件列表,我必须正确地重新设置它们的父级(SetChildren 子)。请注意我必须将它们添加到主机容器以使其了解它们的注释。现在它完美运行!
Public Module Main
Public Sub Main()
Dim FormSpecAsText As String = ... read XML form def from file
Dim Outfile As String = ... output file is in my project
' Setup for Winforms platform
Dim dsg As New DynamicScreenGenerator
Dim ListOfControls As New PanelObjectList
ControlFactoryLocator.AddService( _
New PanelObjectFactoryWinFormBasicControls)
ControlFactoryLocator.AddService(_
New PanelObjectFactoryWinFormAppSpecificCtls)
ControlFactoryLocator.AddService(_
New PanelObjectFactoryWinFormFormEditorCtls)
' Deserialize FormSpecAsText into a flat list of Controls
ListOfControls.AddRange( _
dsg.BuildDSGLists(FormSpecAsText, ListOfControls).ToArray)
' setup for serialization to Code
Dim hsm As New Host.HostSurfaceManager
Dim hc As Host.HostControl = _
hsm.GetNewHost(GetType(Form), Host.LoaderType.CodeDomDesignerLoader)
' Get main form that was created via GetNewHost, autosize it
Dim HostUserControl = _
CType(hc.DesignerHost.Container.Components(0), Form)
' Parent them properly, and add to host (top lvl ctls have parent="")
SetChildren(HostUserControl, "", dsg, hc.DesignerHost.Container)
HostUserControl.AutoSize = True
' write serialized version to a file in my project
IO.File.WriteAllText(Outfile, _
CType(hc.HostSurface.Loader, Loader.CodeDomHostLoader).GetCode("VB"))
End Sub
Sub SetChildren(ByVal Parent As Control, ByVal ParentName As String, _
ByVal dsg As DynamicScreenGenerator, ByVal ctr As IContainer)
For Each PO In (From p In dsg.POList Where p.Parent = ParentName)
Dim child = CType(dsg.CTLList(PO), Control)
ctr.Add(child, PO.Name) ' seem to have to add to container while
' parenting them or .Controls isn't serialized and form is blank.
Parent.Controls.Add(child)
SetChildren(child, PO.Name, dsg, ctr)
Next
End Sub
End Module