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

TextPointer

A TextPointer na další symbol 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 extrakci symbolů v zadané 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 kategorizován jako EmbeddedElement, ElementStartnebo ElementEnd (jak je identifikován GetPointerContext metodou), pak TextPointer vrácená touto metodou je upřesně o jeden symbol z aktuální pozice.

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

Platí pro