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

如果要保留自定义 TextElement 对象,则为 true ;否则为 false

例外

streamdataFormatnull 时发生。

当不支持指定数据格式时发生。 如果从 stream 加载的内容与指定数据格式不匹配,也可能引发。

注解

当 为 falsepreserveTextElements,自定义TextElement对象保存为已知TextElement类型。 例如,假设创建一个名为 的Heading1自定义 TextElement ,它继承自 Paragraph。 调用此方法时,如果preserveTextElements设置为 falseHeading1则会在保存 时TextRange转换为 Paragraph 。 调用此方法时,将 preserveTextElements 设置为 trueHeading1 将保存而不进行转换。 若要保留自定义文本元素, dataFormat 必须设置为 DataFormats.Xaml

Save(Stream, String, Boolean)在 .NET Framework 版本 3.5 中引入。 有关详细信息,请参见版本和依赖关系

适用于