TextPointer.GetNextContextPosition(LogicalDirection) Metod

Definition

Returnerar en pekare till nästa symbol i den angivna logiska riktningen.

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

Parametrar

direction
LogicalDirection

Ett av de LogicalDirection värden som anger den logiska riktning som du vill söka efter nästa symbol i.

Returer

A TextPointer till nästa symbol i den begärda riktningen, eller null om den aktuella TextPointer kantlinjen är början eller slutet av innehållet.

Exempel

I följande exempel visas en användning för den här metoden. I exemplet används GetNextContextPosition metoden tillsammans med GetPointerContext metoden för att bläddra igenom och extrahera symbolerna i en angiven TextElement.

Även om exemplet kan användas för att extrahera en XAML-struktur för innehållet i en viss TextElement, är den endast avsedd för illustrativa ändamål och bör inte användas i produktionskod. System.Xml Se namnområdet för en omfattande uppsättning typer som är utformade för att arbeta med och bearbeta 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.

Kommentarer

Något av följande anses vara en symbol:

Om nästa symbol kategoriseras som EmbeddedElement, ElementStarteller ElementEnd (som identifieras av GetPointerContext metoden), avanceras den TextPointer som returneras av den här metoden med exakt en symbol från den aktuella positionen.

Om nästa symbol kategoriseras som Text, avanceras den TextPointer som returneras av den här metoden förbi texten till nästa symbol som inte är text (det vill: nästa position där TextPointerContext inte Textär ). Det exakta antalet symboler som korsas kan beräknas i förväg genom att anropa GetTextRunLength metoden.

Gäller för