TextPointer.GetPointerContext(LogicalDirection) 方法

定义

返回在指定逻辑方向上与当前 TextPointer 相邻的内容的类别指示标志。

public:
 System::Windows::Documents::TextPointerContext GetPointerContext(System::Windows::Documents::LogicalDirection direction);
public System.Windows.Documents.TextPointerContext GetPointerContext (System.Windows.Documents.LogicalDirection direction);
member this.GetPointerContext : System.Windows.Documents.LogicalDirection -> System.Windows.Documents.TextPointerContext
Public Function GetPointerContext (direction As LogicalDirection) As TextPointerContext

参数

direction
LogicalDirection

LogicalDirection 值之一,它指定确定相邻内容的类别时的逻辑方向。

返回

TextPointerContext

TextPointerContext 值之一,它指示指定逻辑方向上相邻内容的类别。

示例

以下示例演示了此方法的用法。 本示例使用该方法 GetPointerContext 实现算法,用于计算两个指定 TextPointer 位置之间开始和结束元素标记的平衡。 每个开始元素标记都计为 +1,每个结束元素标记都计为 -1。

// Calculate and return the relative balance of opening and closing element tags
// between two specified TextPointers.
int GetElementTagBalance(TextPointer start, TextPointer end)
{
    int balance = 0;
 
    while (start != null && start.CompareTo(end) < 0)
    {
        TextPointerContext forwardContext = start.GetPointerContext(LogicalDirection.Forward);
 
        if (forwardContext == TextPointerContext.ElementStart)     balance++;
        else if (forwardContext == TextPointerContext.ElementEnd)  balance--;
             
        start = start.GetNextContextPosition(LogicalDirection.Forward);
    } // End while.
 
    return balance;
} // End GetElementTagBalance
' Calculate and return the relative balance of opening and closing element tags
' between two specified TextPointers.
Private Function GetElementTagBalance(ByVal start As TextPointer, ByVal [end] As TextPointer) As Integer
    Dim balance As Integer = 0

    Do While start IsNot Nothing AndAlso start.CompareTo([end]) < 0
        Dim forwardContext As TextPointerContext = start.GetPointerContext(LogicalDirection.Forward)

        If forwardContext = TextPointerContext.ElementStart Then
            balance += 1
        ElseIf forwardContext = TextPointerContext.ElementEnd Then
            balance -= 1
        End If

        start = start.GetNextContextPosition(LogicalDirection.Forward)

    Loop ' End while.

    Return balance

End Function ' End GetElementTagBalance

适用于