Pobierz obsługiwane wzorce kontrolek automatyzacji interfejsu użytkownika
Uwaga
Ta dokumentacja jest przeznaczona dla deweloperów programu .NET Framework, którzy chcą używać zarządzanych klas automatyzacja interfejsu użytkownika zdefiniowanych w System.Windows.Automation przestrzeni nazw. Aby uzyskać najnowsze informacje na temat automatyzacja interfejsu użytkownika, zobacz Interfejs API usługi Windows Automation: automatyzacja interfejsu użytkownika.
W tym temacie przedstawiono sposób pobierania obiektów wzorca sterowania z elementów automatyzacja interfejsu użytkownika.
Uzyskiwanie wszystkich wzorców kontrolek
AutomationElement Uzyskaj wzorce sterujące, które cię interesują.
Wywołaj metodę GetSupportedPatterns , aby pobrać wszystkie wzorce kontrolek z elementu .
Uwaga
Zdecydowanie zaleca się, aby klient nie używał polecenia GetSupportedPatterns. Wydajność może mieć poważny wpływ, ponieważ ta metoda wywołuje GetCurrentPattern wewnętrznie dla każdego istniejącego wzorca kontrolki. Jeśli to możliwe, klient powinien wywołać GetCurrentPattern kluczowe wzorce zainteresowania.
Uzyskiwanie określonego wzorca kontrolki
AutomationElement Uzyskaj wzorce sterujące, które cię interesują.
Wywołanie GetCurrentPattern lub TryGetCurrentPattern zapytanie dotyczące określonego wzorca. Te metody są podobne, ale jeśli wzorzec nie zostanie znaleziony, GetCurrentPattern zgłosi wyjątek i TryGetCurrentPattern zwróci wartość
false
.
Przykład
Poniższy przykład pobiera element AutomationElement listy i uzyskuje element SelectionItemPattern z tego elementu.
/// <summary>
/// Sets the focus to a list and selects a string item in that list.
/// </summary>
/// <param name="listElement">The list element.</param>
/// <param name="itemText">The text to select.</param>
/// <remarks>
/// This deselects any currently selected items. To add the item to the current selection
/// in a multiselect list, use AddToSelection instead of Select.
/// </remarks>
public void SelectListItem(AutomationElement listElement, String itemText)
{
if ((listElement == null) || (itemText == ""))
{
throw new ArgumentException("Argument cannot be null or empty.");
}
listElement.SetFocus();
Condition cond = new PropertyCondition(
AutomationElement.NameProperty, itemText, PropertyConditionFlags.IgnoreCase);
AutomationElement elementItem = listElement.FindFirst(TreeScope.Children, cond);
if (elementItem != null)
{
SelectionItemPattern pattern;
try
{
pattern = elementItem.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message); // Most likely "Pattern not supported."
return;
}
pattern.Select();
}
}
''' <summary>
''' Sets the focus to a list and selects a string item in that list.
''' </summary>
''' <param name="listElement">The list element.</param>
''' <param name="itemText">The text to select.</param>
''' <remarks>
''' This deselects any currently selected items. To add the item to the current selection
''' in a multiselect list, use AddToSelection instead of Select.
''' </remarks>
Public Sub SelectListItem(ByVal listElement As AutomationElement, ByVal itemText As String)
If listElement Is Nothing OrElse itemText = "" Then
Throw New ArgumentException("Argument cannot be null or empty.")
End If
listElement.SetFocus()
Dim cond As New PropertyCondition(AutomationElement.NameProperty, itemText, PropertyConditionFlags.IgnoreCase)
Dim elementItem As AutomationElement = listElement.FindFirst(TreeScope.Children, cond)
If Not (elementItem Is Nothing) Then
Dim pattern As SelectionItemPattern
Try
pattern = DirectCast(elementItem.GetCurrentPattern(SelectionItemPattern.Pattern), _
SelectionItemPattern)
Catch ex As InvalidOperationException
Console.WriteLine(ex.Message) ' Most likely "Pattern not supported."
Return
End Try
pattern.Select()
End If
End Sub