AutomationElement.GetCurrentPattern(AutomationPattern) Methode

Definitie

Hiermee haalt u het opgegeven patroonobject op.AutomationElement

public:
 System::Object ^ GetCurrentPattern(System::Windows::Automation::AutomationPattern ^ pattern);
public object GetCurrentPattern(System.Windows.Automation.AutomationPattern pattern);
member this.GetCurrentPattern : System.Windows.Automation.AutomationPattern -> obj
Public Function GetCurrentPattern (pattern As AutomationPattern) As Object

Parameters

pattern
AutomationPattern

De id van het patroon dat moet worden opgehaald.

Retouren

Het patroonobject, als het opgegeven patroon momenteel wordt ondersteund door de AutomationElement.

Uitzonderingen

Het patroon wordt niet ondersteund door het element.

De gebruikersinterface voor de AutomationElement app bestaat niet meer.

Voorbeelden

In het volgende voorbeeld ziet u hoe u deze methode gebruikt om een SelectionItemPatternop te halen, die vervolgens wordt gebruikt om een item in een keuzelijst te selecteren.

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

Note

Voor vaak herhaalde taken, zoals de taken in het voorbeeld, is het efficiƫnter om het patroon op te cachen en te gebruiken GetCachedPattern.

Opmerkingen

GetCurrentPattern haalt het opgegeven patroon op basis van de beschikbaarheid op het moment van de aanroep.

Voor sommige vormen van gebruikersinterface leidt deze methode tot overhead voor de prestaties van meerdere processen. Toepassingen kunnen overhead concentreren door patronen in de cache op te slaan en ze vervolgens op te halen met behulp van GetCachedPattern.

Van toepassing op

Zie ook