简体   繁体   English

您如何在Visual Studio中查看c#控件的自动生成的设计器代码?

[英]How do you view the auto generated designer code for c# controls in Visual Studio?

Right now for my User Controls I right click the control in the Solution Explorer and then choose 'View Code'. 现在,对于我的用户控件,我在解决方案资源管理器中右键单击该控件,然后选择“查看代码”。 Then, in the top right corner where all the class's elements are enumerated in a dropdown box, I choose the grey'd out constructor and this brings me to the auto generated .designer.cs file that I'm looking for. 然后,在下拉框中枚举所有类元素的右上角,选择灰色的构造函数,这将我带到要查找的自动生成的.designer.cs文件中。

I feel like this is a really round-about way of doing it and it doesn't sit well with me. 我觉得这是一种真正的绕行方式,而且感觉不太好。 Am I supposed to be doing a better job of avoiding editing these files? 我应该在避免编辑这些文件方面做得更好吗? Are they hard to get to on purpose or did I just clearly miss something simple in Visual Studio? 是他们很难达到目的,还是我只是清楚地错过了Visual Studio中的简单内容?

You should quite simply be able to use the treeview of the Solution Explorer to expand the user control items and see the code-behind and designer files. 您应该非常简单地能够使用解决方案资源管理器的树视图来展开用户控件项,并查看代码隐藏文件和设计器文件。

This is curious, so I wonder what kind of user control (any particular project type)? 这是奇怪的,所以我不知道什么样的用户控件(任何特定项目类型)的?

As for avoidance of editing these auto-generated files: yes, you should be weary in doing so, and avoid it wherever possible. 至于避免编辑这些自动生成的文件:是的,您应该这样做会感到疲倦,并尽可能避免这样做。 Your changes are going to disappear if the code is ever regenerated (not that likely for user controls, I suppose), and the developers of the tool that generated it can't vouch for it working as it should if edited. 如果重新生成了代码,您的更改将消失(我想不太可能出现在用户控件中),并且生成该代码的工具的开发人员无法保证它可以正常工作(如果进行了编辑)。

There are times when you do want to edit these kinds of files, however. 但是,有时候您确实想编辑这类文件。 So use your own judgement to evaluate the value of doing so. 因此,请使用您自己的判断来评估这样做的价值。 I find myself dipping into the DBML designer files often enough to delete the default constructor which conflicts with my own in the partial definition, haven't found another way to do what I want. 我发现自己经常浸入DBML设计器文件中,以删除与局部定义中与我自己的构造器冲突的默认构造器,但还没有找到另一种方法来完成我想要的事情。 Such is the situation. 这样的情况。

Editing autogenerated code is never a good idea. 编辑自动生成的代码绝不是一个好主意。 The reason being that the code can be generated at any time and your changes will be lost. 原因是可以随时生成代码,并且所做的更改将丢失。 If you really, REALLY need to edit the code, you should be doing so using partial classes. 如果确实需要编辑代码,则应该使用部分类进行编辑。 But 95% of the time you shouldn't need to edit autogenerated code in the first place. 但是95%的时间您不需要首先编辑自动生成的代码。 What exactly is it that you're trying to accomplish? 您要完成的任务到底是什么?

An easy way to do it: Let's say you have a Label control called Label1 . 一种简单的方法:假设您有一个名为Label1Label控件。

If it is never actually used in your code, place some code that uses it somewhere in one of your methods: 如果您的代码中从未真正使用过它,请将一些使用它的代码放在您的一种方法中:

private void Test() {
  Label1.Text = null;
}

Now, put your Cursor over the Label1 control and press F12 . 现在,将光标置于Label1控件上方,然后按F12 This will take you to the definition of Label1 这将带您进入Label1的定义

Hope that helps! 希望有帮助!

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

相关问题 如何在 Visual Studio Designer 中编辑第二个(非主)C# 表单 Window? - How Do You Edit a Second (Non-Main) C# Form Window In Visual Studio Designer? 如何访问由Designer(XAML)创建的xamarin(C#,Visual Studio 2015)中的控件 - How to access controls in xamarin (C#, Visual Studio 2015) created by Designer (XAML) 如何防止Visual Studio设计器在DataGridView中自动生成列? - How do you prevent the Visual Studio designer auto-generating columns in a DataGridView? Visual Studio 2017设计器视图未显示控件 - Visual Studio 2017 Designer View not Showing Controls 如何在Visual C#中打开Designer视图? - How to open Designer View in Visual C#? 如何识别由Visual Studio的GUI设计器生成的代码? - How to recognize code generated by Visual Studio's GUI designer? C#-从异常类自动生成的构造函数在Visual Studio 2015中做什么 - C# - What do the auto generated constructors from the exception class do in visual studio 2015 WPF中自动生成的代码显示错误。 (C#,WPF,Visual Studio社区2013) - Auto-generated code in WPF shows error. (C#, WPF, Visual Studio Community 2013) 如何在Visual Studio 2017的C#中启用代码建议功能? - How do you enable Code suggestion feature in C# in Visual Studio 2017? 如何在Visual Studio中使用c#创建SQLite数据库? - How do you create an SQLite Database with c# in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM