TextRange(TextPointer, TextPointer) 建構函式

定義

初始化 TextRange 類別的新實例,採用兩個指定的 TextPointer 位置作為新範圍的開始和結束位置。

public:
 TextRange(System::Windows::Documents::TextPointer ^ position1, System::Windows::Documents::TextPointer ^ position2);
public TextRange(System.Windows.Documents.TextPointer position1, System.Windows.Documents.TextPointer position2);
new System.Windows.Documents.TextRange : System.Windows.Documents.TextPointer * System.Windows.Documents.TextPointer -> System.Windows.Documents.TextRange
Public Sub New (position1 As TextPointer, position2 As TextPointer)

參數

position1
TextPointer

一個固定的錨點位置,標示了形成新 TextRange的選擇的一端。

position2
TextPointer

一個可移動的位置,標記用來形成新 TextRange的選擇的另一端。

例外狀況

position1position2 不在同一份文件中時會發生。

position1position2 發生 null時。

範例

以下範例示範了建構子的 TextRange 使用方式。

// This method returns a plain text representation of a specified FlowDocument.
string GetTextFromFlowDocument(FlowDocument flowDoc)
{
     // Create a new TextRanage that takes the entire FlowDocument as the current selection.
     TextRange flowDocSelection = new TextRange(flowDoc.ContentStart, flowDoc.ContentEnd);
      
     // Use the Text property to extract a string that contains the unformatted text contents 
     // of the FlowDocument.
     return flowDocSelection.Text;
}
' This method returns a plain text representation of a specified FlowDocument.
Private Function GetTextFromFlowDocument(ByVal flowDoc As FlowDocument) As String
    ' Create a new TextRanage that takes the entire FlowDocument as the current selection.
    Dim flowDocSelection As New TextRange(flowDoc.ContentStart, flowDoc.ContentEnd)

    ' Use the Text property to extract a string that contains the unformatted text contents 
    ' of the FlowDocument.
    Return flowDocSelection.Text
End Function

備註

A TextRange 是由 TextPointers 指示的兩個位置之間的選擇所組成。 其中一個位置(以 position1表示)相對於選擇是固定的,而另一個位置(以 position2表示)則是可移動的。 這與使用滑鼠或鍵盤的使用者所做的選取方式類似。

新文件 TextRange 的實際端點可調整以符合包含新文件 TextRange的任何選擇啟發式。

適用於