WPF UI元素命名约定 [英] WPF UI element naming conventions

查看:475
本文介绍了WPF UI元素命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然匈牙利符号现在被认为是不好的做法,但仍然很常见的是请使用前缀( lblTitle txtFirstName 用户界面元素 >,...)或后缀( TitleLabel FirstNameTextBox ,...)。

Although Hungarian notation is considered bad practice nowadays, it is still quite common to encode the type in the name of user interface elements, either by using a prefix (lblTitle, txtFirstName, ...) or a suffix (TitleLabel, FirstNameTextBox, ...).

在我的公司,我们也这样做,因为它使得由同事(或很久以前自己)写的代码更容易阅读(在我的经验中)。通常反对这样做的参数 - 你必须改变变量的名称,如果类型改变 - 不是很强,因为改变UI元素的类型通常需要重写代码的所有部分,无论如何它被引用。

In my company, we also do this, since it makes code written by co-workers (or by yourself a long time ago) easier to read (in my experience). The argument usually raised against doing this -- you have to change the name of the variable if the type changes -- is not very strong, since changing the type of a UI element usually requires rewriting all parts of the code were it is referenced anyway.

所以,我正在考虑在开始开发WPF时保持这种做法(hmmm ...我们应该使用 txt TextBlocks或TextBoxes的前缀?)。有没有什么大的缺点,我错过了?这是你的机会,说不要这样做,因为...。

So, I'm thinking about keeping this practice when starting with WPF development (hmmm... should we use the txt prefix for TextBlocks or TextBoxes?). Is there any big disadvantage that I have missed? This is your chance to say "Don't do this, because ...".

编辑:我知道使用数据绑定需要减少UI元素。然而,有时,例如,当开发自定义控件...

I know that with databinding the need to name UI elements decreases. Nevertheless, it's necessary sometimes, e.g. when developing custom controls...

推荐答案

个人,我发现WPF更改规则。通常,你可以很少或没有代码背后,所以使用前缀来区分名称使事情更混乱,而不是混乱。

Personally, I find that WPF changes the rules when it comes to this. Often, you can get away with little or no code behind, so having the prefixes to distinguish names makes things more confusing instead of less confusing.

在Windows窗体中,每个控件由代码中的名称引用。有了一个大的用户界面,半匈牙利记号是有用的 - 更容易区分你正在使用什么。

In Windows Forms, every control was referenced by name in code. With a large UI, the semi-hungarian notation was useful - it was easier to distinguish what you were working with.

但在WPF,这是一个罕见的控制,需要一个名字。当你必须通过代码访问控件时,最好使用附加的属性或行为来实现,在这种情况下,你永远不会处理多个控件。如果你在UserControl或Window代码中工作,我只是使用Title和Name而不是txtTitle,尤其是现在你可能只处理几个有限的控件

In WPF, though, it's a rare control that needs a name. When you do have to access a control via code, it's often best to use attached properties or behaviors to do so, in which case you're never dealing with more than a single control. If you're working in the UserControl or Window code-behind, I'd just use "Title" and "Name" instead of "txtTitle", especially since now you'll probably only be dealing with a few, limited controls, instead of all of them.

在大多数情况下,即使自定义控件也不需要名称。您将需要遵循约定的模板名称(即:PART_Name),但不是实际的x:用于UI的名称元素...

Even custom controls shouldn't need names, in most cases. You'll want templated names following convention (ie: PART_Name), but not actual x:Name elements for your UIs...

这篇关于WPF UI元素命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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