更改 C# Windows 窗体应用程序项目中自动生成的代码
当您在 Visual Studio 2008 中打开新的 C# Windows 窗体应用程序项目时,您会获得大量自动生成的代码(AssemblyInfo.cs、Resources.Designer.cs、Settings.Designer.cs、Form1.Designer.cs、Form1.resx、Program 。CS)。
除了将工具箱中的组件添加到 Form1.cs[Design] 并将代码添加到 Form1.cs 之外,您还可以更改哪些文件?又如何呢?哪些文件应该保留原样?
PS:据我所知,这是一个很大的主题。我不期望得到详尽的答案。您愿意分享的任何信息都可以。我还想提一下,我是 C# 初学者,只想要最基本的信息。
PPS:如果您正在考虑回答“搜索互联网”或类似的问题,请不要回答。我已经在互联网上进行了搜索,并将一次又一次地这样做,直到找到我正在寻找的答案。我找到了一些有用的信息,但我觉得我需要更多。我想我很可能会在 stackoverflow.com 上得到它。
更新1:这个问题已经得到解答。我已将 sh_kamalh 的答案标记为已接受。我也想将 David 和 Hans Passant 的答案标记为已接受,但我认为您不能将多个答案标记为已接受。或者你可以吗?
根据我从答案中可以推断出的内容,您永远不应该更改 Visual Studio 2008 中 C# Windows 窗体应用程序项目中自动生成的代码。不过,我并不理解答案中的所有内容,并且可能忽略了某些内容。
如果您不同意上面的摘要或下面的答案,请随意添加矛盾的答案。或者,如果您有补充以下答案的答案,请随时添加该答案。我确信关于这个话题还有更多话要说。
更新 2:看来您也可以编写自己版本的 C# Windows 窗体应用程序项目(请参阅下面 Davids 的回答,当我编写“更新 1”时我忘记了)。如果你这样做的话,似乎你也应该从头开始。
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).
Beside adding components from the Toolbox to Form1.cs[Design] and code to Form1.cs, what files can you change? And how? And what files should be left as they are?
PS: This is a vast subject, as far as I can tell. 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.
PPS: In case you're thinking about answering "Search the internet" or something like that, don't. 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.
Update 1: This question has been answered. I've marked the answer from sh_kamalh as accepted. 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. 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.
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"). It also seems you should start from scratch if you do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
自动生成的文件:
AssemblyInfo.cs :其中的属性由项目 + 属性、应用程序选项卡、程序集信息按钮设置。直接编辑文件就可以了,IDE 不容易被它迷惑。
Settings.Designer.cs:从 Settings.settings 文件自动生成,而该文件又是从设置设计器生成的。对此文件的任何编辑都将很快丢失。
Resources.Designer.cs:从 Resources.resx 文件自动生成,而该文件又是从资源设计器生成的。对此文件的任何编辑都将很快丢失。
Form.Designer.cs:由表单设计器自动生成。可以编辑此文件,没有存储表单布局的独立文件。设计视图是通过在设计时运行此代码并使用反射来恢复表单和控件属性来生成的。当您关闭设计器时再次保存,它会重新生成
InitializeComponent方法和控制变量。要让代码更改在这个过程中幸存下来,需要完全按照设计者本身的方式编写代码。当您进行的更改超出简单的属性值更改时,这是相当不重要的。也存在相当大的风险,不明智的改变很容易导致异常。当这种情况在设计时发生时,您会遇到臭名昭著的 WSOD(白屏),它显示一个通常非常神秘的异常,并告诉您无法再设计表单。
您可以通过观察在设计器中修改表单时对 InitializeComponent 所做的更改来了解有关设计器生成代码的方式的更多信息。正确地做到这一点当然不是初学者的材料,当您加快 .NET 编程速度时,您可能有更紧迫的事情需要学习。设计师的目的是让您的生活更轻松,并最大限度地减少出错的风险。
The auto-generated files:
AssemblyInfo.cs : the attributes inside it are set by Project + Properties, Application Tab, Assembly Information button. Editing the file directly is okay, the IDE doesn't easily get confused by it.
Settings.Designer.cs : auto-generated from the Settings.settings file which in turn is generated from the Settings designer. 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. Any edits to this file will be lost quickly.
Form.Designer.cs : auto-generated by the form designer. 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. And saved again when you close the designer, it re-generates the
InitializeComponent method and the control variables. 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.
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. 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. The designers are there to make your life easier and to minimize the risk of getting it wrong.
为什么要更改自动生成的文件?
对于表单,您不应更改 .Designer.cs 或 .resx 文件,因为每次更改表单时都会生成它们,因此您所做的任何更改都将在下次重新生成代码时被覆盖。
如果您想更改 Designer.cs 中的某些内容,您可以在原始 cs 文件中进行更改,因为该类被标记为部分,这意味着文件 Form1.cs 和 Form1.Designer.cs 中的代码将组合起来生成最终代码。
如果要添加资源,请添加资源文件并保持生成的 .resx 文件不变。
对于AssemblyInfo.cs,您可以更改程序集的一般信息。
长话短说,只需保持生成的文件完好无损即可。
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.
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.
If you want to add your resources add a resource file and leave the generated .resx file intact.
For AssemblyInfo.cs you can change the general information of the assembly.
Long story short just leave the generated files intact.
大多数这些文件都是模板化的,您可以修改它们在新项目中的包含内容以及文件中包含的内容。您甚至可以为新的项目类型或项目创建自己的模板。 http://msdn.microsoft.com/en-us/magazine/cc188697.aspx
至于修改已有的内容,除非您确定您要处理的每个项目都需要这些更改,否则我不建议这样做,创建一个新模板会更容易对于不同的项目/文件类型。
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
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.