AutomationElement.GetCachedPropertyValue 方法

定義

AutomationElement 擷取所指定屬性的已快取值。Retrieves the cached value of the specified property from an AutomationElement.

多載

GetCachedPropertyValue(AutomationProperty)

從這個 AutomationElement 的快取擷取所指定屬性的值。Retrieves the value of the specified property from the cache of this AutomationElement. 對於目標 使用者介面 (UI)user interface (UI) 元素未明確支援的屬性,會傳回屬性類型的適當預設值。An appropriate default value for the property type is returned for properties not explicitly supported by the target 使用者介面 (UI)user interface (UI) element.

GetCachedPropertyValue(AutomationProperty, Boolean)

從這個 AutomationElement 的快取擷取所指定屬性的值,選擇忽略任何預設屬性。Retrieves the value of the specified property from the cache of this AutomationElement, optionally ignoring any default property.

備註

GetCachedPropertyValue從的快取中AutomationElement抓取指定的屬性。GetCachedPropertyValue retrieves the specified property from the cache of the AutomationElement. 若要取出目前的屬性, GetCurrentPropertyValue請呼叫。To retrieve the current property, call GetCurrentPropertyValue.

GetCachedPropertyValue(AutomationProperty)

從這個 AutomationElement 的快取擷取所指定屬性的值。Retrieves the value of the specified property from the cache of this AutomationElement. 對於目標 使用者介面 (UI)user interface (UI) 元素未明確支援的屬性,會傳回屬性類型的適當預設值。An appropriate default value for the property type is returned for properties not explicitly supported by the target 使用者介面 (UI)user interface (UI) element.

public:
 System::Object ^ GetCachedPropertyValue(System::Windows::Automation::AutomationProperty ^ property);
public object GetCachedPropertyValue (System.Windows.Automation.AutomationProperty property);
member this.GetCachedPropertyValue : System.Windows.Automation.AutomationProperty -> obj
Public Function GetCachedPropertyValue (property As AutomationProperty) As Object

參數

property
AutomationProperty

要擷取之屬性的識別碼。The identifier of the property to retrieve.

傳回

包含所指定屬性之值的物件。An object containing the value of the specified property.

例外狀況

要求的屬性不在快取中。The requested property is not in the cache.

AutomationElement使用者介面 (UI)user interface (UI) 不再存在。The 使用者介面 (UI)user interface (UI) for the AutomationElement no longer exists.

範例

以下顯示如何使用這個方法來抓取快取的屬性。The following shows how this method can be used to retrieve a cached property.

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

備註

如果元素本身的使用者介面自動化提供者支援屬性,則會傳回屬性的值。If the UI Automation provider for the element itself supports the property, the value of the property is returned. 否則,會傳回由UI 自動化UI Automation指定的預設屬性。Otherwise, a default property specified by UI 自動化UI Automation is returned. 如需預設屬性的詳細資訊,請參閱的屬性AutomationElement識別碼欄位, AcceleratorKeyProperty例如。For information on default properties, see the property identifier fields of AutomationElement, such as AcceleratorKeyProperty.

GetCachedPropertyValueAutomationElement的快取中抓取指定的屬性。GetCachedPropertyValue retrieves the specified property from the AutomationElement's cache. 取得指定屬性呼叫GetCurrentPropertyValue的目前物件。To retrieve the current object for the specified property call GetCurrentPropertyValue.

如果先前未快取要求的屬性,這個方法就會擲回例外狀況。This method throws an exception if the requested property was not previously cached.

GetCachedPropertyValue(AutomationProperty, Boolean)

從這個 AutomationElement 的快取擷取所指定屬性的值,選擇忽略任何預設屬性。Retrieves the value of the specified property from the cache of this AutomationElement, optionally ignoring any default property.

public:
 System::Object ^ GetCachedPropertyValue(System::Windows::Automation::AutomationProperty ^ property, bool ignoreDefaultValue);
public object GetCachedPropertyValue (System.Windows.Automation.AutomationProperty property, bool ignoreDefaultValue);
member this.GetCachedPropertyValue : System.Windows.Automation.AutomationProperty * bool -> obj
Public Function GetCachedPropertyValue (property As AutomationProperty, ignoreDefaultValue As Boolean) As Object

參數

property
AutomationProperty

要擷取之屬性的識別碼。The identifier of the property to retrieve.

ignoreDefaultValue
Boolean

一個值,它指定若不支援指定的屬性時是否要忽略預設值。A value that specifies whether a default value should be ignored if the specified property is not supported.

傳回

包含所指定屬性的物件,或者若元素未提供值且 ignoreDefaultValuetrue,則是 NotSupportedAn object containing the value of the specified property, or NotSupported if the element does not supply a value and ignoreDefaultValue is true.

例外狀況

要求的屬性不在快取中。The requested property is not in the cache.

AutomationElement 的 UI 不再存在。The UI for the AutomationElement no longer exists.

範例

下列範例會示範如何使用這個方法來抓取快取的屬性。The following example shows how this method can be used to retrieve a cached property.

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

備註

GetCachedPropertyValue從的AutomationElement快取中抓取指定的屬性。GetCachedPropertyValue retrieves the specified property from the cache for the AutomationElement. 若要取出目前的屬性, GetCurrentPropertyValue請呼叫。To retrieve the current property, call GetCurrentPropertyValue.

傳入相當於呼叫AutomationElement.GetCachedPropertyValue(AutomationProperty)false ignoreDefaultValuePassing false in ignoreDefaultValue is equivalent to calling AutomationElement.GetCachedPropertyValue(AutomationProperty).

如果元素本身的使用者介面自動化提供者支援屬性,則會傳回屬性的值。If the UI Automation provider for the element itself supports the property, the value of the property is returned. 否則,如果ignoreDefaultValuefalseUI 自動化UI Automation則會傳回所指定的預設屬性。Otherwise, if ignoreDefaultValue is false, a default property specified by UI 自動化UI Automation is returned. 如需預設屬性的詳細資訊,請參閱的屬性AutomationElement識別碼欄位, AcceleratorKeyProperty例如。For information on default properties, see the property identifier fields of AutomationElement, such as AcceleratorKeyProperty.

如果先前未快取要求的屬性,這個方法就會擲回例外狀況。This method throws an exception if the requested property was not previously cached.

適用於