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

若要保留自定义 对象,则为 ;否则为 >。

例外

在或为streamdataFormatnull发生 。

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

注解

如果 preserveTextElementsfalse,自定义 TextElement 对象将保存为已知 TextElement 类型。 例如,假设你创建了一个从中继承的TextElement自定义Heading1调用Paragraph。 调用设置为 &a0> 的方法时,将转换为保存时 调用设置为 <a0/&a0> 的方法时,将保存而不进行转换。 若要保留自定义文本元素, dataFormat 必须设置为 DataFormats.Xaml

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

适用于