Is there a way to set the AutomationID of an object without using XAML?

北战南征 提交于 2019-12-01 14:38:28

问题


I need to automate a Winform application. How do I set the AutomationID (or AutomationName) like the the XAML in this article does?

From this stack overflow article the answer seems to be no, unless I switch the application to a WPF application (so I can use XAML to define the controls).

I have tried this naïve approach:

  AutomationElement formAutomation = AutomationElement.FromHandle(this.Handle);
  formAutomation.Current.Name = "SandboxResponseDialogName";
  formAutomation.Current.ClassName = "SandboxResponseDialogClassName";
  formAutomation.Current.AutomationId = "SandboxResponseDialogID;

But at this point in the constructor for the control, these Automation properties have getters only; no setters.


回答1:


If you want to set anything in relation to UI Automation in code, you need to use this:

using System.Windows.Automation;

And in your code:

YourObjectClass element = // just get your element.
element.SetValue(AutomationProperties.AutomationIdProperty, "elementAutomationID");

You can also use AutomationProperties.NameProperty for the UIAutomation Name. AutomationProperties contains all the properties for UIAutomation elements (setter and getter) as the name suggest.



来源:https://stackoverflow.com/questions/14617061/is-there-a-way-to-set-the-automationid-of-an-object-without-using-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!