ExpandCollapsePattern.Collapse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
隱藏所有下階節點、控制項或 AutomationElement 內容。
public:
void Collapse();
public void Collapse ();
member this.Collapse : unit -> unit
Public Sub Collapse ()
例外狀況
範例
在下列範例中,代表功能表項的UI Automation元素會傳遞至函式,該函式會根據目前的 ExpandCollapseState來嘗試展開或折疊功能表項。
///--------------------------------------------------------------------
/// <summary>
/// Obtains an ExpandCollapsePattern control pattern from an
/// automation element.
/// </summary>
/// <param name="targetControl">
/// The automation element of interest.
/// </param>
/// <returns>
/// A ExpandCollapsePattern object.
/// </returns>
///--------------------------------------------------------------------
private ExpandCollapsePattern GetExpandCollapsePattern(
AutomationElement targetControl)
{
ExpandCollapsePattern expandCollapsePattern = null;
try
{
expandCollapsePattern =
targetControl.GetCurrentPattern(
ExpandCollapsePattern.Pattern)
as ExpandCollapsePattern;
}
// Object doesn't support the ExpandCollapsePattern control pattern.
catch (InvalidOperationException)
{
return null;
}
return expandCollapsePattern;
}
'''--------------------------------------------------------------------
''' <summary>
''' Obtains an ExpandCollapsePattern control pattern from an
''' automation element.
''' </summary>
''' <param name="targetControl">
''' The automation element of interest.
''' </param>
''' <returns>
''' A ExpandCollapsePattern object.
''' </returns>
'''--------------------------------------------------------------------
Private Function GetExpandCollapsePattern( _
ByVal targetControl As AutomationElement) As ExpandCollapsePattern
Dim expandCollapsePattern As ExpandCollapsePattern = Nothing
Try
expandCollapsePattern = DirectCast( _
targetControl.GetCurrentPattern(expandCollapsePattern.Pattern), _
ExpandCollapsePattern)
Catch exc As InvalidOperationException
' Object doesn't support the ExpandCollapsePattern control pattern.
Return Nothing
End Try
Return expandCollapsePattern
End Function 'GetExpandCollapsePattern
///--------------------------------------------------------------------
/// <summary>
/// Programmatically expand or collapse a menu item.
/// </summary>
/// <param name="menuItem">
/// The target menu item.
/// </param>
///--------------------------------------------------------------------
private void ExpandCollapseMenuItem(
AutomationElement menuItem)
{
if (menuItem == null)
{
throw new ArgumentNullException(
"AutomationElement argument cannot be null.");
}
ExpandCollapsePattern expandCollapsePattern =
GetExpandCollapsePattern(menuItem);
if (expandCollapsePattern == null)
{
return;
}
if (expandCollapsePattern.Current.ExpandCollapseState ==
ExpandCollapseState.LeafNode)
{
return;
}
try
{
if (expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.Expanded)
{
// Collapse the menu item.
expandCollapsePattern.Collapse();
}
else if (expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.Collapsed ||
expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.PartiallyExpanded)
{
// Expand the menu item.
expandCollapsePattern.Expand();
}
}
// Control is not enabled
catch (ElementNotEnabledException)
{
// TO DO: error handling.
}
// Control is unable to perform operation.
catch (InvalidOperationException)
{
// TO DO: error handling.
}
}
'''--------------------------------------------------------------------
''' <summary>
''' Programmatically expand or collapse a menu item.
''' </summary>
''' <param name="menuItem">
''' The target menu item.
''' </param>
'''--------------------------------------------------------------------
Private Sub ExpandCollapseMenuItem(ByVal menuItem As AutomationElement)
If menuItem Is Nothing Then
Throw New ArgumentNullException( _
"AutomationElement argument cannot be null.")
End If
Dim expandCollapsePattern As ExpandCollapsePattern = _
GetExpandCollapsePattern(menuItem)
If expandCollapsePattern Is Nothing Then
Return
End If
If expandCollapsePattern.Current.ExpandCollapseState = _
ExpandCollapseState.LeafNode Then
Return
End If
Try
If expandCollapsePattern.Current.ExpandCollapseState = _
ExpandCollapseState.Expanded Then
' Collapse the menu item.
expandCollapsePattern.Collapse()
ElseIf expandCollapsePattern.Current.ExpandCollapseState = _
ExpandCollapseState.Collapsed OrElse _
expandCollapsePattern.Current.ExpandCollapseState = _
ExpandCollapseState.PartiallyExpanded Then
' Expand the menu item.
expandCollapsePattern.Expand()
End If
Catch exc As ElementNotEnabledException
' Control is not enabled
' TO DO: error handling.
Catch exc As InvalidOperationException
' Control is unable to perform operation
' TO DO: error handling.
End Try
End Sub
備註
這是在UI自動化專案折疊之後傳回的封鎖方法。
在某些情況下,標示為分葉節點的UI自動化專案可能不知道它是否具有子系,直到 Collapse 呼叫 或 Expand 方法為止。 這種行為可以透過樹視圖控件來延遲載入其子專案。 例如,Microsoft Windows 檔案總管可能會顯示節點的展開圖示,即使目前沒有子專案也一樣;按兩下圖示時,控件會輪詢子項目、尋找無專案,並移除展開圖示。 在這些情況下,客戶端應該藉由向 方法註冊事件處理程式AddAutomationPropertyChangedEventHandler,ExpandCollapseState在屬性上接聽屬性變更的事件。