UI オートメーション フラグメント プロバイダーでのナビゲーションの有効化

Note

このドキュメントは、System.Windows.Automation 名前空間で定義されているマネージド UI オートメーション クラスを使用する .NET Framework 開発者を対象としています。 UI オートメーションの最新情報については、Windows Automation API の「UI オートメーション」を参照してください。

このトピックのコード例では、フラグメント内の要素に対して UI オートメーション プロバイダーでのナビゲーションを有効にする方法を示します。

次のコード例では、リスト内のリスト項目に対して Navigate を実装しています。 親要素はリスト ボックス要素で、兄弟要素はそのリスト コレクション内の他の項目です。 このメソッドは正しくない方向の場合に null (Visual Basic ではNothing ) を返します。このケースでは、 FirstChildLastChildがこれに相当します (要素に子がないため)。

/// <summary>
/// Navigate to adjacent elements in the automation tree.
/// </summary>
/// <param name="direction">Direction to navigate.</param>
/// <returns>The element in that direction, or null.</returns>
/// <remarks>
/// parentControl is the provider for the list box.
/// parentItems is the collection of list item providers.
/// </remarks>
public IRawElementProviderFragment Navigate(NavigateDirection direction)
{
    int myIndex = parentItems.IndexOf(this);
    if (direction == NavigateDirection.Parent)
    {
        return (IRawElementProviderFragment)parentControl;
    }
    else if (direction == NavigateDirection.NextSibling)
    {
        if (myIndex < parentItems.Count - 1)
        {
            return (IRawElementProviderFragment)parentItems[myIndex + 1];
        }
        else
        {
            return null;
        }
    }
    else if (direction == NavigateDirection.PreviousSibling)
    {
        if (myIndex > 0)
        {
            return (IRawElementProviderFragment)parentItems[myIndex - 1];
        }
        else
        {
            return null;
        }
    }
    else
    {
        return null;
    }
}
''' <summary>
''' Navigate to adjacent elements in the automation tree.
''' </summary>
''' <param name="direction">Direction to navigate.</param>
''' <returns>The element in that direction, or null.</returns>
''' <remarks>
''' parentControl is the provider for the list box.
''' parentItems is the collection of list item providers.
''' </remarks>
Public Function Navigate(ByVal direction As NavigateDirection) As IRawElementProviderFragment _
    Implements IRawElementProviderFragment.Navigate

    Dim myIndex As Integer = parentItems.IndexOf(Me)
    If direction = NavigateDirection.Parent Then
        Return DirectCast(parentControl, IRawElementProviderFragment)
    ElseIf direction = NavigateDirection.NextSibling Then
        If myIndex < parentItems.Count - 1 Then
            Return DirectCast(parentItems((myIndex + 1)), IRawElementProviderFragment)
        Else
            Return Nothing
        End If
    ElseIf direction = NavigateDirection.PreviousSibling Then
        If myIndex > 0 Then
            Return DirectCast(parentItems((myIndex - 1)), IRawElementProviderFragment)
        Else
            Return Nothing
        End If
    Else
        Return Nothing
    End If

End Function 'Navigate

関連項目