获取受支持的 UI 自动化控件模式Get Supported UI Automation Control Patterns

备注

本文档适用于想要使用 UI 自动化UI Automation 命名空间中定义的托管 System.Windows.Automation 类的 .NET Framework 开发人员。This documentation is intended for .NET Framework developers who want to use the managed UI 自动化UI Automation classes defined in the System.Windows.Automation namespace. 有关 UI 自动化UI Automation的最新信息,请参阅 Windows 自动化 API:UI 自动化For the latest information about UI 自动化UI Automation, see Windows Automation API: UI Automation.

本主题演示如何从 UI 自动化UI Automation 元素检索控件模式对象。This topic shows how to retrieve control pattern objects from UI 自动化UI Automation elements.

获取所有控件模式Obtain All Control Patterns

  1. 获取你对其控件模式感兴趣的 AutomationElementGet the AutomationElement whose control patterns you are interested in.

  2. 调用 GetSupportedPatterns,以便从该元素获取所有控件模式。Call GetSupportedPatterns to get all control patterns from the element.

注意

强烈建议客户端不要使用 GetSupportedPatternsIt is strongly recommended that a client not use GetSupportedPatterns. 因为此方法针对每个现有的控件模式在内部调用 GetCurrentPattern,所以性能会受到严重影响。Performance can be severely affected as this method calls GetCurrentPattern internally for each existing control pattern. 如有可能,客户端应当针对相关的关键模式来调用 GetCurrentPatternIf possible, a client should call GetCurrentPattern for the key patterns of interest.

获取特定的控件模式Obtain a Specific Control Pattern

  1. 获取你对其控件模式感兴趣的 AutomationElementGet the AutomationElement whose control patterns you are interested in.

  2. 调用 GetCurrentPatternTryGetCurrentPattern 以查询特定模式。Call GetCurrentPattern or TryGetCurrentPattern to query for a specific pattern. 这两种方法非常相似,但是,如果没有找到该模式,GetCurrentPattern 会引发异常,而 TryGetCurrentPattern 会返回 falseThese methods are similar, but if the pattern is not found, GetCurrentPattern raises an exception, and TryGetCurrentPattern returns false.

示例Example

下面的示例检索某个列表项的 AutomationElement,并从该元素获取一个 SelectionItemPatternThe following example retrieves an AutomationElement for a list item and obtains a SelectionItemPattern from that element.

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

另请参阅See also