【发布时间】: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