CacheRequest.Activate Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Définit ce CacheRequest comme spécification active pour les éléments qui sont retournés lorsqu'un AutomationElement est demandé sur le même thread.
public:
IDisposable ^ Activate();
public IDisposable Activate ();
member this.Activate : unit -> IDisposable
Public Function Activate () As IDisposable
Retours
Objet qui peut être utilisé pour supprimer CacheRequest.
Exemples
L’exemple suivant montre comment utiliser Activate pour mettre en cache des modèles et des propriétés.
/// <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;
}
''' <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
Remarques
L’utilisation de cette méthode est généralement préférable à l’utilisation Push et Pop comme moyen d’activer le CacheRequest. L’objet est poussé sur la pile quand Activate il est appelé, puis décollé lorsqu’il est supprimé. Pour garantir l’élimination, placez la valeur de retour dans un using
bloc (Using
dans Visual Basic).