有没有一种方法来设置对象的AutomationID不使用XAML? [英] Is there a way to set the AutomationID of an object without using XAML?

查看:688
本文介绍了有没有一种方法来设置对象的AutomationID不使用XAML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个自动化的WinForm应用程序。我如何设置的AutomationID(或AutomationName)之类的XAML中的本文呢?

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

此的堆栈溢出文章,答案似乎是否定的,除非我切换应用程序的WPF应用程序(这样我就可以使用XAML来定义的控件)。

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 naive approach:

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



但在构造函数中这点控制,这些自动化特性只有干将;没有制定者

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

推荐答案

如果你想有关的任何设置为 UI自动化中的代码,你需要使用这样的:

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

using System.Windows.Automation;

而在你的代码:

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

您也可以使用 AutomationProperties.NameProperty 为UIAutomation名称。 AutomationProperties 包含UIAutomation元素(setter和getter)作为名称暗示的所有属性。

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.

这篇关于有没有一种方法来设置对象的AutomationID不使用XAML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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