TextPointer.GetNextContextPosition(LogicalDirection) Metoda

Definicja

Zwraca wskaźnik do następnego symbolu w określonym kierunku logicznym.

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

LogicalDirection Jedna z wartości określających kierunek logiczny, w którym ma być wyszukiwany następny symbol.

Zwraca

TextPointer

Do TextPointer następnego symbolu w żądanym kierunku lub null jeśli bieżący TextPointer obramuje początek lub koniec zawartości.

Przykłady

W poniższym przykładzie pokazano użycie tej metody. W przykładzie użyto GetNextContextPosition metody w połączeniu z GetPointerContext metodą , aby przechodzić i wyodrębniać symbole w określonym TextElementobiekcie .

Chociaż przykład może służyć do wyodrębniania struktury XAML dla zawartości danego TextElementobiektu , jest przeznaczony tylko do celów ilustracyjnych i nie powinien być używany w kodzie produkcyjnym. System.Xml Zobacz przestrzeń nazw, aby uzyskać bogaty zestaw typów przeznaczonych do pracy z kodem XML i przetwarzania go.

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

Uwagi

Każdy z poniższych elementów jest uważany za symbol:

  • Tag otwierający lub zamykający TextElement dla elementu.

  • Element UIElement zawarty w elemecie InlineUIContainer lub BlockUIContainer. Należy pamiętać, że taki element UIElement jest zawsze liczone jako dokładnie jeden symbol. Każda dodatkowa zawartość lub elementy zawarte w obiekcie UIElement nie są liczone jako symbole.

  • 16-bitowy znak Unicode wewnątrz elementu tekstowego Run .

Jeśli następny symbol jest kategoryzowany jako EmbeddedElement, ElementStartlub ElementEnd (określony przez GetPointerContext metodę), TextPointer zwracany przez tę metodę jest zaawansowany przez dokładnie jeden symbol z bieżącego położenia.

Jeśli następny symbol jest skategoryzowany jako Text, zwracany TextPointer przez tę metodę jest zaawansowany obok tekstu do następnego symbolu innego niż tekst (czyli następnej pozycji, w której TextPointerContext nie Textma wartości ). Dokładna liczba skrzyżowanych symboli można obliczyć z wyprzedzeniem przez wywołanie GetTextRunLength metody .

Dotyczy