AutomationElement.CachedParent 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得這個 AutomationElement 的快取父系。
public:
property System::Windows::Automation::AutomationElement ^ CachedParent { System::Windows::Automation::AutomationElement ^ get(); };
public System.Windows.Automation.AutomationElement CachedParent { get; }
member this.CachedParent : System.Windows.Automation.AutomationElement
Public ReadOnly Property CachedParent As AutomationElement
屬性值
父元素。 如果指定的專案沒有父代,則傳回值可以是 null
,例如,如果這個專案是根節點,則為 。
例外狀況
快取不到任何父項目。
AutomationElement 的 UI 不再存在。
範例
在下列範例中 Name ,會快取清單方塊及其清單專案的屬性。 清單方塊元素可在每個清單專案專案的 屬性中使用 CachedParent 。
/// <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