简体   繁体   English

C# 中 windows 控件的命名约定

[英]Naming convention for windows control in C#

I want to know what the guidance on name the control in c#.我想知道 c# 中的控件名称指南。 I have seen the following link but it does not talks about controls: http://msdn.microsoft.com/en-us/library/ms229002.aspx我看过以下链接,但它没有谈论控件: http://msdn.microsoft.com/en-us/library/ms229002.aspx

Other links tell me to use hungarian notation but some links tell me it is bad practice to use hungarian notation.其他链接告诉我使用匈牙利符号,但一些链接告诉我使用匈牙利符号是不好的做法。

Personally using hangarian notation seems ok to me for controls.个人使用hangarian 符号对我来说似乎可以控制。 http://vb.byu.edu/vb/HungarianNotation.htm http://vb.byu.edu/vb/HungarianNotation.htm

But I am not sure, need some guidance?但我不确定,需要一些指导吗?

name it using the same convertion you use for the rest of your code.使用与代码的 rest 相同的转换来命名它。

if your code uses Hungarian, then name it using that.. if your code uses something else, use that.如果您的代码使用匈牙利语,则使用该名称命名它。如果您的代码使用其他名称,请使用该名称。

The one exception would be public-facing API (types, enums, etc).. I would suggest you take the route MS did and name things camel-case without prefixes and other cruft.一个例外是面向公众的 API(类型、枚举等)。我建议您采取 MS 所做的路线,并将其命名为驼峰式,不带前缀和其他杂音。

While I am personally opposed to Hungarian notation, I tend to do something similar with the names of Windows controls.虽然我个人反对匈牙利符号,但我倾向于对 Windows 控件的名称做类似的事情。 Rather than use a prefix, however, I tend to put the type of control as a suffix.然而,我倾向于将控件类型作为后缀,而不是使用前缀。

liquidHydrogenCapacityTextBox
extraKetchupCheckBox
fileOpenMenuItem
favoriteFormerNSyncMemberDropDownList

I think this is OK, not because it describes the type of the variable, but because it is descriptive of what the variable actually contains.我认为这没问题,不是因为它描述了变量的类型,而是因为它描述了变量实际包含的内容。

I used Hungarian notation for a while, but the main problem is that if you change the underlying type of your control you're kind of stuck into the name you decided.我使用了匈牙利符号一段时间,但主要问题是,如果您更改控件的底层类型,您就会陷入您决定的名称中。 Nowadays refactoring tools are helping, but in my everyday job I keep coming across "calSomething" or "edtWhatever" which are not calendars and editors anymore.如今,重构工具有所帮助,但在我的日常工作中,我不断遇到不再是日历和编辑器的“calSomething”或“edtWhatever”。 I remember that Microsoft was used to that too, so we still have wParam and lParam in the Windows SDK, despite the fact that they are basically the same since the advent of 32bit operating systems.我记得微软也习惯了,所以我们在 Windows SDK 中仍然有 wParam 和 lParam,尽管它们在 32 位操作系统出现后基本相同。

My suggestion is to not use Hungarian notation at all, but choosing meaningful names for your controls.我的建议是根本不要使用匈牙利符号,而是为您的控件选择有意义的名称。 Intellisense and such are more than helpful for determining the type of your control the majority of time, and using a prefix sometimes could make your code less clear to the occasional reader, because it diverts your attention from the semantics of your program to the mechanisms used by it. Intellisense 等在大多数情况下对确定控件的类型很有帮助,并且使用前缀有时可能会使您的代码对偶尔的读者来说不太清楚,因为它会将您的注意力从程序的语义转移到使用的机制上通过它。

I'll take the minority position here, and say that our shop still generally uses Hungarian notation for WinForms controls.我这里就拿少数position来说,说我们店里WinForms控件一般还是用匈牙利符号的。 Mostly because that was the standard back when we first wrote our UI, and there's been no reason to go back and do mass renames.主要是因为这是我们第一次编写 UI 时的标准回溯,并且没有理由回溯 go 并进行大规模重命名。

As we write new UI code though, especially WPF, we've been drifting more toward straightforward names like "userNameTextBox" (on those rare occasions when we need to name WPF controls at all).但是,当我们编写新的 UI 代码时,尤其是 WPF 时,我们更倾向于使用简单的名称,例如“userNameTextBox”(在极少数情况下,我们需要命名 WPF 控件)。 It's a bit more readable.它更具可读性。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM