TextRange.Save Méthode

Définition

Enregistre la sélection actuelle au format de données spécifié dans le flux indiqué.

Surcharges

Save(Stream, String)

Enregistre la sélection actuelle au format de données spécifié dans le flux indiqué.

Save(Stream, String, Boolean)

Enregistre la sélection actuelle au format de données spécifié dans le flux indiqué, en conservant éventuellement les objets TextElement personnalisés.

Save(Stream, String)

Enregistre la sélection actuelle au format de données spécifié dans le flux indiqué.

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)

Paramètres

stream
Stream

Flux de données vide accessible en écriture dans lequel enregistrer la sélection actuelle.

dataFormat
String

Format de données auquel enregistrer la sélection actuelle. Les formats de données actuellement pris en charge sont les suivants : Rtf, Text, Xaml et XamlPackage.

Exceptions

stream ou dataFormat est null.

Le format de données spécifié n'est pas pris en charge.

- ou -

Le contenu chargé à partir de stream n'est pas au format de données indiqué.

Exemples

L'exemple suivant illustre l'utilisation de la méthode 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

Remarques

Lorsque cette méthode retourne, stream est laissé ouvert et la position actuelle à l’intérieur stream n’est pas définie.

Dans le cadre de l’opération d’enregistrement, le contenu de la sélection actuelle peut être converti au format de données spécifié par dataFormat.

Voir aussi

S’applique à

Save(Stream, String, Boolean)

Enregistre la sélection actuelle au format de données spécifié dans le flux indiqué, en conservant éventuellement les objets TextElement personnalisés.

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)

Paramètres

stream
Stream

Flux de données vide accessible en écriture dans lequel enregistrer la sélection actuelle.

dataFormat
String

Format de données auquel enregistrer la sélection actuelle. Les formats de données actuellement pris en charge sont les suivants : Rtf, Text, Xaml et XamlPackage.

preserveTextElements
Boolean

true pour conserver les objets TextElement personnalisés ; sinon, false.

Exceptions

Se produit quand stream ou dataFormat est null.

Levée lorsque le format de données spécifié n'est pas pris en charge. Peut également être levée si le contenu chargé à partir de stream n'est pas au format de données indiqué.

Remarques

Quand preserveTextElements est false, les objets personnalisés TextElement sont enregistrés en tant que types connus TextElement . Par exemple, supposons que vous créez un personnalisé TextElement appelé Heading1, qui hérite de Paragraph. Lorsque vous appelez cette méthode avec preserveTextElements la valeur définie falsesur , Heading1 est converti en un Paragraph lorsque le TextRange est enregistré. Lorsque vous appelez cette méthode avec preserveTextElements la valeur définie sur true, Heading1 est enregistré sans être converti. Pour conserver des éléments de texte personnalisés, dataFormat doit être défini sur DataFormats.Xaml.

Save(Stream, String, Boolean) est introduit dans le .NET Framework version 3.5. Pour plus d’informations, consultez Versions et dépendances.

S’applique à