.net - AutomationProperties.Name VS x :Name

标签 .net wpf silverlight accessibility ui-automation

AutomationProperties.Name 之间的“CodedUI 测试构建器”没有区别。和 x:Name .但是第一个可以覆盖第二个。
AtomationProperties.Name 也支持数据绑定(bind),x:Name当然没有。

As we know如果您使用 MVVM 模式,最好只使用 x:Name需要的时候。
AutomationProperties.Name 也是如此优先于x:Name ?

最佳答案

概括
x:NameAutomationProperties.Name是两个完全不同的东西,所以“我应该使用一个还是另一个”这个问题是基于一个错误的前提:一般来说,你不能使用一个或另一个。
x:Name的目的|是在代码隐藏中标识一个 WPF 控件,以便开发人员可以访问它。在为特定 WPF 元素建模的类范围之外,它没有意义(或唯一)。

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

虽然任何工具(例如 XAML 编译器)都可以选择使用 x:Name 的值对于 AutomationProperties.Name也并不意味着这是您应该做的事情;恕我直言,这正是导致问题的“便利”类型,因为两者之间的差异对开发人员是隐藏的,因此总是一个或另一个属性最终会具有语义错误的值。

有关每个属性的语义和技术方面的信息,请参见下一节。

x:名称

MSDN documentation页面解释说

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.

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

[...]

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 :
  • 用于访问代码中的元素(不是 XAML),因为它控制包含元素
  • 的字段的名称
  • XAML namescope 中必须是唯一的(因为代码中不能有两个同名的字段)

  • 自动化属性.名称

    WPF accessibility documentation解释说

    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.

    关于.net - AutomationProperties.Name VS x :Name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4605777/

    相关文章:

    c# - 对 MVC 站点具有特定要求的粒度权限

    .net - WPF 桌面应用程序中的 gRPC 服务器?

    c# - 将证书注册到 SSL 端口

    wpf - 将插入符号/光标位置设置为字符串值 WPF 文本框的末尾

    silverlight - Silverlight如何在数据模板中使用按钮时提升列表框的选定项目

    c# - 如何从 C# 调用设置 int 数组值的 C++ 函数?

    wpf - 您如何让 Silverlight 调整文本内容的大小以适合?

    wpf - 从 DataTrigger 内部更改样式

    silverlight - 自定义pivotitem header时的问题

    c# - 高效地将音频字节 - byte[] 转换为 Short[]