TextPointer.GetNextContextPosition(LogicalDirection) Méthode

Définition

Retourne un pointeur vers le symbole suivant 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

Une des LogicalDirection valeurs qui spécifie la direction logique dans laquelle rechercher le symbole suivant.

Retours

Vers TextPointer le symbole suivant dans la direction demandée, ou null si le début ou la fin du contenu est en cours TextPointer .

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 objet spécifié TextElement.

Bien que l’exemple puisse être utilisé pour extraire une structure XAML pour le contenu d’un élément donné TextElement, il est destiné uniquement à des fins d’illustration et ne doit pas être utilisé dans le code de production. Consultez l’espace System.Xml de noms pour un ensemble complet de types conçus pour l’utilisation et le 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 ouvrante ou fermante pour un élément TextElement.

  • Élément UIElement contenu dans un InlineUIContainer ou BlockUIContainer. Notez qu’une telle UIElement est toujours comptée comme un symbole exactement unique ; tout contenu ou élément supplémentaire contenu par l'UIElement n’est pas comptabilisé comme symbole.

  • 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 au 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 à