2016-11-28 21 views
1

我想在測試期間使用Windows AutomationElements來模擬Userinput。 我的particulat用例是manupilating列表框選擇和我在網上找到的,我將需要一個AutomationElement爲我的列表框來操縱它。獲取WPF用戶控件的自動化元素

假設我有這樣一個窗口:

<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> 

由於我必須到用戶控件的一個參考,我應該能夠找到它,而無需啓動從桌面(AutomationElement.RootElement)我的搜索。

什麼是最快的方式獲得AutomationElement我的window.FindMeUserControl

使用AutomationElement.RootElement.FindFirst(...);將開始與桌面,我沒有看到一種通用的方式,這將使此搜索快速沒有任何可能的誤報。

回答

0

這應該是找到它的最快方法。這也假定你給這個窗口一個名字,否則它很難找到,除非你從你的應用程序啓動這個進程並且有一個進程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設置爲子項,因此在查找有問題的元素時不會掃描整個樹。取決於你的用戶控件的作用,你找回的元素可能有些無用。如果沒有爲你的控制實現一些自定義模式,你將只能從中獲取其他元素。

+0

是的,我不得不放棄試圖找到任何東西,而不從根開始。 – Johannes