visual-studio 在 Visual Studio 中禁用设计器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/567606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:10:47  来源:igfitidea点击:

Disable designer in Visual Studio?

visual-studiowindows-forms-designer

提问by I. J. Kennedy

I'm using Visual C# Express to write Windows Form applications, and call me old school, but I don't like the designer. It is a nuisance and pollutes my project with lots of unwanted files, as well as unwanted monkey-generated source code.

我正在使用 Visual C# Express 编写 Windows 窗体应用程序,并称我为老派,但我不喜欢设计器。这是一件令人讨厌的事情,并且会用大量不需要的文件以及不需要的猴子生成的源代码污染我的项目。

Please tell me there's a way to turn it off completely.

请告诉我有一种方法可以完全关闭它。

采纳答案by Avram

Just dont add "new form", add to the project new class and inherit him from the Form class.

只是不要添加“新表单”,添加到项目新类并从表单类继承他。

回答by

Add a [System.ComponentModel.DesignerCategory("")]attribute before a UI class to avoid designer activation on double-click.

[System.ComponentModel.DesignerCategory("")]在 UI 类之前添加属性以避免在双击时激活设计器。

Note that including System.ComponentModelat the top of the file and then just adding the attribute as [DesignerCategory("")]will not work (at least not in Visual Studio 2010/2013). You must use the full, namespace-qualified attribute type name.

请注意,包括System.ComponentModel在文件顶部,然后仅添加属性 as[DesignerCategory("")]将不起作用(至少在 Visual Studio 2010/2013 中不会)。您必须使用完整的、命名空间限定的属性类型名称。

回答by Richard

Right clicking on a project file and selecting open with allows you to set the defaults for opening file types.

右键单击项目文件并选择打开方式允许您设置打开文件类型的默认值。

回答by Keith Mattix

Actually, all you have to do in VS2010 is right click the .cs file that you don'twant to open in the designer, select the "Open With.." option, then make CSharp Editor the default. Notice that the form view is the default before you change it.

实际上,在 VS2010 中您要做的就是右键单击您不想在设计器中打开的 .cs 文件,选择“打开方式..”选项,然后将 CSharp Editor 设为默认值。请注意,表单视图是更改前的默认视图。

回答by fishy_pkat151

Frederik, sure you're right, but not in one fact. ( this one I.J wanted to know ) As soon as you inherit from any Component, the studio tries to open it in a designer editor. That is a default behaviour of the visual studio.

弗雷德里克,你肯定是对的,但事实并非如此。(这个 IJ 想知道)一旦您从任何组件继承,工作室就会尝试在设计器编辑器中打开它。这是 Visual Studio 的默认行为。

If you double click a file in the solution explorer, then a designer opens. So it's really annoying if you always get such strange designer for classes that are just inherited from a Component, but do not contain any visible things.

如果您双击解决方案资源管理器中的文件,则会打开一个设计器。因此,如果您总是为仅从 Component 继承但不包含任何可见内容的类获得如此奇怪的设计器,这真的很烦人。

The only thing that really helped me out, was to set this attribute:[System.ComponentModel.DesignerCategory("")]

唯一真正帮助我的是设置这个属性:[System.ComponentModel.DesignerCategory("")]

I didn't knew this =) so cool hint !

我不知道这个 =) 很酷的提示!

I just get a problem with partial MainForm classes. Then it yells "duplicate attribute"...

我只是遇到部分 MainForm 类的问题。然后它大喊“重复属性”......

Does here anybody knows a solution to avoid in some partial class-files the designer to be opened on double click ( without the duplicate attribute error on compile ? )

这里有人知道避免在某些部分类文件中双击打开设计器的解决方案吗(编译时没有重复的属性错误?)



I think I found a small bug, in VS2005... By setting the DesignerCategory-Attribute on the partial form class, on save the solution explorer shows up an icon, identifying the file as a c# file (no form).

我想我在 VS2005 中发现了一个小错误...通过在部分表单类上设置 DesignerCategory-Attribute,在保存解决方案资源管理器时会显示一个图标,将文件标识为 ac# 文件(无表单)。

Then I put a comment (//) before the attribute - and it keeps opening the file in code view. Also after closing and reopening it's stored internally as a non designable form-part. Even on re opening the complete solution.

然后我在属性前添加一个注释 (//) - 它会一直在代码视图中打开文件。同样在关闭和重新打开后,它在内部存储为不可设计的表单部件。即使重新打开完整的解决方案。

So I think there is any information in the solution or project file...

所以我认为解决方案或项目文件中有任何信息......

... I found this in the .csproj file: ...

...我在 .csproj 文件中找到了这个: ...

<Compile Include="GUI\VFormMain_Test.cs">
</Compile>
<Compile Include="GUI\VFormMain_Theme.cs">
</Compile>
<Compile Include="Core\VTTEnv.cs" />
<Compile Include="GUI\VFormMain.cs">
  <SubType>form</SubType>
</Compile>

...

...

Studio updates from time to time - then it updated the form files again (doh)... ok.. but there is probably a workaround to avoid this.

Studio 不时更新 - 然后它再次更新表单文件(doh)......好吧......但可能有一种解决方法可以避免这种情况。



snip... last edit... found something:

剪断...上次编辑...找到了一些东西:

http://social.msdn.microsoft.com/Forums/en-US/csharpide/thread/64c77755-b0c1-4447-8ac9-b5a63a681b78

http://social.msdn.microsoft.com/Forums/en-US/csharpide/thread/64c77755-b0c1-4447-8ac9-b5a63a681b78

( name the file you want to open without designer into .Designer.cs )

(将要在没有设计器的情况下打开的文件命名为 .Designer.cs )

( Yes you have to name something to switch it of LOL ) Seems to work.

(是的,您必须命名一些东西才能将其切换为 LOL)似乎有效。

回答by Frederik Gheysels

If you don't want to use the designer, then don't use it ?

如果你不想使用设计器,那么就不要使用它?

You can create a new form from scratch, without the designer generating any code by creating a new empty class, and let the class inherit from System.Windows.Forms.Form

您可以从头开始创建一个新表单,而无需设计者通过创建一个新的空类来生成任何代码,并让该类继承自 System.Windows.Forms.Form

Then, VS.NET will probably still indicate that your class is a Form, and you can still open it up in the designer if you want. (And drop controls on it).

然后,VS.NET 可能仍会指示您的类是一个表单,如果您愿意,您仍然可以在设计器中打开它。(并在其上放置控件)。