简体   繁体   English

在C#Windows窗体应用程序项目中更改自动生成的代码

[英]Changing autogenerated code in a C# Windows Forms Application project

When you open a new C# Windows Forms Application project in Visual Studio 2008, you get a lot of autogenerated code (AssemblyInfo.cs, Resources.Designer.cs, Settings.Designer.cs, Form1.Designer.cs, Form1.resx, Program.cs). 当您在Visual Studio 2008中打开一个新的C#Windows窗体应用程序项目时,您将获得大量自动生成的代码(AssemblyInfo.cs,Resources.Designer.cs,Settings.Designer.cs,Form1.Designer.cs,Form1.resx,Program的.cs)。

Beside adding components from the Toolbox to Form1.cs[Design] and code to Form1.cs, what files can you change? 除了将工具箱中的组件添加到Form1.cs [Design]和代码到Form1.cs之外,您可以更改哪些文件? And how? 如何? And what files should be left as they are? 应该保留哪些文件?

PS: This is a vast subject, as far as I can tell. PS:据我所知,这是一个广泛的主题。 I don't expect an exhaustive answer. 我不希望得到详尽的答案。 Any information you care to share will do. 您需要分享的任何信息都可以。 I also want to mention that I'm a C# beginner and only want the most basic information. 我还想提一下,我是一个C#初学者,只想要最基本的信息。

PPS: In case you're thinking about answering "Search the internet" or something like that, don't. PPS:如果您正在考虑回答“搜索互联网”或类似问题,请不要。 I have searched the internet and will do so again and again until I find the answers I'm looking for. 我已经搜索过互联网,并会一次又一次地搜索,直到找到我正在寻找的答案。 I have found some useful information, but I feel I need more. 我找到了一些有用的信息,但我觉得我需要更多。 I think it's likely I will get that at stackoverflow.com. 我想我很可能会在stackoverflow.com上得到它。


Update 1: This question has been answered. 更新1:此问题已得到解答。 I've marked the answer from sh_kamalh as accepted. 我已经接受了sh_kamalh的答案。 I would like to mark the answers from David and Hans Passant as accepted as well, but I don't think you can mark more than one answer as the accepted one. 我想将David和Hans Passant的答案标记为已被接受,但我认为您不能将多个答案标记为已接受的答案。 Or can you? 或者你呢?

From what I can dedcue from the answers, you should never change the autogenerated code in a C# Windows Forms Application project in Visual Studio 2008. I don't understand everything in the answers, though, and may have overlooked something. 从我可以从答案中解脱出来,你永远不应该在Visual Studio 2008中的C#Windows窗体应用程序项目中更改自动生成的代码。但是,我不了解答案中的所有内容,并且可能忽略了某些内容。

If you don't agree with the summary right above or the answers below, feel free to add a contradictory answer. 如果您不同意上述摘要或下面的答案,请随意添加一个矛盾的答案。 Or if you have an answer that supplement the answers below, feel free to add that answer. 或者,如果您有一个补充以下答案的答案,请随意添加该答案。 I'm sure there's more to be said on this subject. 我相信在这个问题上还有更多要说的。


Update 2: It seems you can also write your own version of a C# Windows Forms Application project (see Davids answer below, which I forgot about when I wrote "Update 1"). 更新2:您似乎也可以编写自己的C#Windows窗体应用程序项目版本(请参阅下面的Davids答案,我在编写“Update 1”时忘记了这一点)。 It also seems you should start from scratch if you do. 如果你这样做,你似乎应该从头开始。

