Share via


TextPointer.GetTextInRun 方法

定義

傳回與目前 TextPointer 相鄰的文字。

多載

GetTextInRun(LogicalDirection)

傳回一個字串,其中包含與指定之邏輯方向中目前 TextPointer 相鄰的任何文字。

GetTextInRun(LogicalDirection, Char[], Int32, Int32)

將指定的最大字元數從指定方向中的任何相鄰文字複製到呼叫者提供的字元陣列中。

GetTextInRun(LogicalDirection)

傳回一個字串,其中包含與指定之邏輯方向中目前 TextPointer 相鄰的任何文字。

public:
 System::String ^ GetTextInRun(System::Windows::Documents::LogicalDirection direction);
public string GetTextInRun (System.Windows.Documents.LogicalDirection direction);
member this.GetTextInRun : System.Windows.Documents.LogicalDirection -> string
Public Function GetTextInRun (direction As LogicalDirection) As String

參數

direction
LogicalDirection

其中一個 LogicalDirection 值,這個值會指定將在哪一個邏輯方向尋找並傳回任何相鄰文字。

傳回

包含指定之邏輯方向中任何相鄰文字的字串,或為 Empty (如果找不到相鄰文字的話)。

範例

下列範例示範這個方法的用法。 此範例會 GetTextInRun 使用 方法來實作簡單的文字擷取器。 方法會傳回兩個指定 TextPointer 實例之間所有文字的字串串連。

雖然此範例可用來擷取兩 TextPointer 個實例之間的任何文字,但僅供說明之用,不應用於生產程式碼。 請改用 TextRange.Text 屬性。

// Returns a string containing the text content between two specified TextPointers.
string GetTextBetweenTextPointers(TextPointer start, TextPointer end)
{
    StringBuilder buffer = new StringBuilder();
 
    while (start != null && start.CompareTo(end) < 0)
    {
        if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
            buffer.Append(start.GetTextInRun(LogicalDirection.Forward));
 
        // Note that when the TextPointer points into a text run, this skips over the entire
        // run, not just the current character in the run.
        start = start.GetNextContextPosition(LogicalDirection.Forward);
    }
    return buffer.ToString();
} // End GetTextBetweenPointers.
' Returns a string containing the text content between two specified TextPointers.
Private Function GetTextBetweenTextPointers(ByVal start As TextPointer, ByVal [end] As TextPointer) As String
    Dim buffer As New StringBuilder()

    Do While start IsNot Nothing AndAlso start.CompareTo([end]) < 0
        If start.GetPointerContext(LogicalDirection.Forward) = TextPointerContext.Text Then
            buffer.Append(start.GetTextInRun(LogicalDirection.Forward))
        End If

        ' Note that when the TextPointer points into a text run, this skips over the entire
        ' run, not just the current character in the run.
        start = start.GetNextContextPosition(LogicalDirection.Forward)
    Loop
    Return buffer.ToString()

End Function ' End GetTextBetweenPointers.

備註

這個方法只會傳回不中斷的文字執行。 如果 以外的 Text 任何符號類型與指定方向的目前 TextPointer 相鄰,則不會傳回任何專案。 同樣地,文字只會傳回至下一個非文字元號。

另請參閱

適用於

GetTextInRun(LogicalDirection, Char[], Int32, Int32)

將指定的最大字元數從指定方向中的任何相鄰文字複製到呼叫者提供的字元陣列中。

public:
 int GetTextInRun(System::Windows::Documents::LogicalDirection direction, cli::array <char> ^ textBuffer, int startIndex, int count);
public int GetTextInRun (System.Windows.Documents.LogicalDirection direction, char[] textBuffer, int startIndex, int count);
member this.GetTextInRun : System.Windows.Documents.LogicalDirection * char[] * int * int -> int
Public Function GetTextInRun (direction As LogicalDirection, textBuffer As Char(), startIndex As Integer, count As Integer) As Integer

參數

direction
LogicalDirection

其中一個 LogicalDirection 值,這個值會指定將在哪一個邏輯方向尋找並複製任何相鄰文字。

textBuffer
Char[]

任何文字都會複製到其中的緩衝區。

startIndex
Int32

開始寫入所複製文字的 textBuffer 索引。

count
Int32

要複製的最大字元數。

傳回

實際複製到 textBuffer 中的字元數。

例外狀況

startIndex 小於 0 或大於 textBufferLength 屬性。

-或-

count小於 0 或大於 (textBufferLength 中的 textBuffer 剩餘空間。減去 startIndex) 。

備註

這個方法只會傳回不中斷的文字執行。 如果 以外的 Text 任何符號類型與指定方向的目前 TextPointer 相鄰,則不會傳回任何專案。 同樣地,文字只會傳回至下一個非文字元號。

另請參閱

適用於