我正在尝试使用部分NameProperty识别Windows静态文本控件。这是我的代码:

// Get a reference to the window
AutomationElement epoWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "MsiDialog"));
// Get a reference to the control
AutomationElement epoControl = epoWindow.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, controlText));

目前,我需要完整的controlText字符串才能正常工作,但我想搜索该字符串的一部分并返回找到的所有控件。
我该怎么做呢?
谢谢,
约翰

最佳答案

您可以使用预定义的TrueCondition对子集合进行迭代,如下所示:

  foreach(AutomationElement child in epoWindow.FindAll(TreeScope.Subtree, Condition.TrueCondition))
  {
      if (child.Current.Name.Contains("whatever"))
      {
          // do something
      }
  }

PS:如果您不想降低应用程序的性能(如果它具有较大的子层次结构)或无限期等待,则需要仔细选择TreeScope

关于c# - 在Windows Automation中使用部分NameProperty查找Windows控件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30804184/

10-12 20:04