C# GUI 命名约定的最佳实践? [英] Best practices for C# GUI naming conventions?

查看:38
本文介绍了C# GUI 命名约定的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GUI,无论是用 WinForms 还是 XAML 编写的,似乎在我看到的项目之间具有最广泛不同的命名约定.对于一个简单的人名TextBox,我见过各种命名约定:

TextBox tbName//匈牙利符号TextBox txtName//替代匈牙利语TextBox NameTextBox//甚至不是驼峰式大小写TextBox nameTextBox//后一个字段,最后是 TextBoxTextBox TextBoxName//在答案中建议...TextBox textBoxName//在答案中建议...TextBox uxName//在答案中建议...TextBox name//具有欺骗性,因为您需要 name.Text 来获取实际值TextBox textBox1//默认名称,尽你所能

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

在 GUI 中命名元素的最佳做法是什么?

解决方案

我使用旧式的匈牙利语... txt 代表 TextBox,btn 代表 Button,然后是一个概括的词,然后是一个更具体的词.即:

btnUserEmail

有很多人说天啊,太老了,VB6 调用!"但是在 UI 丰富的 winforms 应用程序中,我可以更快地找到和修改内容,因为通常您首先想到的是了解一个控件是它的类型,然后是它的类别,然后是具体的.虽然较新风格的命名约定人们一直在努力记住他们为该文本框命名的内容.

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

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

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.

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

解决方案

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

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.

The original specification for controls is here (archived).

这篇关于C# GUI 命名约定的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