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