AutomationElement.GetCurrentPattern(AutomationPattern) メソッド

定義

この AutomationElement の指定したパターン オブジェクトを取得します。Retrieves the specified pattern object on this 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

パラメーター

pattern
AutomationPattern

取得するパターンの識別子。The identifier of the pattern to retrieve.

戻り値

パターン オブジェクト (指定したパターンが現在 AutomationElement でサポートされている場合)。The pattern object, if the specified pattern is currently supported by the AutomationElement.

例外

パターンは要素でサポートされていません。The pattern is not supported by the element.

AutomationElement の UI はなくなりました。The UI for the AutomationElement no longer exists.

次の例は、このメソッドを使用して、 SelectionItemPatternリストボックス内の項目を選択するために使用されるを取得する方法を示しています。The following example shows how to use this method to retrieve a SelectionItemPattern, which is then used to select an item in a list box.

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

注意

例のような繰り返しのタスクの場合、パターンをキャッシュして使用GetCachedPatternする方が効率的です。For often-repeated tasks such as the one in the example, it would be more efficient to cache the pattern and use GetCachedPattern.

注釈

GetCurrentPattern呼び出し時の使用可能範囲に基づいて、指定されたパターンを取得します。GetCurrentPattern gets the specified pattern based on its availability at the time of the call.

UIUI形式によっては、この方法でプロセス間のパフォーマンスのオーバーヘッドが発生します。For some forms of UIUI, this method will incur cross-process performance overhead. アプリケーションでは、パターンをキャッシュし、を使用GetCachedPatternしてそれらを取得することによって、オーバーヘッドに専念できます。Applications can concentrate overhead by caching patterns and then retrieving them by using GetCachedPattern.

適用対象

こちらもご覧ください