AutomationProperties.Name
之间的“CodedUI 测试构建器”没有区别。和 x:Name
.但是第一个可以覆盖第二个。
AtomationProperties.Name 也支持数据绑定(bind),x:Name
当然没有。
As we know如果您使用 MVVM 模式,最好只使用 x:Name
需要的时候。AutomationProperties.Name
也是如此优先于x:Name
?
最佳答案
概括x:Name
和 AutomationProperties.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
:自动化属性.名称
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/