共用方式為


TextRange.Save 方法

定義

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

多載

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 內的目前位置。

在儲存作業中,目前選取範圍中的內容可能會轉換成 dataFormat所指定的數據格式。

另請參閱

適用於

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

例外狀況

發生於 streamdataFormatnull時。

發生於不支援指定的數據格式時。 如果從 stream 載入的內容不符合指定的數據格式,也可能引發。

備註

preserveTextElementsfalse時,自定義 TextElement 物件會儲存為已知的 TextElement 類型。 例如,假設您建立稱為 Heading1的自訂 TextElement,其繼承自 Paragraph。 當您呼叫此方法時,將 preserveTextElements 設為 false時,Heading1 會在儲存 TextRange 時轉換成 Paragraph。 當您呼叫這個方法時,preserveTextElements 設定為 trueHeading1 會儲存而不轉換。 若要保留自訂文字元素,dataFormat 必須設定為 DataFormats.Xaml

Save(Stream, String, Boolean) 是在 .NET Framework 3.5 版中引進的。 如需詳細資訊,請參閱 版本和相依性

適用於