TextPointer.GetNextContextPosition(LogicalDirection) Metoda

Definice

Vrátí ukazatel na další symbol v zadaném logickém směru.

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

Parametry

direction
LogicalDirection

Jedna z LogicalDirection hodnot, která určuje logický směr, ve kterém se má vyhledat další symbol.

Návraty

A TextPointer k dalšímu symbolu v požadovaném směru nebo null pokud aktuální TextPointer ohraničení začátku nebo konce obsahu.

Příklady

Následující příklad ukazuje použití pro tuto metodu. Příklad používá metodu GetNextContextPosition ve spojení s metodou GetPointerContext k procházení a extrahování symbolů v zadaném TextElement.

I když lze příklad použít k extrakci struktury XAML pro obsah daného TextElementobjektu , je určen pouze pro ilustrativní účely a neměl by být použit v produkčním kódu. Podívejte se na System.Xml obor názvů pro bohatou sadu typů navržených pro práci s XML a zpracování.

// 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.

Poznámky

Za symbol se považuje některý z následujících způsobů:

Pokud je další symbol zařazen do EmbeddedElementkategorie , ElementStartnebo ElementEnd (jak je identifikován GetPointerContext metodou), TextPointer pak vrácená touto metodou je rozšířena o přesně jeden symbol z aktuální pozice.

Pokud je další symbol zařazen do kategorie Text, pak TextPointer vrácená touto metodou je rozšířena za text na další netextový symbol (to znamená, že další pozice, kde TextPointerContext není Text). Přesný počet překřížených symbolů lze vypočítat předem voláním GetTextRunLength metody.

Platí pro