首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我应该使用AutomationPeer还是AutomationElement?还是两者都有?

我应该使用AutomationPeer还是AutomationElement?还是两者都有?
EN

Stack Overflow用户
提问于 2015-06-26 15:33:27
回答 1查看 3.4K关注 0票数 4

我刚刚开始使用AutomationElement,因为我们希望对自定义控件进行集成测试,我认为我应该使用AutomationElement。

我已经成功地创建了一个带有自定义控件的窗口,并且可以成功地获得窗口和控件的AutomationElements。

代码语言:javascript
复制
  // Retrieve the View
  System.Windows.Automation.Condition viewCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, "MyTestView");
  AutomationElement view = AutomationElement.RootElement.FindFirst(TreeScope.Children, viewCondition);
  Assert.IsNotNull(view);

  // Retrieve the CustomControl
  System.Windows.Automation.Condition comboboxCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, "MyCustomControl");
  AutomationElement combobox = view.FindFirst(TreeScope.Children, comboboxCondition);
  Assert.IsNotNull(comboboxCondition);

现在,我想要做的是使用,例如ValuePattern。这就是我感到困惑的地方。

为了寻找信息,我在referencesource.microsoft.com上搜索了WPF的源代码。我遇到了ComboboxAutomationPeer,它实现了IValueProvider,所以现在我很困惑。

我是否也应该实现实现IValueProvider的IValueProvider,然后AutomationElement是否会与ValuePattern一起工作?还是应该让MyCustomControl实现IValueProvider?

EN

回答 1

Stack Overflow用户

发布于 2015-06-26 18:12:56

您不需要实现任何东西就可以使用模式。UI自动化为您(充当目标应用程序的代理)做到了这一点。这在这里的正式文档中得到了很好的解释:获得支持的UI自动化控制模式

下面是一个示例摘录:

代码语言:javascript
复制
    SelectionItemPattern pattern;
    try
    {
        pattern = yourAutomationElement.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
    }
    catch (InvalidOperationException ex)
    {
        Console.WriteLine(ex.Message);  // Most likely "Pattern not supported." 
        return;
    }
    pattern.Select();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31077082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档