![]() |
---|
이 문서는 System.Windows.Automation 네임스페이스에 정의된 관리되는 UI Automation 클래스를 사용하려는 .NET Framework 개발자를 위해 작성되었습니다.UI Automation에 대한 최신 정보는 Windows Automation API: UI Automation을 참조하십시오. |
이 항목에서는 UI Automation 요소에서 컨트롤 패턴 개체를 검색하는 방법을 보여 줍니다.
모든 컨트롤 패턴 가져오기
원하는 컨트롤 패턴이 있는 AutomationElement를 가져옵니다.
GetSupportedPatterns를 호출하여 요소에서 모든 컨트롤 패턴을 가져옵니다.
![]() |
---|
클라이언트는 GetSupportedPatterns를 사용하지 않는 것이 좋습니다.이 메서드는 기존의 각 컨트롤 패턴에 대해 내부적으로 GetCurrentPattern을 호출하기 때문에 성능에 심각한 영향을 미칠 수 있습니다.가능하면 클라이언트는 원하는 주요 패턴에 대해 GetCurrentPattern을 호출해야 합니다. |
특정 컨트롤 패턴 가져오기
원하는 컨트롤 패턴이 있는 AutomationElement를 가져옵니다.
GetCurrentPattern 또는 TryGetCurrentPattern을 호출하여 특정 패턴을 쿼리합니다. 이들 메서드는 비슷하지만 패턴이 없는 경우 GetCurrentPattern은 예외를 발생시키고 TryGetCurrentPattern은 false를 반환합니다.
예제
다음 예제에서는 AutomationElement에서 목록 항목을 검색하고 해당 요소에서 SelectionItemPattern을 가져옵니다.
''' <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 'SelectListItem
/// <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();
}
}