wpf 差异 AutomationProperties.AutomationId AutomationProperties.Name
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22711461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference AutomationProperties.AutomationId AutomationProperties.Name
提问by Hansa
Is there any real differencebetween the XAML Propertys AutomationProperties.AutomationIdand AutomationProperties.Name?
XAML 属性AutomationProperties.AutomationId和 AutomationProperties.Name之间 有什么真正的区别吗?
And most importantly, is there any reason to choose one over the other?
最重要的是,是否有任何理由选择其中之一?
Neither of them seem unique to me although msdnsays so about .AutomationId: "The string that uniquely identifies the specified element."
尽管msdn对 .AutomationId 这么说,但它们对我来说似乎都不是唯一的:“唯一标识指定元素的字符串。”
I tested a WPF application with 2 buttons each with the same .AutomationId within the same tree path, and the .AutomationId is not unique at all.
我测试了一个 WPF 应用程序,其中有 2 个按钮,每个按钮在同一树路径中具有相同的 .AutomationId,并且 .AutomationId 根本不是唯一的。
XAML allows me to give them the same .AutomationId, and if I search for them via the following code, it returns both Buttons.
XAML 允许我为它们提供相同的 .AutomationId,如果我通过以下代码搜索它们,它会返回两个按钮。
PropertyCondition cond = new PropertyCondition(AutomationElement.AutomationIdProperty, "myButton");
returnedAEs = myMainWindow.FindAll(TreeScope.Children, cond);
foreach (AutomationElement element in returnedAEs)
{
//...enters here 2 times
}
XAML:
XAML:
<Window ... >
<Grid ...>
<Button ... AutomationProperties.AutomationId="myButton"/>
<Button ... AutomationProperties.AutomationId="myButton"/>
<Window ... >
</Grid>
</Window>
Your input would be very much appreciated!
非常感谢您的意见!
回答by Hansa
Here are some differences I found:
以下是我发现的一些差异:
- It seems like Inspect.exe (former UISpy.exe) can only read .AutomationId, but not .AutomationName
- this two links about nameand automationIdmight be useful as well. biggest difference from the two links: "If AutomationProperties.AutomationId is not specifically set, it returns the AutomationProperties.Name property value." this is not specified for the name property.
- If neither AutomationProperties.AutomationId nor AutomationProperties.Name are set, and in xaml the Name (not x:Name, that one doesn't work!!) is set, then the AutomationProperties.AutomationId is set to the same value as the Name Property is (be aware that I mean two differnt things with the Name (standard XAML Property) and AutomationProperties.Name!!)
If for instance the following TextBlock is used:
<TextBlock x:Name="msgStatus" Text="idle" ... />the value "idle" of the Text Property can be accessed by AutomationProperties.Name! The same applies for reading the text of Labels. A Labels Content can also be read by the AutomationProperties.Name(if the AutomationProperties.Name is not specififed in the .xaml)!
- 似乎Inspect.exe(以前的UISpy.exe)只能读取.AutomationId,而不能读取.AutomationName
- 这两个关于name和 automationId 的链接 也可能有用。两个链接的最大区别:“如果没有专门设置AutomationProperties.AutomationId,则返回AutomationProperties.Name 属性值。” 这不是为 name 属性指定的。
- 如果 AutomationProperties.AutomationId 和 AutomationProperties.Name 都没有设置,并且在 xaml 中设置了 Name(不是 x:Name,那个不起作用!!),则 AutomationProperties.AutomationId 设置为与 Name 属性相同的值是(请注意,我的意思是 Name(标准 XAML 属性)和 AutomationProperties.Name 两个不同的东西!!)
例如,如果使用以下 TextBlock:
<TextBlock x:Name="msgStatus" Text="idle" ... />可以通过 AutomationProperties.Name 访问 Text 属性的值“空闲”!这同样适用于阅读标签的文本。标签内容也可以由 AutomationProperties.Name 读取(如果 .xaml 中未指定 AutomationProperties.Name)!
回答by ouflak
It really just may be a matter of context and maintenance. Most people would expect the ID to be used something like the Tag attribute on a control, to convey information about the base object that is not otherwise available through a standard property and, in this case, is also a form of identification. The Name is just that, a name. You might not have any issues in your own code using the two interchangeably, as long as you keep track of it and remember you've done so, but if this code had to be integrated into someone else's code, or you yourself have to re-visit it some months/years later, the usage of the properties in ways in which they were not intended could be problematic. Note that you might be able to give two different buttons the same ID, but you cannot give them the same Name. As counter-intuitive as it might seem, this actually give you more flexibilityfor what to do with that ID. For example, that ID might be used to very simply identify what page that button resides on. All the buttons on that are on a particular Page might have an AutomationID of '1'. But they each have their own unique Name. In such a case, your little method to return all of the buttons on Page '1' could be very handy.
这实际上可能只是上下文和维护问题。大多数人都希望 ID 用于控件上的 Tag 属性之类的东西,以传达有关基础对象的信息,而这些信息在其他情况下无法通过标准属性获得,在这种情况下,也是一种标识形式。名字只是一个名字。你可能不会在你自己的代码中交替使用这两个问题,只要你跟踪它并记住你已经这样做了,但是如果必须将此代码集成到其他人的代码中,或者你自己必须重新- 几个月/几年后访问它,以非预期的方式使用这些属性可能会出现问题。请注意,您可以为两个不同的按钮提供相同的 ID,但不能为它们提供相同的名称。尽管看起来有悖常理,更灵活地处理该 ID。例如,该 ID 可用于非常简单地标识该按钮所在的页面。特定页面上的所有按钮的 AutomationID 可能为“1”。但他们每个人都有自己独特的名字。在这种情况下,返回页面“1”上所有按钮的小方法可能非常方便。

