使用 UI 自動化取得核取方塊的切換狀態
![]() |
---|
這份文件適用於想要使用 System.Windows.Automation 命名空間中定義之 Managed UI Automation 類別的 .NET Framework 開發人員。如需 UI Automation 的最新資訊,請參閱 Windows Automation API:使用者介面自動化 (英文)。 |
本主題說明如何使用 Microsoft UI Automation來切換控制項的狀態。
範例
這個範例會使用 AutomationElement 類別的 GetCurrentPattern 方法,以便從控制項取得 TogglePattern 物件並傳回其 ToggleState 屬性。
''' <summary>
''' Gets the toggle state of an element in the target application.
''' </summary>
''' <param name="element">The target element.</param>
Private Function IsElementToggledOn(ByVal element As AutomationElement) As Boolean
If element Is Nothing Then
' TODO: Invalid parameter error handling.
Return False
End If
Dim objPattern As Object = Nothing
Dim togPattern As TogglePattern
If True = element.TryGetCurrentPattern(TogglePattern.Pattern, objPattern) Then
togPattern = TryCast(objPattern, TogglePattern)
Return togPattern.Current.ToggleState = ToggleState.On
End If
' TODO: Object doesn't support TogglePattern error handling.
Return False
End Function
/// <summary>
/// Gets the toggle state of an element in the target application.
/// </summary>
/// <param name="element">The target element.</param>
private bool IsElementToggledOn(AutomationElement element)
{
if (element == null)
{
// TODO: Invalid parameter error handling.
return false;
}
Object objPattern;
TogglePattern togPattern;
if (true == element.TryGetCurrentPattern(TogglePattern.Pattern, out objPattern))
{
togPattern = objPattern as TogglePattern;
return togPattern.Current.ToggleState == ToggleState.On;
}
// TODO: Object doesn't support TogglePattern error handling.
return false;
}