【问题标题】:UI elements obscured from UIAutomation by UserControlUIAutomation 被 UserControl 遮挡的 UI 元素
【发布时间】:2017-11-28 19:40:19
【问题描述】:

我有一个自动化客户端,它使用 AutomationElement.FromPoint 方法来获取光标下的 AutomationElement:

 AutomationElement element = AutomationElement.FromPoint(point);

通常这很好用,但我一直遇到某些 WPF 应用程序的问题。当用户控件与另一个重要的 UI 元素处于同一 UI 级别时,就会出现此问题。

例如:

<Window x:Class="wpfTestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" xmlns:c="clr-namespace:wpfTestApp">
<Window.Resources>
    <c:NameList x:Key="NameListData"/>
</Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource NameListData}}" 
             Height="300" HorizontalAlignment="Left" Margin="10,10,0,0" Name="listBox1" 
             VerticalAlignment="Top" Width="153" >
        </ListBox>
        <UserControl Name="exampleUserControl">
              <TextBlock Visibility="Hidden">Loading...</TextBlock>
        </UserControl>
    </Grid>
</Window>

当我尝试指向任何列表框项(甚至列表框本身)时,我得到的只是“exampleUserControl”。

我知道还有其他方法可以获取不依赖于位置的 AutomationElement,但在这种情况下,这是我唯一的选择,因为我们试图获取光标下的元素。问题是,在这种情况下,重要的元素(即列表框项)被这个不重要的项(包含“正在加载...”文本的“exampleUserControl”)所覆盖。

FromPoint 方法是否有任何替代方法,或者我可以通过某种方式让它忽略这些元素?

【问题讨论】:

    标签: wpf ui-automation


    【解决方案1】:

    有几种方法可以找到/搜索元素。

    您可以在 MSDN http://msdn.microsoft.com/en-us/library/ms606809 上找到这些接口 例如:

    AutomationElement.FindFirst - http://msdn.microsoft.com/en-us/library/system.windows.automation.automationelement.findfirst

    AutomationElement.FindAll - http://msdn.microsoft.com/en-us/library/system.windows.automation.automationelement.findall

    // 示例:查找第一个元素和类名 = ""SciCalc" AutomationElement calcRoot = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "SciCalc"));

    “AutomationElement.RootElement”是所有当前打开的窗口和控件的父级。 为了提高性能,您可以先找到目标窗口,然后扫描目标窗口的 AutomationElement 上的控件。

    例如:您可以先通过“AutomationElement.FindFirst”或“AutomationElement.FromHandle”找到并创建目标 WPF 窗口,然后在目标窗口上搜索您的列表框。

    【讨论】:

      【解决方案2】:

      您的问题的干净解决方案是将Visibility="Hidden" 设置为UserControl 而不是TextBlock

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-08
        • 2019-07-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多