The auto-generated files: 自动生成的文件:

  • AssemblyInfo.cs : the attributes inside it are set by Project + Properties, Application Tab, Assembly Information button. AssemblyInfo.cs:其中的属性由Project + Properties,Application Tab,Assembly Information按钮设置。 Editing the file directly is okay, the IDE doesn't easily get confused by it. 直接编辑文件是可以的,IDE不容易被它搞糊涂。

  • Settings.Designer.cs : auto-generated from the Settings.settings file which in turn is generated from the Settings designer. Settings.Designer.cs:从Settings.settings文件自动生成,然后由Settings设计器生成。 Any edits to this file will be lost quickly. 对此文件的任何编辑都将很快丢失。

  • Resources.Designer.cs : auto-generated from the Resources.resx file which in turn is generated from the Resources designer. Resources.Designer.cs:从Resources.resx文件自动生成,而该资源又是由Resources设计器生成的。 Any edits to this file will be lost quickly. 对此文件的任何编辑都将很快丢失。

  • Form .Designer.cs : auto-generated by the form designer. Form .Designer.cs:由表单设计器自动生成。 Editing this file is possible, there is no independent file that stores the form layout. 可以编辑此文件,没有存储表单布局的独立文件。 The design view is generated by running this code at design time and using Reflection to recover the form and control properties. 通过在设计时运行此代码并使用Reflection恢复表单和控件属性来生成设计视图。 And saved again when you close the designer, it re-generates the InitializeComponent method and the control variables. 当您关闭设计器时再次保存,它会重新生成InitializeComponent方法和控制变量。 Getting your code changes to survive this process requires writing the code exactly the way the designer itself would. 让代码更改以在此过程中生存需要完全按照设计者本身的方式编写代码。 Which is fairly untrivial when you make changes beyond simple property value changes. 当您进行超出简单属性值更改的更改时,这是相当不重要的。 There's also considerable risk, an unwise change can easily cause an exception. 也存在相当大的风险,不明智的变化很容易导致异常。 When that happens at design time, you get the infamous WSOD (White Screen Of Darn) which displays an often very cryptic exception and tells you that you cannot design your form anymore. 当这种情况发生在设计时,你会得到臭名昭着的WSOD(白色画面),它会显示一个经常非常神秘的例外,并告诉你不能再设计你的表格了。

You can learn more about the way the designer generates code by observing the changes it makes to InitializeComponent when you modify the form in the designer. 通过观察设计器中修改表单时对InitializeComponent所做的更改,您可以了解设计器生成代码的方式。 Getting this right is certainly not beginner material, you are probably have more pressing things to learn while you get up to speed on .NET programming. 正确的做法当然不是初学者的材料,当你加快.NET编程速度时,你可能需要学习更多紧迫的东西。 The designers are there to make your life easier and to minimize the risk of getting it wrong. 设计师可以让您的生活更轻松,并最大限度地降低错误风险。

Most of those files are templated and you can modify their inclusion in a new project and what is contained in the files. 这些文件中的大多数都是模板化的,您可以修改它们在新项目中的包含以及文件中包含的内容。 You can even create your own templates for new project types or items. 您甚至可以为新项目类型或项目创建自己的模板。 http://msdn.microsoft.com/en-us/magazine/cc188697.aspx http://msdn.microsoft.com/en-us/magazine/cc188697.aspx

As far as modifying what is already there, unless you're sure that every project you're going to work on is going to need those changes I wouldn't recommend doing that, it's easier to just create a new template for a different project/file type. 至于修改已经存在的内容,除非你确定你要处理的每个项目都需要这些更改我不建议这样做,否则为不同的项目创建一个新模板会更容易/文件类型。

Why do you want to change the auto-generated files? 为什么要更改自动生成的文件?
In case of forms you should not change .Designer.cs or the .resx files because they will be generated everytime you change the form so whatever changes you do will be overwritten next time the code is regenerated. 如果是表单,则不应更改.Designer.cs或.resx文件,因为每次更改表单时都会生成它们,因此下次重新生成代码时,您所做的任何更改都将被覆盖。
If you want to change something in Designer.cs, you can do that change in the original cs file because the class is marked as partial which means that the code from the files Form1.cs and Form1.Designer.cs is combined to generate the final code. 如果要在Designer.cs中更改某些内容,可以在原始cs文件中进行更改,因为该类被标记为partial,这意味着将文件Form1.cs和Form1.Designer.cs中的代码组合在一起生成最终代码。
If you want to add your resources add a resource file and leave the generated .resx file intact. 如果要添加资源,请添加资源文件并保留生成的.resx文件。 For AssemblyInfo.cs you can change the general information of the assembly. 对于AssemblyInfo.cs,您可以更改程序集的常规信息。

Long story short just leave the generated files intact. 简而言之,只需保留生成的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM