Partager via


TextPointer.GetNextContextPosition(LogicalDirection) Méthode

Définition

Renvoie un pointeur vers le prochain symbole dans la direction logique spécifiée.

public:
 System::Windows::Documents::TextPointer ^ GetNextContextPosition(System::Windows::Documents::LogicalDirection direction);
public System.Windows.Documents.TextPointer GetNextContextPosition (System.Windows.Documents.LogicalDirection direction);
member this.GetNextContextPosition : System.Windows.Documents.LogicalDirection -> System.Windows.Documents.TextPointer
Public Function GetNextContextPosition (direction As LogicalDirection) As TextPointer

Paramètres

direction
LogicalDirection

L'une des valeurs LogicalDirection qui spécifient la direction logique dans laquelle rechercher le prochain symbole.

Retours

TextPointer

Un TextPointer au prochain symbole dans la direction requise, ou null si le TextPointer actuel borde le début ou la fin du contenu.

Exemples

L’exemple suivant illustre une utilisation pour cette méthode. L’exemple utilise la GetNextContextPosition méthode conjointement avec la GetPointerContext méthode pour parcourir et extraire les symboles dans un spécifié TextElement.

Bien que l’exemple puisse être utilisé pour extraire une structure XAML pour le contenu d’un donné TextElement, il est destiné uniquement à des fins d’illustration et ne doit pas être utilisé dans le code de production. Consultez l’espace de noms pour un ensemble complet de types conçus pour l’utilisation et le System.Xml traitement xml.

// This method will extract and return a string that contains a representation of 
// the XAML structure of content elements in a given TextElement.        
string GetXaml(TextElement element)
{
    StringBuilder buffer = new StringBuilder();
 
    // Position a "navigator" pointer before the opening tag of the element.
    TextPointer navigator = element.ElementStart;

    while (navigator.CompareTo(element.ElementEnd) < 0)
    {
        switch (navigator.GetPointerContext(LogicalDirection.Forward))
        {
            case TextPointerContext.ElementStart : 
                // Output opening tag of a TextElement
                buffer.AppendFormat("<{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
                break;
            case TextPointerContext.ElementEnd :
                // Output closing tag of a TextElement
                buffer.AppendFormat("</{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
                break;
            case TextPointerContext.EmbeddedElement :
                // Output simple tag for embedded element
                buffer.AppendFormat("<{0}/>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
                break;
            case TextPointerContext.Text :
                // Output the text content of this text run
                buffer.Append(navigator.GetTextInRun(LogicalDirection.Forward));
                break;
        }
 
        // Advance the naviagtor to the next context position.
        navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
    } // End while.

    return buffer.ToString();
} // End GetXaml method.
' This method will extract and return a string that contains a representation of 
' the XAML structure of content elements in a given TextElement.        
Private Function GetXaml_Renamed(ByVal element As TextElement) As String
    Dim buffer As New StringBuilder()

    ' Position a "navigator" pointer before the opening tag of the element.
    Dim navigator As TextPointer = element.ElementStart

    Do While navigator.CompareTo(element.ElementEnd) < 0
        Select Case navigator.GetPointerContext(LogicalDirection.Forward)
            Case TextPointerContext.ElementStart
                ' Output opening tag of a TextElement
                buffer.AppendFormat("<{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name)
            Case TextPointerContext.ElementEnd
                ' Output closing tag of a TextElement
                buffer.AppendFormat("</{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name)
            Case TextPointerContext.EmbeddedElement
                ' Output simple tag for embedded element
                buffer.AppendFormat("<{0}/>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name)
            Case TextPointerContext.Text
                ' Output the text content of this text run
                buffer.Append(navigator.GetTextInRun(LogicalDirection.Forward))
        End Select

        ' Advance the naviagtor to the next context position.
        navigator = navigator.GetNextContextPosition(LogicalDirection.Forward)

    Loop ' End while.

    Return buffer.ToString()

End Function ' End GetXaml method.

Remarques

L’un des éléments suivants est considéré comme un symbole :

  • Balise d’ouverture ou de fermeture pour un TextElement élément.

  • Élément UIElement contenu dans un InlineUIContainer ou BlockUIContainer. Notez qu’un UIElement tel symbole est toujours comptabilisé comme un symbole exactement ; tout contenu ou élément supplémentaire contenu par celui-ci UIElement n’est pas comptabilisé comme symboles.

  • Caractère Unicode 16 bits à l’intérieur d’un élément de texte Run .

Si le symbole suivant est classé comme EmbeddedElement, ElementStartou ElementEnd (comme identifié par la GetPointerContext méthode), le TextPointer symbole retourné par cette méthode est avancé par exactement un symbole de la position actuelle.

Si le symbole suivant est classé comme Text, le TextPointer retour de cette méthode est avancé au-delà du texte vers le symbole non-texte suivant (autrement dit, la position suivante où le TextPointerContext n’est pas Text). Le nombre exact de symboles croisés peut être calculé à l’avance en appelant la GetTextRunLength méthode.

S’applique à