TextPointer.GetNextContextPosition(LogicalDirection) Método

Definição

Devolve um ponteiro para o símbolo seguinte 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 LogicalDirection valores que especifica a direção lógica para procurar o próximo símbolo.

Devoluções

A TextPointer para o símbolo seguinte na direção solicitada, ou null se o atual TextPointer marcar o início ou o fim do conteúdo.

Exemplos

O exemplo seguinte demonstra a utilização deste método. O exemplo utiliza o GetNextContextPosition método em conjunto com o GetPointerContext método para percorrer e extrair os símbolos num determinado TextElement.

Embora o exemplo possa ser usado para extrair uma estrutura XAML para o conteúdo de um dado TextElement, destina-se apenas a fins ilustrativos e não deve ser usado em código de produção. Veja o System.Xml namespace para um conjunto rico de tipos concebidos para trabalhar 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.

Observações

Qualquer um dos seguintes elementos é considerado um símbolo:

  • Uma etiqueta de abertura ou fecho para um TextElement elemento.

  • Um UIElement elemento contido num InlineUIContainer ou BlockUIContainer. Note-se que tal é UIElement sempre contado como exatamente um símbolo; qualquer conteúdo adicional ou elemento contido por o UIElement não é contado como símbolos.

  • Um carácter Unicode de 16 bits dentro de um elemento de texto Run .

Se o símbolo seguinte for categorizado como EmbeddedElement, , ou ElementEnd (conforme identificado pelo GetPointerContext método), então o TextPointer devolvido por este método avança exatamente um símbolo a partir ElementStartda posição atual.

Se o símbolo seguinte for categorizado como Text, então o TextPointer devolvido por este método é avançado para além do texto até ao próximo símbolo não-texto (isto é, a posição seguinte onde o TextPointerContext não Texté ). A contagem exata de símbolos cruzados pode ser calculada antecipadamente chamando o GetTextRunLength método.

Aplica-se a