TextPointer.GetPointerContext(LogicalDirection) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回指定邏輯方向中目前 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其中一個值表示在指定邏輯方向相鄰內容的類別。
範例
以下範例展示了此方法的應用。 本範例使用此 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