Share via


TextPointer.GetInsertionPosition(LogicalDirection) 方法

定義

TextPointer 傳回至在指定的邏輯方向中的最接近插入位置。

public:
 System::Windows::Documents::TextPointer ^ GetInsertionPosition(System::Windows::Documents::LogicalDirection direction);
public System.Windows.Documents.TextPointer GetInsertionPosition (System.Windows.Documents.LogicalDirection direction);
member this.GetInsertionPosition : System.Windows.Documents.LogicalDirection -> System.Windows.Documents.TextPointer
Public Function GetInsertionPosition (direction As LogicalDirection) As TextPointer

參數

direction
LogicalDirection

其中一個 LogicalDirection 值,這個值會指定將在哪一個邏輯方向搜尋最接近插入位置。

傳回

TextPointer

在指定的方向中最接近插入位置的 TextPointer

範例

此範例示範如何使用 GetInsertionPosition 方法來檢查指定的 TextElement 是否為可列印內容的空白。

// Tests to see if the specified TextElement is empty (has no printatble content).
bool IsElementEmpty(TextElement element)
{
    // Find starting and ending insertion positions in the element.
    // Inward-facing directions are used to make sure that insertion position
    // will be found correctly in case when the element may contain inline 
    // formatting elements (such as a Span or Run that contains Bold or Italic elements).
    TextPointer start = element.ContentStart.GetInsertionPosition(LogicalDirection.Forward);
    TextPointer end = element.ContentEnd.GetInsertionPosition(LogicalDirection.Backward);
     
    // The element has no printable content if its first and last insertion positions are equal.
    return start.CompareTo(end) == 0;
} // End IsEmptyElement method.
' Tests to see if the specified TextElement is empty (has no printatble content).
Private Function IsElementEmpty(ByVal element As TextElement) As Boolean
    ' Find starting and ending insertion positions in the element.
    ' Inward-facing directions are used to make sure that insertion position
    ' will be found correctly in case when the element may contain inline 
    ' formatting elements (such as a Span or Run that contains Bold or Italic elements).
    Dim start As TextPointer = element.ContentStart.GetInsertionPosition(LogicalDirection.Forward)
    Dim [end] As TextPointer = element.ContentEnd.GetInsertionPosition(LogicalDirection.Backward)

    ' The element has no printable content if its first and last insertion positions are equal.
    Return start.CompareTo([end]) = 0

End Function ' End IsEmptyElement method.

備註

插入位置是一個位置,其中可以新增新內容,而不會中斷相關聯內容的任何語意規則。 在實務上,插入位置是插入號可能放置於內容中的任何位置。 不是插入位置的有效 TextPointer 位置範例是兩個相鄰 Paragraph 標籤之間的位置 (,也就是在前一個段落的結尾標籤與下一個段落的開頭標記之間) 的位置。

TextPointer如果 已經指向有效的插入位置,但非空白格式順序的結束記號會直接遵循指定方向的位置,則 TextPointer 此方法所傳回的 會調整為指向格式順序關閉之後的插入位置。 例如,請考慮標記序列 <Bold>a</Bold>b 。 請注意,字母 a b 和 之間有兩個插入位置-一個位於結尾 Bold 標記前面,另一個緊接在結尾 Bold 標記後面。 如果在 GetInsertionPositionTextPointer 直接呼叫 至字母 a 後面和結尾 Bold 標記之前的位置,而且使用 directionForward ,則傳 TextPointer 回的 會調整為指向字母 b 之前的位置,在結尾 Bold 標記之後。 以相反邏輯方向運作時,會針對開啟格式標記進行類似的調整。 這個方法旨在提供類似案例中插入位置之間的厘清方法。

當涉及一系列結構化標籤時,這個方法也可以用來選擇性地選擇插入點。 例如,當位於結尾和開頭段落標記之間的某個位置時,方向參數可用來藉由指定 LogicalDirection.Forward LogicalDirection.Backward) 或上一個段落結尾指定) ,以選取下列段落開頭最接近的插入點 ( (。

如果指標已經在插入位置,而且指定的 direction 中沒有連續的格式標記,則傳 TextPointer 回的 會指向與呼叫 TextPointer 相同的位置。

與 所 TextPointer 指向的位置相對,可能沒有任何有效的插入位置存在。 如果參考的內容結構不完整,如空白資料表或清單中所示,就會發生這種情況。 在這種情況下,這個方法只會將 傳回 TextPointer 與呼叫這個方法的 相同位置 TextPointer 。 這個方法一律會傳回有效的 TextPointer

適用於

另請參閱