Export (0) Print
Expand All

AutomationElement.TryGetCachedPattern Method

Retrieves a control pattern from the cache.

Namespace:  System.Windows.Automation
Assembly:  UIAutomationClient (in UIAutomationClient.dll)

public bool TryGetCachedPattern(
	AutomationPattern pattern,
	out Object patternObject
)

Parameters

pattern
Type: System.Windows.Automation.AutomationPattern

The identifier of the control pattern to retrieve.

patternObject
Type: System.Object

On return, contains the pattern if it is in the cache; otherwise null.

Return Value

Type: System.Boolean
true if the pattern is in the cache; false if it is not in the cache or not supported.

The following example shows how to cache and retrieve a control pattern.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft