다음을 통해 공유


UI 자동화의 캐싱 사용

참고참고

이 문서는 System.Windows.Automation 네임스페이스에 정의된 관리되는 UI Automation 클래스를 사용하려는 .NET Framework 개발자를 위해 작성되었습니다.UI Automation에 대한 최신 정보는 Windows Automation API: UI Automation을 참조하십시오.

이 단원에서는 AutomationElement 속성 및 컨트롤 패턴의 캐싱을 구현하는 방법을 보여 줍니다.

캐시 요청 활성화

  1. CacheRequest를 만듭니다.

  2. Add를 사용하여 캐시할 속성 및 패턴을 지정합니다.

  3. TreeScope 속성을 설정하여 캐시 범위를 지정합니다.

  4. TreeFilter 속성을 설정하여 하위 트리의 뷰를 지정합니다.

  5. 개체에 대한 전체 참조를 검색하지 않음으로써 효율성을 높이려면 AutomationElementMode 속성을 None으로 설정합니다. 이렇게 하면 해당 개체에서 현재 값을 검색할 수 없습니다.

  6. using 블록(Microsoft Visual Basic .NET에서는 Using) 안에 Activate를 사용하여 요청을 활성화합니다.

AutomationElement 개체를 가져오거나 이벤트를 구독하는 후 Pop를 사용하거나(Push가 사용된 경우) Activate로 생성된 개체를 삭제하여 요청을 비활성화합니다. 여기서 Activate는 using 블록(Microsoft Visual Basic .NET에서는 Using) 안에 사용합니다.

AutomationElement 속성 캐시

  1. CacheRequest가 활성화된 상태에서 FindFirst 또는 FindAll을 사용하여 AutomationElement 개체를 가져옵니다. 또는 CacheRequest가 활성화될 때 등록한 이벤트의 소스로 AutomationElement를 가져옵니다. 또한 CacheRequest를 GetUpdatedCache 또는 TreeWalker 메서드 중 하나에 전달하여 캐시를 만들 수도 있습니다.

  2. GetCachedPropertyValue를 사용하거나 AutomationElementCached 속성에서 속성을 검색합니다.

캐시된 패턴 및 속성 가져오기

  1. CacheRequest가 활성화된 상태에서 FindFirst 또는 FindAll을 사용하여 AutomationElement 개체를 가져옵니다. 또는 CacheRequest가 활성화될 때 등록한 이벤트의 소스로 AutomationElement를 가져옵니다. 또한 CacheRequest를 GetUpdatedCache 또는 TreeWalker 메서드 중 하나에 전달하여 캐시를 만들 수도 있습니다.

  2. GetCachedPattern 또는 TryGetCachedPattern을 사용하여 캐시된 패턴을 검색합니다.

  3. 컨트롤 패턴의 Cached 속성에서 속성 값을 검색합니다.

예제

다음 코드 예제에서는 CacheRequest를 활성화하기 위해 Activate를 사용함으로써 캐시의 다양한 측면을 보여 줍니다.

    ''' <summary>
    ''' Caches and retrieves properties for a list item by using CacheRequest.Activate.
    ''' </summary>
    ''' <param name="elementList">Element from which to retrieve a child element.</param>
    ''' <remarks>
    ''' This code demonstrates various aspects of caching. It is not intended to be 
    ''' an example of a useful method.
    ''' </remarks>
    Private Sub CachePropertiesByActivate(ByVal elementList As AutomationElement)

        ' Set up the request.
        Dim myCacheRequest As New CacheRequest()
        myCacheRequest.Add(AutomationElement.NameProperty)
        myCacheRequest.Add(AutomationElement.IsEnabledProperty)
        myCacheRequest.Add(SelectionItemPattern.Pattern)
        myCacheRequest.Add(SelectionItemPattern.SelectionContainerProperty)

        Dim elementListItem As AutomationElement

        ' Obtain an element and cache the requested items.
        Using myCacheRequest.Activate()
            Dim myCondition As New PropertyCondition( _
                AutomationElement.IsSelectionItemPatternAvailableProperty, True)
            elementListItem = elementList.FindFirst(TreeScope.Children, myCondition)
        End Using


        ' The CacheRequest is now inactive.
        ' Retrieve the cached property and pattern.
        Dim pattern As SelectionItemPattern
        Dim itemName As String
        Try
            itemName = elementListItem.Cached.Name
            pattern = DirectCast(elementListItem.GetCachedPattern(SelectionItemPattern.Pattern), _
                SelectionItemPattern)
        Catch ex As InvalidOperationException
            Console.WriteLine("Object was not in cache.")
            Return
        End Try
        ' Alternatively, you can use TryGetCachedPattern to retrieve the cached pattern.
        Dim cachedPattern As Object = Nothing
        If True = elementListItem.TryGetCachedPattern(SelectionItemPattern.Pattern, cachedPattern) Then
            pattern = DirectCast(cachedPattern, SelectionItemPattern)
        End If

        ' Specified pattern properties are also in the cache.
        Dim parentList As AutomationElement = pattern.Cached.SelectionContainer

        ' The following line will raise an exception, because the HelpText property was not cached.
        '** String itemHelp = elementListItem.Cached.HelpText; **

        ' Similarly, pattern properties that were not specified in the CacheRequest cannot be 
        ' retrieved from the cache. This would raise an exception.
        '** bool selected = pattern.Cached.IsSelected; **

        ' This is still a valid call, even though the property is in the cache.
        ' Of course, the cached value and the current value are not guaranteed to be the same.
        itemName = elementListItem.Current.Name
    End Sub 'CachePropertiesByActivate

