C# GUI 命名约定的最佳实践?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1246546/
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-08-06 14:11:04  来源:igfitidea点击:

Best practices for C# GUI naming conventions?

c#winformsuser-interfacenaming-conventions

提问by Will Eddins

GUIs, whether written in WinForms or XAML, seem to have the most widely differing naming conventions between projects I see. For a simple TextBoxfor a person's name, I've seen various naming conventions:

无论是用 WinForms 还是 XAML 编写的 GUI,我看到的项目之间的命名约定似乎差异最大。对于一个简单TextBox的人名,我已经看到了各种命名约定:

TextBox tbName      // Hungarian notation
TextBox txtName     // Alternative Hungarian
TextBox NameTextBox // Not even camelCase
TextBox nameTextBox // Field after field with TextBox on the end
TextBox TextBoxName // Suggested in an answer...
TextBox textBoxName // Suggested in an answer...
TextBox uxName      // Suggested in an answer...
TextBox name        // Deceptive since you need name.Text to get the real value
TextBox textBox1    // Default name, as bad as you can get

I abide by the StyleCop rules for all my .cs files normally, and see others do so as well, but the GUI tends to break these rules or vary wildly. I haven't seen any Microsoft guidelines that specifically refer to GUI elements instead of just normal variables, or even examples that would apply outside of a console application.

我通常遵守所有 .cs 文件的 StyleCop 规则,并看到其他人也这样做,但 GUI 往往会违反这些规则或变化很大。我还没有看到任何专门引用 GUI 元素而不仅仅是普通变量的 Microsoft 指南,甚至还没有看到适用于控制台应用程序之外的示例。

What are the best practices for naming elements in a GUI?

在 GUI 中命名元素的最佳实践是什么?

采纳答案by Neil N

I use the old school hungarian... txt for TextBox, btn for Button, followed by a generalized word, then a more specific word. i.e.:

我使用老式的匈牙利语... txt 表示文本框,btn 表示按钮,然后是一个广义词,然后是一个更具体的词。IE:

btnUserEmail

Have had a lot of people say things like "omg thats so old, VB6 calling!"But in a UI Rich winforms app, I can find and modify things quicker because usually the first thing you know about a control is it's type, then it's category, then get specific. While the newer style naming convention guys are stuck trying to remember what they named that text box.

有很多人说“天哪,太老了,VB6 调用!”之类的话。但是在 UI Rich winforms 应用程序中,我可以更快地找到和修改内容,因为通常您首先了解控件的是它的类型,然后是类别,然后是具体的。虽然较新的样式命名约定的人被困在试图记住他们命名该文本框的内容。

The original specification for controls is here (archived).

控件的原始规范在这里(存档)。

回答by Andrew Hare

The most important thing about naming conventions is to choose something that makes sense, get a consensus from all parties, and stick to it like your life depended on it.

命名约定最重要的是选择有意义的东西,获得各方的共识,并像依赖它一样坚持下去。

As for which convention to use I would vote for this one:

至于使用哪种约定,我会投票给这个约定:

TextBox name

It is short and has semantic value as an identifier. As for the typeof the identifier I would rely on Visual Studio to tell you that as it tends to be good at that sort of thing.

它很短并且具有作为标识符的语义价值。至于标识符的类型,我将依靠 Visual Studio 来告诉您,因为它往往擅长这类事情。

回答by Matt Grande

I name all my UI elements TypeDescriptor. Following your example, TextBoxName.

我将所有 UI 元素命名为 TypeDescriptor。按照你的例子,TextBoxName.

回答by Cleiton

I use Hungarian notation, that makes easy to find controlls in large pages.

我使用匈牙利符号,这使得在大页面中很容易找到控件。

回答by Brad Bruce

I've been working with a team lately that is moving from MFC (6.0 ...). There they would have something like

我最近一直在与一个从 MFC (6.0 ...) 迁移的团队一起工作。在那里他们会有类似的东西

CString Name;
CEdit ctlName;

The easiest way to migrate has been to use something like

迁移的最简单方法是使用类似

TextBox ctlName

It's just enough of a reminder that the variable is the control and not the value of the control.

这足以提醒变量是控件而不是控件的值。

I think including the type as a part of the name is just OLD.

我认为包括类型作为名称的一部分只是旧的。

-- edit -- Another benefit is that all of the controls are grouped together when navigating. If the actual type were used, the ComboBox controls would be quite far from the TextBox controls.

-- 编辑 -- 另一个好处是导航时所有控件都组合在一起。如果使用实际类型,ComboBox 控件将与 TextBox 控件相去甚远。

回答by Austin Salonen

For the elements that I don't plan on using in my code, I just let the designer handle it for me; if they do become something used in my code, they're changed to something meaningful and that happens to be descriptionType(nameTextBox). It's how the designer creates them if given enough information (check out menu items -- "Exit" becomes exitMenuItem).

对于我不打算在代码中使用的元素,我只是让设计师为我处理;如果它们确实成为我的代码中使用的东西,它们会更改为有意义的东西,并且恰好是descriptionType(nameTextBox)。如果提供足够的信息,这就是设计者创建它们的方式(检查菜单项——“退出”变成了 exitMenuItem)。

回答by Lee

I use:

我用:

TextBox nameTextBox;

Just like I would use:

就像我会使用:

MailAddress homeAddress;

The reason for this is that in these cases "TextBox" and "Address" is descriptive of what the object represents, not how it is stored or used. But in another case like storing a person's full name I would use:

这样做的原因是,在这些情况下,“TextBox”和“Address”描述的是对象代表什么,而不是它的存储或使用方式。但在另一种情况下,比如存储一个人的全名,我会使用:

string fullName;

Not:

不是:

string fullNameString;

Because "String" is not descriptive of what the object represents, but only how it is stored.

因为“String”不是描述对象代表什么,而是描述它是如何存储的。

回答by Christian Hayter

Same convention as everything else in .NET: camel case descriptive name only, optionally followed by a suffix if you need to distinguish different classes for the same logical "thing". For example:

与 .NET 中的其他所有内容相同的约定:仅使用驼峰式大小写的描述性名称,如果需要区分同一逻辑“事物”的不同类,可选择后跟后缀。例如:

string name; // a name
TextBox nameText; // the control used to edit the name
Label nameLabel; // the control used to label the edit control
List<string> nameList; // a list of names

and so on ad infinitum. It really doesn't matter what the suffixes are as long as they are consistent and descriptive.

等等,无止境。只要后缀是一致的和描述性的,它们实际上并不重要。

回答by terR0Q

My own practice is: Type _contextDescriptionType. E.g.:

我自己的做法是:输入_contextDescriptionType。例如:

TextBox _searchValueTextBox

Anyway naming convention is either too personal or imposed by general rules. In any case it should be documented somewhere so that all project developers can easyly access.

无论如何,命名约定要么过于个人化,要么是由一般规则强加的。在任何情况下,它都应该记录在某个地方,以便所有项目开发人员都可以轻松访问。

回答by Brandon

This is not my invention, but I like it:

这不是我的发明,但我喜欢它:

TextBox uxName = new TextBox();
Label uxNameLabel = new Label();
Button uxAccept = new Button();

I prefer this to Hungarian notation since all of my UI controls show up in one block in intelisense. UX for "User eXperience". It's also nice if you change a control from a textbox to a combobox or something, as the name won't change.

与匈牙利表示法相比,我更喜欢这种表示法,因为我的所有 UI 控件都显示在智能感知中的一个块中。“用户体验”的 UX。如果您将控件从文本框更改为组合框或其他内容也很好,因为名称不会更改。