TextRange.Save Método

Definição

Salva a seleção atual em um determinado fluxo de um formato de dados especificado.

Sobrecargas

Save(Stream, String)

Salva a seleção atual em um determinado fluxo de um formato de dados especificado.

Save(Stream, String, Boolean)

Salva a seleção atual em um fluxo especificado em um formato de dados especificado, com a opção de preservar objetos TextElement personalizados.

Save(Stream, String)

Salva a seleção atual em um determinado fluxo de um formato de dados especificado.

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)

Parâmetros

stream
Stream

Um fluxo gravável vazio para salvar a seleção atual.

dataFormat
String

Um formato de dados para salvar a seleção atual. Os formatos de dados com suporte no momento são Rtf, Text, Xaml e XamlPackage.

Exceções

stream ou dataFormat é null.

Não há suporte para o formato de dados especificado.

-ou

O conteúdo carregado de stream não coincide com o formato de dados especificado.

Exemplos

O exemplo a seguir demonstra o uso do método 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

Comentários

Quando esse método retorna, stream é deixado aberto e a posição atual dentro stream de é indefinida.

Como parte da operação de salvamento, o conteúdo na seleção atual pode ser convertido no formato de dados especificado por dataFormat.

Confira também

Aplica-se a

Save(Stream, String, Boolean)

Salva a seleção atual em um fluxo especificado em um formato de dados especificado, com a opção de preservar objetos TextElement personalizados.

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)

Parâmetros

stream
Stream

Um fluxo gravável vazio para salvar a seleção atual.

dataFormat
String

Um formato de dados para salvar a seleção atual. Os formatos de dados com suporte no momento são Rtf, Text, Xaml e XamlPackage.

preserveTextElements
Boolean

true para preservar os objetos TextElement personalizados; caso contrário, false.

Exceções

Ocorre quando stream ou dataFormat é null.

Ocorre quando não há suporte para o formato de dados especificado. Também poderá ser gerado se o conteúdo carregado de stream não coincidir com o formato de dados especificado.

Comentários

Quando preserveTextElements é false, os objetos personalizados TextElement são salvos como tipos conhecidos TextElement . Por exemplo, suponha que você crie um personalizado TextElement chamado Heading1, que herda de Paragraph. Quando você chama esse método com definido como falsepreserveTextElements , Heading1 é convertido em um Paragraph quando o TextRange é salvo. Quando você chama esse método com definido truecomo preserveTextElements , Heading1 é salvo sem ser convertido. Para preservar elementos de texto personalizados, dataFormat deve ser definido como DataFormats.Xaml.

Save(Stream, String, Boolean)é introduzido no .NET Framework versão 3.5. Para saber mais, confira Versões e dependências.

Aplica-se a