TextRange.Save 方法

定義

以指定的數據格式,將目前的選取範圍儲存至指定的數據流。

多載

名稱 Description
Save(Stream, String)

以指定的數據格式,將目前的選取範圍儲存至指定的數據流。

Save(Stream, String, Boolean)

使用保留自訂 TextElement 物件的選項,將目前的選取範圍儲存至指定的數據格式指定的數據流。

Save(Stream, String)

以指定的數據格式,將目前的選取範圍儲存至指定的數據流。

public:
 void Save(System::IO::Stream ^ stream, System::String ^ dataFormat);
public void Save(System.IO.Stream stream, string dataFormat);
member this.Save : System.IO.Stream * string -> unit
Public Sub Save (stream As Stream, dataFormat As String)

參數

stream
Stream

一個空的、可寫入的串流,用來儲存目前的選取。

dataFormat
String

一種用來儲存當前選擇的資料格式。 目前支援的資料格式有 RtfTextXamlXamlPackage、 和 。

例外狀況

streamdataFormatnull

所指定的資料格式不支援。

-或

載入的內容 stream 與指定的資料格式不符。

範例

下列範例示範 Save 方法的用法。

// This method accepts an input stream and a corresponding data format.  The method
// will attempt to load the input stream into a TextRange selection, apply Bold formatting
// to the selection, save the reformatted selection to an alternat stream, and return 
// the reformatted stream.  
Stream BoldFormatStream(Stream inputStream, string dataFormat)
{
    // A text container to read the stream into.
    FlowDocument workDoc = new FlowDocument();
    TextRange selection = new TextRange(workDoc.ContentStart, workDoc.ContentEnd);
    Stream outputStream = new MemoryStream();

    try
    {
        // Check for a valid data format, and then attempt to load the input stream
        // into the current selection.  Note that CanLoad ONLY checks whether dataFormat
        // is a currently supported data format for loading a TextRange.  It does not 
        // verify that the stream actually contains the specified format.  An exception 
        // may be raised when there is a mismatch between the specified data format and 
        // the data in the stream. 
        if (selection.CanLoad(dataFormat))
            selection.Load(inputStream, dataFormat);
    }
    catch (Exception e) { return outputStream; /* Load failure; return a null stream. */ }

    // Apply Bold formatting to the selection, if it is not empty.
    if (!selection.IsEmpty)
        selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

    // Save the formatted selection to a stream, and return the stream.
    if (selection.CanSave(dataFormat))
        selection.Save(outputStream, dataFormat);

    return outputStream;
}
' This method accepts an input stream and a corresponding data format.  The method
' will attempt to load the input stream into a TextRange selection, apply Bold formatting
' to the selection, save the reformatted selection to an alternat stream, and return 
' the reformatted stream.  
Private Function BoldFormatStream(ByVal inputStream As Stream, ByVal dataFormat As String) As Stream
    ' A text container to read the stream into.
    Dim workDoc As New FlowDocument()
    Dim selection As New TextRange(workDoc.ContentStart, workDoc.ContentEnd)
    Dim outputStream As Stream = New MemoryStream()

    Try
        ' Check for a valid data format, and then attempt to load the input stream
        ' into the current selection.  Note that CanLoad ONLY checks whether dataFormat
        ' is a currently supported data format for loading a TextRange.  It does not 
        ' verify that the stream actually contains the specified format.  An exception 
        ' may be raised when there is a mismatch between the specified data format and 
        ' the data in the stream. 
        If selection.CanLoad(dataFormat) Then
            selection.Load(inputStream, dataFormat)
        End If
    Catch e As Exception ' Load failure return a null stream. 
        Return outputStream
    End Try

    ' Apply Bold formatting to the selection, if it is not empty.
    If Not selection.IsEmpty Then
        selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold)
    End If

    ' Save the formatted selection to a stream, and return the stream.
    If selection.CanSave(dataFormat) Then
        selection.Save(outputStream, dataFormat)
    End If

    Return outputStream
End Function

備註

當此方法返回時,會 stream 保持開啟,且目前 stream 位置尚未定義。

作為儲存操作的一部分,當前選取中的內容可能會轉換成由 dataFormatS 指定的資料格式。

另請參閱

適用於

Save(Stream, String, Boolean)

使用保留自訂 TextElement 物件的選項,將目前的選取範圍儲存至指定的數據格式指定的數據流。

public:
 void Save(System::IO::Stream ^ stream, System::String ^ dataFormat, bool preserveTextElements);
public void Save(System.IO.Stream stream, string dataFormat, bool preserveTextElements);
member this.Save : System.IO.Stream * string * bool -> unit
Public Sub Save (stream As Stream, dataFormat As String, preserveTextElements As Boolean)

參數

stream
Stream

一個空的、可寫入的串流,用來儲存目前的選取。

dataFormat
String

一種用來儲存當前選擇的資料格式。 目前支援的資料格式有 RtfTextXamlXamlPackage、 和 。

preserveTextElements
Boolean

true以保存自訂TextElement物件;否則,。 false

例外狀況

streamdataFormat 發生 null時。

當指定的資料格式不支援時會發生。 若載入的內容 stream 與指定資料格式不符,也可能提高。

備註

preserveTextElementsfalse時,自訂 TextElement 物件會被儲存為已知 TextElement 型別。 例如,假設你建立一個名為 的自訂TextElement,繼承自 Heading1Paragraph 當你將此方法設定為 時,當 被儲存時,該方法preserveTextElements會轉換成 a falseHeading1ParagraphTextRange 當你將此方法 preserveTextElements 設定為 trueHeading1 ,會儲存而未被轉換。 為保留自訂文字元素, dataFormat 必須將 設為 DataFormats.Xaml

Save(Stream, String, Boolean) 於 .NET Framework 3.5 版本中引入。 欲了解更多資訊,請參閱 版本與相依關係。

適用於