【问题标题】:Is there a way to set the AutomationID of an object without using XAML?有没有办法在不使用 XAML 的情况下设置对象的 AutomationID?
【发布时间】:2013-01-31 01:36:10
【问题描述】:

我需要自动化 Winform 应用程序。如何像 this article 中的 XAML 一样设置 AutomationID(或 AutomationName)?

从这个stack overflow article 看来,答案似乎是否定的,除非我将应用程序切换到 WPF 应用程序(这样我就可以使用 XAML 来定义控件)。

我尝试过这种幼稚的方法:

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

但此时在控件的构造函数中,这些自动化属性只有 getter;没有二传手。

【问题讨论】:

    标签: c# winforms ui-automation


    【解决方案1】:

    如果你想在代码中设置任何与UI Automation 相关的东西,你需要使用这个:

    using System.Windows.Automation;
    

    在你的代码中:

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

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

    【讨论】:

    • 请注意,在此示例中,element 不是 UI 自动化元素 (AutomationElement),而是 WPF 控件(例如 CheckBox)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多