Is it possible to tell Visual Studio not to treat a source file as a “component”? [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-04 16:25:44

问题


Possible Duplicate:
Is there an attribute I can add to a class so it will be edited as code, not in the designer?

Class with System.ComponentModel.Component on their inheritance path are automatically treated as "components" within Visual Studio (2008), triggering a different icon for the source file:

While the icon does not really matter, the changed double click behavior is really annoying: instead of opening the source code in the text editor, Visual Studio now shows a screen encouraging me to add components to my class by dragging them "from the Toolbox". I do not want to do that!

I am aware that I can right click the source file and choose "View Code", but whenever I forget to do this, I am stuck waiting for a dialog which is absolutely useless. Is there any way to disable the component behavior (preferably in the source code)?


回答1:


Can't you use the DesignerCategory attribute to decorate your class ?

When decorating your class with this attribute like displayed below, the file should open in 'code view' when you double click it:

[System.ComponentModel.DesignerCategory("Code")]
public class MyComponent
{
}



回答2:


As OregonGhost already mentioned in his comment this also happens if you make a partial class file from a form.

The problem is even more worse. If you take a look into your project file you can find entries for every file include like this:

<Compile Include="FormMain.cs">
  <SubType>Form</SubType>
</Compile>

So the solution should be to delete the line <SubType>...</SubType> cause it seems to be the root of all the problems. But if you delete this line, save the file, open it in Visual Studio, save it again and take a look again into, the line will reappear!

There seems to be only one hard-coded exception within Visual Studio and this is *.Designer.cs. So there is no solution to accomplish this problem.




回答3:


You could right-click the source file, choose "View Code", then click "Set as Default". It's a bit of a blunt instrument, but if you have more false components in your solution than real components, then you may find it convenient.



来源:https://stackoverflow.com/questions/2138684/is-it-possible-to-tell-visual-studio-not-to-treat-a-source-file-as-a-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!