[英]How to create a VS 2008 Windows Forms project programmatically, that can be maintained just like any other project created manually
I have a bunch of control properties (type of control, location, size, etc.) derived as a text file from an old system on a different platform. 我有很多控件属性(控件类型,位置,大小等),它们是从不同平台上的旧系统中作为文本文件导出的。 I thought it would be fairly easy to load these "control descriptions" into a Windows Forms project in VS 2008 and then be able to maintain the new project in VS just as if it had been created manually. 我认为将这些“控件描述”加载到VS 2008中的Windows Forms项目中,然后能够像在手动创建的情况下一样,在VS中维护新项目将相当容易。
I'm familiar with using VS and C# but it I'm not sure where to start with this. 我对使用VS和C#很熟悉,但是我不确定从哪里开始。
I searched the web and found a number of articles about creating controls on forms at run time and that is not so hard, given the information I have, but I want the created form to be a permanent part of a Win Forms project, so I need to get what was created at run time into VS, and accessible in the project just as if it had been manually dropped on the designer surface. 我在网上搜索,发现了许多有关在运行时在窗体上创建控件的文章,鉴于我所掌握的信息,这并不难,但是我希望创建的窗体成为Win Forms项目的永久组成部分,所以我需要将在运行时创建的内容放入VS,并且可以在项目中对其进行访问,就像已将其手动放置在设计器表面上一样。 Is there a way I could create these forms with a program and then "save" them in a form acceptable to VS? 有没有办法我可以使用程序创建这些表单,然后以VS可接受的形式“保存”它们?
I manually created a windows Form project to try and use as a "template" and identify how VS does it. 我手动创建了一个Windows Form项目,以尝试用作“模板”并确定VS的工作方式。 I have tried to understand the contents and the relationships between the various resource files, but it is a bit too much. 我试图了解内容以及各种资源文件之间的关系,但这有点过多。 There are also warnings that various files are created by VS and should not be amended manually. 也有警告说,VS创建了各种文件,不应手动对其进行修改。
I have almost 1000 forms from the old system so the thought of manually having to add each one to VS is pretty non-viable. 我有来自旧系统的近1000种表单,因此手动将每个表单添加到VS的想法几乎是不可行的。 I could translate the text descriptions into XML if there is a way to feed an XML description of a Form into a VS Forms project...? 如果有一种方法可以将表单的XML描述输入到VS Forms项目中,我可以将文本描述转换为XML。
Any advice\\direction towards this would be greatly appreciated. 任何对此的建议\\方向将不胜感激。
VS uses code generation to create controls in Windows Forms. VS使用代码生成在Windows窗体中创建控件。 If you look at a form eg Form1
that's in your project, it consists of three files, Form1.cs
, Form1.designer.cs
and Form1.resx
. 如果您查看项目中的表单(例如Form1
,则它由三个文件Form1.cs
, Form1.designer.cs
和Form1.resx
。 The Form1.designer.cs
file contains the construction of the controls in the InitializeComponent
method. Form1.designer.cs
文件包含InitializeComponent
方法中控件的构造。 VS executes this and parses it at design time to give you the design view of the form where you can make modifications. VS执行此操作并在设计时对其进行解析,以提供表单的设计视图,您可以在其中进行修改。 You could write a program to parse your text files and generate C# code for InitializeComponent
to get the right effect. 您可以编写一个程序来解析您的文本文件,并为InitializeComponent
生成C#代码以获得正确的效果。 There are various options but you probably would want to use the CSharpCodeProvider
class. 有多种选择,但您可能希望使用CSharpCodeProvider
类。
Another option is to use WPF. 另一种选择是使用WPF。 Forms are designed in WPF using XAML, which is an XML based format. 表单是使用XAML(基于XML的格式)在WPF中设计的。 (By that I mean, an XAML document is also a valid XML document, but the reverse is not true.) If you create a WPF project and add some controls to it then you could take a look at the XAML file to get an idea of the structure. (我的意思是,XAML文档也是有效的XML文档,但事实并非如此。)如果创建WPF项目并向其中添加一些控件,则可以查看XAML文件来了解一下结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.