AutomationProperties.Name VS x:Name [英] AutomationProperties.Name VS x:Name

查看:27
本文介绍了AutomationProperties.Name VS x:Name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AutomationProperties.Namex:Name 之间的CodedUI 测试构建器"没有区别.但是第一个可以覆盖第二个.AtomationProperties.Name 也支持数据绑定,x:Name 当然不支持.

There is no difference for the "CodedUI test builder" between the AutomationProperties.Name and x:Name. But the first one can override the second one. Also the AtomationProperties.Name supports data binding, x:Name of course doesn't.

我们知道如果您使用的是 MVVM 模式,最好仅在需要时使用 x:Name.

As we know if you are using the MVVM pattern it is best to only use x:Name when needed.

那么 AutomationProperties.Name 应该比 x:Name 更受欢迎吗?

So should AutomationProperties.Name be preferred to x:Name?

推荐答案

总结

x:NameAutomationProperties.Name 是两个完全不同的东西,所以我应该使用一个还是另一个"这个问题是基于一个错误的前提:在一般情况下,您不能使用其中一种.

Summary

x:Name and AutomationProperties.Name are two totally different things, so the question "should I use one or the other" is based on a false premise: in general, you cannot use one or the other.

x:Name 的目的是识别代码隐藏中的 WPF 控件,以便开发人员可以访问它.在对特定 WPF 元素建模的类的范围之外,它没有意义(或唯一).

The purpose of x:Name is to identify a WPF control in code-behind so that the developer can access it. It is not meaningful (or unique) outside the scope of the class that models a specific WPF element.

另一方面,AutomationProperties.Name 的目的是在呈现给用户进行交互的对话框或其他类型窗口的上下文中识别用户界面元素.具体来说,它的值应该与用户认为该用户界面元素的标签"相匹配(例如,无障碍工具可以告知用户该元素的用途).

On the other hand, the purpose of AutomationProperties.Name is to identify a user interface element in the context of a dialog or other type of window that is presented to the user for interaction. Specifically, its value should match what a user would perceive as the "label" of that user interface element (so that e.g. an accessibility tool can inform the user of the purpose of the element).

虽然任何工具(例如 XAML 编译器)都可以选择将 x:Name 的值用于 AutomationProperties.Name ,但这并不意味着它是某种东西你应该做;恕我直言,这正是导致问题的便利"类型,因为两者之间的差异对开发人员是隐藏的,因此一个或另一个属性最终会产生语义错误的值.

While any tool (such as a XAML compiler) can choose to use the value of x:Name for AutomationProperties.Name as well doesn't mean that it's something you should do; IMHO this is exactly the type of "convenience" that results in problems because the difference between the two is hidden from the developer, so invariably one or the other property would end up having a semantically wrong value.

有关每个属性的语义和技术方面的信息将在接下来的部分中介绍.

Information on the semantic and technical aspects of each of the property follows in the next sections.

MSDN 文档页面解释了这一点

将 x:Name 应用于框架的支持编程模型后,名称等同于保存对象引用的变量或构造函数返回的实例.

After x:Name is applied to a framework's backing programming model, the name is equivalent to the variable that holds an object reference or an instance as returned by a constructor.

x:Name 指令用法的值在 XAML 中必须是唯一的名称范围.

The value of an x:Name directive usage must be unique within a XAML namescope.

[...]

在使用的 WPF 应用程序的标准构建配置下XAML、部分类和代码隐藏,指定的 x:Name 变为XAML 时在底层代码中创建的字段的名称由标记编译构建任务处理,该字段保存对对象的引用.

Under the standard build configuration for a WPF application that uses XAML, partial classes, and code-behind, the specified x:Name becomes the name of a field that is created in the underlying code when XAML is processed by a markup compilation build task, and that field holds a reference to the object.

从上面我们可以看出x:Name:

  1. 用于访问代码中的元素(不是 XAML),因为它控制着包含元素的字段的名称
  2. XAML 名称范围中必须是唯一的(因为不能有两个相同的字段)代码中的名称)
  1. is used to access the element in code (not XAML), since it controls the name of the field that holds the element
  2. must be unique within a XAML namescope (since you cannot have two fields with the same name in code)

自动化属性.名称

WPF 辅助功能文档 解释了这一点

自动化元素的名称由开发人员分配.这名称属性应始终与上的标签文本一致屏幕.例如,按钮元素的名称必须是浏览..."以浏览..."作为标签.

The Name for an automation element is assigned by the developer. The Name property should always be consistent with the label text on screen. For example, the Name must be "Browse…" for the button element with "Browse…" as the label.

这篇关于AutomationProperties.Name VS x:Name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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