CacheRequest.TreeScope 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个值,该值指定是仅对子树的根进行缓存,还是也对其子级或子代进行缓存。
public:
property System::Windows::Automation::TreeScope TreeScope { System::Windows::Automation::TreeScope get(); void set(System::Windows::Automation::TreeScope value); };
public System.Windows.Automation.TreeScope TreeScope { get; set; }
member this.TreeScope : System.Windows.Automation.TreeScope with get, set
Public Property TreeScope As TreeScope
属性值
一个或多个 Element、Children、Descendants 或 Subtree。 默认值是 Element。
例外
示例
在以下示例中,列表框元素在活动且处于活动状态TreeScopeChildren时CacheRequest从父窗口元素获取。 子元素的指定属性 (,即列表项) 存储在缓存中,可以从列表框检索 CachedChildren 。
/// <summary>
/// Gets a list box element and caches the Name property of its children (the list items).
/// </summary>
/// <param name="elementMain">The UI Automation element for the parent window.</param>
void CachePropertiesWithScope(AutomationElement elementMain)
{
AutomationElement elementList;
// Set up the CacheRequest.
CacheRequest cacheRequest = new CacheRequest();
cacheRequest.Add(AutomationElement.NameProperty);
cacheRequest.TreeScope = TreeScope.Element | TreeScope.Children;
// Activate the CacheRequest and get the element. Note that the scope of the CacheRequest
// is in relation to the object being retrieved: the list box and its children are
// cached, not the main window and its children.
using (cacheRequest.Activate())
{
// Load the list element and cache the specified properties for its descendants.
Condition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.List);
elementList = elementMain.FindFirst(TreeScope.Children, cond);
}
if (elementList == null) return;
// The following illustrates that the children of the list are in the cache.
foreach (AutomationElement listItem in elementList.CachedChildren)
{
Console.WriteLine(listItem.Cached.Name);
}
// The following call raises an exception, because the IsEnabled property was not cached.
/*** Console.WriteLine(listItem.Cached.IsEnabled); ***/
// The following illustrates that because the list box itself was cached, it is now
// available as the CachedParent of each list item.
AutomationElement child = elementList.CachedChildren[0];
Console.WriteLine(child.CachedParent.Cached.Name);
}
''' <summary>
''' Gets a list box element and caches the Name property of its children (the list items).
''' </summary>
''' <param name="elementMain">The UI Automation element for the parent window.</param>
Sub CachePropertiesWithScope(ByVal elementMain As AutomationElement)
Dim elementList As AutomationElement
' Set up the CacheRequest.
Dim cacheRequest As New CacheRequest()
cacheRequest.Add(AutomationElement.NameProperty)
cacheRequest.TreeScope = TreeScope.Element Or TreeScope.Children
' Activate the CacheRequest and get the element. Note that the scope of the CacheRequest
' is in relation to the object being retrieved: the list box and its children are
' cached, not the main window and its children.
Using cacheRequest.Activate()
' Load the list element and cache the specified properties for its descendants.
Dim myCondition As New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.List)
elementList = elementMain.FindFirst(TreeScope.Children, myCondition)
If elementList Is Nothing Then
Return
End If
' The following illustrates that the children of the list are in the cache.
Dim listItem As AutomationElement
For Each listItem In elementList.CachedChildren
Console.WriteLine(listItem.Cached.Name)
Next listItem
' The following call raises an exception, because the IsEnabled property was not cached.
'** Console.WriteLine(listItem.Cached.IsEnabled) **
' The following illustrates that because the list box itself was cached, it is now
' available as the CachedParent of each list item.
Dim child As AutomationElement = elementList.CachedChildren(0)
Console.WriteLine(child.CachedParent.Cached.Name)
End Using
End Sub
注解
缓存的范围与要提取的对象或对象有关,而不是与调用的对象FindFirstFindAll有关。 请参阅示例。