AutomationElement.GetCachedPropertyValue 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從 AutomationElement 擷取所指定屬性的已快取值。
多載
GetCachedPropertyValue(AutomationProperty) |
從這個 AutomationElement 的快取擷取所指定屬性的值。 屬性類型的適當預設值會針對目標使用者介面 (UI) 元素未明確支援的屬性傳回。 |
GetCachedPropertyValue(AutomationProperty, Boolean) |
從這個 AutomationElement 的快取擷取所指定屬性的值,選擇忽略任何預設屬性。 |
備註
GetCachedPropertyValue 從 的 AutomationElement 快取擷取指定的屬性。 若要擷取目前屬性,請呼叫 GetCurrentPropertyValue 。
GetCachedPropertyValue(AutomationProperty)
從這個 AutomationElement 的快取擷取所指定屬性的值。 屬性類型的適當預設值會針對目標使用者介面 (UI) 元素未明確支援的屬性傳回。
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
要擷取之屬性的識別碼。
傳回
包含所指定屬性之值的物件。
例外狀況
要求的屬性不在快取中。
使用者介面 (UI) AutomationElement 已不存在。
範例
下列說明如何使用這個方法來擷取快取的屬性。
/// <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
備註
如果元素本身的 UI 自動化提供者支援 屬性,則會傳回 屬性的值。 否則,會傳回使用者介面自動化所指定的預設屬性。 如需預設屬性的資訊,請參閱 的屬性 AutomationElement 識別碼欄位,例如 AcceleratorKeyProperty 。
GetCachedPropertyValue 從 AutomationElement 的快取中擷取指定的屬性。 若要擷取指定之屬性呼叫 GetCurrentPropertyValue 的目前物件。
如果先前未快取要求的屬性,這個方法會擲回例外狀況。
適用於
GetCachedPropertyValue(AutomationProperty, Boolean)
從這個 AutomationElement 的快取擷取所指定屬性的值,選擇忽略任何預設屬性。
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
要擷取之屬性的識別碼。
- ignoreDefaultValue
- Boolean
一個值,它指定若不支援指定的屬性時是否要忽略預設值。
傳回
包含所指定屬性的物件,或者若元素未提供值且 ignoreDefaultValue
為 true
,則是 NotSupported。
例外狀況
要求的屬性不在快取中。
AutomationElement 的 UI 不再存在。
範例
下列範例示範如何使用這個方法來擷取快取的屬性。
/// <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 快取擷取指定的屬性。 若要擷取目前屬性,請呼叫 GetCurrentPropertyValue 。
false
ignoreDefaultValue
傳入相當於呼叫 AutomationElement.GetCachedPropertyValue(AutomationProperty) 。
如果元素本身的 UI 自動化提供者支援 屬性,則會傳回 屬性的值。 否則,如果 ignoreDefaultValue
是 false
,則會傳回使用者介面自動化所指定的預設屬性。 如需預設屬性的資訊,請參閱 的屬性 AutomationElement 識別碼欄位,例如 AcceleratorKeyProperty 。
如果先前未快取要求的屬性,這個方法會擲回例外狀況。