Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取WPF用户控件的AutomationElement_C#_Wpf_Ui Automation_Automationelement - Fatal编程技术网

C# 获取WPF用户控件的AutomationElement

C# 获取WPF用户控件的AutomationElement,c#,wpf,ui-automation,automationelement,C#,Wpf,Ui Automation,Automationelement,我想在测试期间使用WindowsAutomationElements模拟用户输入。 我的特殊用例是制作一个列表框选择,根据我在网上找到的内容,我需要为我的列表框添加一个AutomationElement来操作它 假设我有一扇这样的窗户: <Window x:Class="CryptoAdmin_Test.Helper.FreshWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentat

我想在测试期间使用Windows
AutomationElement
s模拟用户输入。 我的特殊用例是制作一个列表框选择,根据我在网上找到的内容,我需要为我的列表框添加一个AutomationElement来操作它

假设我有一扇这样的窗户:

<Window x:Class="CryptoAdmin_Test.Helper.FreshWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:CryptoAdmin_Test.Helper">
    <StackPanel>
        <UserControl x:FieldModifier="public" x:Name="FindMe" />
    </StackPanel>
</Window>

由于我有一个对UserControl的引用,我应该能够在不从桌面开始搜索的情况下找到它(
AutomationElement.RootElement

为我的
窗口获取
自动元素
的最快方法是什么。FindMe
用户控件


使用AutomationElement.RootElement.FindFirst(…)将从桌面开始,我看不到一种通用的方法可以使搜索快速,而不会出现误报。

这应该是找到它的最快方法。这还假设您为窗口指定了一个名称,因为除非您从应用程序启动进程并为其设置进程id,否则很难找到窗口

AutomationElement mainWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "MainWindow"));
AutomationElement findMe = mainWindow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "FindMe"));

由于TreeScope设置为children,因此在查找相关元素时,它不会扫描整个树。根据用户控件所做的操作,返回的元素可能会有些无用。如果不为控件实现一些自定义模式,您唯一能做的就是从中获取其他元素。

是的,我不得不放弃不从根开始查找任何内容的尝试。