TextPointer.GetNextContextPosition(LogicalDirection) 方法

定义

返回指向指定逻辑方向的下一个符号的指针。

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

参数

direction
LogicalDirection

其中 LogicalDirection 一个值指定要在其中搜索下一个符号的逻辑方向。

返回

TextPointer请求方向的下一个符号,或者null当前TextPointer是否与内容的开始或结尾边框。

示例

以下示例演示了此方法的用法。 该示例将 GetNextContextPosition 该方法与该方法结合使用 GetPointerContext 来遍历和提取指定 TextElement中的符号。

虽然该示例可用于提取给定 TextElement内容的 XAML 结构,但它仅用于说明目的,不应在生产代码中使用。 System.Xml请参阅用于处理和处理 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.

注解

以下任一项都被视为符号:

如果下一个符号被分类为 EmbeddedElementElementStartElementEnd (由 GetPointerContext 方法标识),则 TextPointer 此方法返回的符号由当前位置的一个符号进行高级。

如果下一个符号被分类为 Text,则 TextPointer 此方法返回的文本将提前到下一个非文本符号(即,下一个位置,其中 TextPointerContext 不是 Text)。 通过调用 GetTextRunLength 该方法可以提前计算交叉的确切符号计数。

适用于