/// <summary>
/// Caches and retrieves properties for a list item by using CacheRequest.Activate.
/// </summary>
/// <param name="elementList">Element from which to retrieve a child element.</param>
/// <remarks>
/// This code demonstrates various aspects of caching. It is not intended to be 
/// an example of a useful method.
/// </remarks>
private void CachePropertiesByActivate(AutomationElement elementList)
{
    AutomationElement elementListItem;

    // Set up the request.
    CacheRequest cacheRequest = new CacheRequest();
    cacheRequest.Add(AutomationElement.NameProperty);
    cacheRequest.Add(AutomationElement.IsEnabledProperty);
    cacheRequest.Add(SelectionItemPattern.Pattern);
    cacheRequest.Add(SelectionItemPattern.SelectionContainerProperty);

    // Obtain an element and cache the requested items.
    using (cacheRequest.Activate())
    {
        Condition cond = new PropertyCondition(AutomationElement.IsSelectionItemPatternAvailableProperty, true);
        elementListItem = elementList.FindFirst(TreeScope.Children, cond);
    }
    // The CacheRequest is now inactive.

    // Retrieve the cached property and pattern.
    SelectionItemPattern pattern;
    String itemName;
    try
    {
        itemName = elementListItem.Cached.Name;
        pattern = elementListItem.GetCachedPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
    }
    catch (InvalidOperationException)
    {
        Console.WriteLine("Object was not in cache.");
        return;
    }
    // Alternatively, you can use TryGetCachedPattern to retrieve the cached pattern.
    object cachedPattern;
    if (true == elementListItem.TryGetCachedPattern(SelectionItemPattern.Pattern, out cachedPattern))
    {
        pattern = cachedPattern as SelectionItemPattern;
    }

    // Specified pattern properties are also in the cache.
    AutomationElement parentList = pattern.Cached.SelectionContainer;

    // The following line will raise an exception, because the HelpText property was not cached.
    /*** String itemHelp = elementListItem.Cached.HelpText; ***/

    // Similarly, pattern properties that were not specified in the CacheRequest cannot be 
    // retrieved from the cache. This would raise an exception.
    /*** bool selected = pattern.Cached.IsSelected; ***/

    // This is still a valid call, even though the property is in the cache.
    // Of course, the cached value and the current value are not guaranteed to be the same.
    itemName = elementListItem.Current.Name;
}

다음 코드 예제에서는 CacheRequest를 활성화하기 위해 Push를 사용함으로써 캐시의 다양한 측면을 보여 줍니다. 캐시 요청을 중첩하려는 경우 외에는 Activate를 사용하는 것이 좋습니다.

''' <summary>
''' Caches and retrieves properties for a list item by using CacheRequest.Push.
''' </summary>
''' <param name="elementList">Element from which to retrieve a child element.</param>
''' <remarks>
''' This code demonstrates various aspects of caching. It is not intended to be 
''' an example of a useful method.
''' </remarks>
Private Sub CachePropertiesByPush(ByVal elementList As AutomationElement)
    ' Set up the request.
    Dim cacheRequest As New CacheRequest()

    ' Do not get a full reference to the cached objects, only to their cached properties and patterns.
    cacheRequest.AutomationElementMode = AutomationElementMode.None

    ' Cache all elements, regardless of whether they are control or content elements.
    cacheRequest.TreeFilter = Automation.RawViewCondition

    ' Property and pattern to cache.
    cacheRequest.Add(AutomationElement.NameProperty)
    cacheRequest.Add(SelectionItemPattern.Pattern)

    ' Activate the request.
    cacheRequest.Push()

    ' Obtain an element and cache the requested items.
    Dim myCondition As New PropertyCondition(AutomationElement.IsSelectionItemPatternAvailableProperty, _
        True)
    Dim elementListItem As AutomationElement = elementList.FindFirst(TreeScope.Children, myCondition)

    ' At this point, you could call another method that creates a CacheRequest and calls Push/Pop.
    ' While that method was retrieving automation elements, the CacheRequest set in this method 
    ' would not be active. 
    ' Deactivate the request.
    cacheRequest.Pop()

    ' Retrieve the cached property and pattern.
    Dim itemName As String = elementListItem.Cached.Name
    Dim pattern As SelectionItemPattern = _
        DirectCast(elementListItem.GetCachedPattern(SelectionItemPattern.Pattern), SelectionItemPattern)

    ' The following is an alternative way of retrieving the Name property.
    itemName = CStr(elementListItem.GetCachedPropertyValue(AutomationElement.NameProperty))

    ' This is yet another way, which returns AutomationElement.NotSupported if the element does
    ' not supply a value. If the second parameter is false, a default name is returned.
    Dim objName As Object = elementListItem.GetCachedPropertyValue(AutomationElement.NameProperty, True)
    If objName Is AutomationElement.NotSupported Then
        itemName = "Unknown"
    Else
        itemName = CStr(objName)
    End If
    ' The following call raises an exception, because only the cached properties are available, 
    '  as specified by cacheRequest.AutomationElementMode. If AutomationElementMode had its
    '  default value (Full), this call would be valid.
    '** bool enabled = elementListItem.Current.IsEnabled; **

End Sub 'CachePropertiesByPush
/// <summary>
/// Caches and retrieves properties for a list item by using CacheRequest.Push.
/// </summary>
/// <param name="autoElement">Element from which to retrieve a child element.</param>
/// <remarks>
/// This code demonstrates various aspects of caching. It is not intended to be 
/// an example of a useful method.
/// </remarks>
private void CachePropertiesByPush(AutomationElement elementList)
{
    // Set up the request.
    CacheRequest cacheRequest = new CacheRequest();

    // Do not get a full reference to the cached objects, only to their cached properties and patterns.
    cacheRequest.AutomationElementMode = AutomationElementMode.None;

    // Cache all elements, regardless of whether they are control or content elements.
    cacheRequest.TreeFilter = Automation.RawViewCondition;

    // Property and pattern to cache.
    cacheRequest.Add(AutomationElement.NameProperty);
    cacheRequest.Add(SelectionItemPattern.Pattern);

    // Activate the request.
    cacheRequest.Push();

    // Obtain an element and cache the requested items.
    Condition cond = new PropertyCondition(AutomationElement.IsSelectionItemPatternAvailableProperty, true);
    AutomationElement elementListItem = elementList.FindFirst(TreeScope.Children, cond);

    // At this point, you could call another method that creates a CacheRequest and calls Push/Pop.
    // While that method was retrieving automation elements, the CacheRequest set in this method 
    // would not be active. 

    // Deactivate the request.
    cacheRequest.Pop();

    // Retrieve the cached property and pattern.
    String itemName = elementListItem.Cached.Name;
    SelectionItemPattern pattern = elementListItem.GetCachedPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;

    // The following is an alternative way of retrieving the Name property.
    itemName = elementListItem.GetCachedPropertyValue(AutomationElement.NameProperty) as String;

    // This is yet another way, which returns AutomationElement.NotSupported if the element does
    // not supply a value. If the second parameter is false, a default name is returned.
    object objName = elementListItem.GetCachedPropertyValue(AutomationElement.NameProperty, true);
    if (objName == AutomationElement.NotSupported)
    {
        itemName = "Unknown";
    }
    else
    {
        itemName = objName as String;
    }

    // The following call raises an exception, because only the cached properties are available, 
    //  as specified by cacheRequest.AutomationElementMode. If AutomationElementMode had its
    //  default value (Full), this call would be valid.
    /*** bool enabled = elementListItem.Current.IsEnabled; ***/
}

참고 항목

개념

UI 자동화 클라이언트의 캐싱