Share via


TextPointer.GetNextContextPosition(LogicalDirection) Método

Definição

Retorna um ponteiro para o próximo símbolo na direção lógica especificada.

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

Parâmetros

direction
LogicalDirection

Um dos valores LogicalDirection que especifica a direção lógica na qual pesquisar o próximo símbolo.

Retornos

TextPointer

Um TextPointer para o próximo símbolo na direção solicitada ou null se o TextPointer atual limitar o início ou o fim do conteúdo.

Exemplos

O exemplo a seguir demonstra um uso para este método. O exemplo usa o GetNextContextPosition método em conjunto com o GetPointerContext método para percorrer e extrair os símbolos em um especificado TextElement.

Embora o exemplo possa ser usado para extrair uma estrutura XAML para o conteúdo de um determinado TextElement, ele destina-se apenas a fins ilustrativos e não deve ser usado no código de produção. Consulte o System.Xml namespace para obter um conjunto avançado de tipos projetados para trabalhar com e processar 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.

Comentários

Qualquer um dos seguintes é considerado um símbolo:

  • Uma marca de abertura ou fechamento para um TextElement elemento.

  • Um UIElement elemento contido em um InlineUIContainer ou BlockUIContainer. Observe que esse UIElement tipo sempre é contado como exatamente um símbolo; qualquer conteúdo adicional ou elementos contidos pelo UIElement não são contados como símbolos.

  • Um caractere Unicode de 16 bits dentro de um elemento de texto Run .

Se o próximo símbolo for categorizado como EmbeddedElement, ElementStartou ElementEnd (conforme identificado pelo GetPointerContext método), o TextPointer retornado por esse método será avançado por exatamente um símbolo da posição atual.

Se o próximo símbolo for categorizado como Text, o TextPointer retornado por esse método será avançado após o texto para o próximo símbolo não texto (ou seja, a próxima posição em que não TextPointerContext Textestá). A contagem exata de símbolos cruzada pode ser calculada com antecedência chamando o GetTextRunLength método.

Aplica-se a