Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
GetUpdatedCache Method

AutomationElement.GetUpdatedCache Method

Retrieves a new AutomationElement with an updated cache.

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

public AutomationElement GetUpdatedCache(
	CacheRequest request
)

Parameters

request
Type: System.Windows.Automation.CacheRequest

The patterns and properties to include in the updated cache.

Return Value

Type: System.Windows.Automation.AutomationElement
A new AutomationElement that has an updated cache.

The original AutomationElement is unchanged. GetUpdatedCache returns a new AutomationElement, that refers to the same user interface (UI) and has the same RuntimeIdProperty.

The following example demonstrates how a cache might be updated whenever the selection in a combo box changes.

CacheRequest comboCacheRequest;
AutomationEventHandler selectHandler;
AutomationElement elementCombo;
AutomationElement selectedItem;

/// <summary> 
/// Retrieves a combo box automation element, caches a pattern and a property, 
/// and registers the event handler. 
/// </summary> 
/// <param name="elementAppWindow">The element for the parent window.</param>
private void SetupComboElement(AutomationElement elementAppWindow)
{
    // Set up the CacheRequest.
    comboCacheRequest = new CacheRequest();
    comboCacheRequest.Add(SelectionPattern.Pattern);
    comboCacheRequest.Add(SelectionPattern.SelectionProperty);
    comboCacheRequest.Add(AutomationElement.NameProperty);
    comboCacheRequest.TreeScope = TreeScope.Element | TreeScope.Descendants;

    // Activate the CacheRequest and get the element. 
    using (comboCacheRequest.Activate())
    {
        // Load the combo box element and cache the specified properties and patterns.
        Condition propCondition = new PropertyCondition(
            AutomationElement.AutomationIdProperty, "comboBox1", PropertyConditionFlags.IgnoreCase);
        elementCombo = elementAppWindow.FindFirst(TreeScope.Descendants, propCondition);
    }

    // Get the list from the combo box.
    Condition propCondition1 = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.List);
    AutomationElement listElement = elementCombo.FindFirst(TreeScope.Children, propCondition1);

    // Register for ElementSelectedEvent on list items. 
    if (listElement != null)
    {
        Automation.AddAutomationEventHandler(SelectionItemPattern.ElementSelectedEvent, listElement,
            TreeScope.Children, selectHandler = new AutomationEventHandler(OnListItemSelect));
    }
}

/// <summary> 
/// Handle ElementSelectedEvent on items in the combo box. 
/// </summary> 
/// <param name="src">Object that raised the event.</param>
/// <param name="e">Event arguments.</param>
private void OnListItemSelect(object src, AutomationEventArgs e)
{
    // Update the cache.
    AutomationElement updatedElement = elementCombo.GetUpdatedCache(comboCacheRequest);

    // Retrieve the pattern and the selected item from the cache. This code is here only to  
    // demonstrate that the current selection can now be retrieved from the cache. In an application, 
    // this would be done only when the information was needed.
    SelectionPattern pattern = updatedElement.GetCachedPattern(SelectionPattern.Pattern) as SelectionPattern;
    AutomationElement[] selectedItems = pattern.Cached.GetSelection();
    selectedItem = selectedItems[0];
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft