TextRange.Save Metodo

Definizione

Salva la selezione corrente in un flusso specificato in un formato dati specificato.

Overload

Save(Stream, String)

Salva la selezione corrente in un flusso specificato in un formato dati specificato.

Save(Stream, String, Boolean)

Salva la selezione corrente in un flusso specificato in un formato dati specificato, con l’opzione per conservare gli oggetti TextElement personalizzati.

Save(Stream, String)

Salva la selezione corrente in un flusso specificato in un formato dati specificato.

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)

Parametri

stream
Stream

Flusso vuoto, scrivibile nel quale salvare la selezione corrente.

dataFormat
String

Formato dati nel quale salvare la selezione corrente. I formati dati supportati attualmente sono Rtf, Text, Xaml e XamlPackage.

Eccezioni

stream o dataFormat è null.

Il formato dati specificato non è supportato.

-oppure–

Il contenuto caricato da stream non corrisponde al formato dati specificato.

Esempio

Nell'esempio seguente viene illustrato l'uso del metodo 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

Commenti

Quando questo metodo viene restituito, stream viene lasciato aperto e la posizione corrente all'interno stream non è definita.

Nell'ambito dell'operazione di salvataggio, il contenuto nella selezione corrente può essere convertito nel formato di dati specificato da dataFormat.

Vedi anche

Si applica a

Save(Stream, String, Boolean)

Salva la selezione corrente in un flusso specificato in un formato dati specificato, con l’opzione per conservare gli oggetti TextElement personalizzati.

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)

Parametri

stream
Stream

Flusso vuoto, scrivibile nel quale salvare la selezione corrente.

dataFormat
String

Formato dati nel quale salvare la selezione corrente. I formati dati supportati attualmente sono Rtf, Text, Xaml e XamlPackage.

preserveTextElements
Boolean

true per conservare gli oggetti TextElement personalizzati; in caso contrario false.

Eccezioni

Viene generato quando stream o dataFormat è null.

Si verifica quando il formato dati specificato non è supportato. Può essere generato anche se il contenuto caricato da stream non corrisponde al formato dati specificato.

Commenti

Quando preserveTextElements è false, gli oggetti personalizzati TextElement vengono salvati come tipi noti TextElement . Si supponga, ad esempio, di creare un oggetto personalizzato TextElement denominato Heading1, che eredita da Paragraph. Quando si chiama questo metodo con preserveTextElements impostato su false, Heading1 viene convertito in un Paragraph quando viene TextRange salvato. Quando si chiama questo metodo con preserveTextElements impostato su true, Heading1 viene salvato senza essere convertito. Per mantenere gli elementi di testo personalizzati, dataFormat è necessario impostare su DataFormats.Xaml.

Save(Stream, String, Boolean) viene introdotto in .NET Framework versione 3.5. Per altre informazioni, vedere Versioni e dipendenze.

Si applica a