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這是指定搜尋最近插入位置的邏輯方向的值之一。

傳回

A 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。 請注意,字母 和 b 之間有兩個插入位置a——一個位於結束Bold標籤之前,另一個緊接在結束Bold標籤之後。 若 GetInsertionPosition 在 a 上被調到TextPointer字母正下方a、關閉Bold標籤前的位置,且 a direction 為 ,Forward則回傳TextPointer會調整指向關閉標籤後Bold字母 之前的位置b。 在相反邏輯方向工作時,開啟格式標籤也會做類似調整。 此方法旨在提供類似情況下插入位置間的歧義。

當涉及一連串結構標籤時,此方法也可用於選擇插入點。 例如,當位於結尾與開頭段落標籤之間時,方向參數可用來選擇下一段開頭(指定 LogicalDirection.Forward)或前一段末尾(指定 LogicalDirection.Backward)最近的插入點。

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

有可能相對於由 指向的位置 TextPointer,沒有有效的插入位置存在。 如果參考內容結構不完整,例如空表格或清單,就可能發生這種情況。 在這種情況下,這個方法會將 a TextPointer 返回到與 TextPointer 該方法相同的位置。 此方法總是回傳有效的 TextPointer

適用於

另請參閱