TextRange.Save Methode

Definition

Speichert die aktuelle Auswahl in einem angegebenen Stream in einem angegebenen Datenformat.

Überlädt

Save(Stream, String)

Speichert die aktuelle Auswahl in einem angegebenen Stream in einem angegebenen Datenformat.

Save(Stream, String, Boolean)

Speichert die aktuelle Auswahl in einem angegebenen Stream in einem angegebenen Datenformat. Optional können benutzerdefinierte TextElement-Objekte beibehalten werden.

Save(Stream, String)

Speichert die aktuelle Auswahl in einem angegebenen Stream in einem angegebenen Datenformat.

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)

Parameter

stream
Stream

Ein leerer, schreibbarer Stream, in dem die aktuelle Auswahl gespeichert werden soll.

dataFormat
String

Das Datenformat, in dem die aktuelle Auswahl gespeichert werden soll. Derzeit werden die Datenformate Rtf, Text, Xaml und XamlPackage unterstützt.

Ausnahmen

stream oder dataFormat ist null.

Das angegebene Datenformat wird nicht unterstützt.

– oder –

Der aus stream geladene Inhalt entspricht nicht dem angegebenen Datenformat.

Beispiele

Im folgenden Beispiel wird die Verwendung der Save-Methode gezeigt.

// 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

Hinweise

Wenn diese Methode zurückgibt, stream bleibt sie geöffnet, und die aktuelle Position in stream ist nicht definiert.

Im Rahmen des Speichervorgangs können Inhalte in der aktuellen Auswahl in das von dataFormatangegebene Datenformat konvertiert werden.

Weitere Informationen

Gilt für:

Save(Stream, String, Boolean)

Speichert die aktuelle Auswahl in einem angegebenen Stream in einem angegebenen Datenformat. Optional können benutzerdefinierte TextElement-Objekte beibehalten werden.

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)

Parameter

stream
Stream

Ein leerer, schreibbarer Stream, in dem die aktuelle Auswahl gespeichert werden soll.

dataFormat
String

Das Datenformat, in dem die aktuelle Auswahl gespeichert werden soll. Derzeit werden die Datenformate Rtf, Text, Xaml und XamlPackage unterstützt.

preserveTextElements
Boolean

true, um benutzerdefinierte TextElement-Objekte beizubehalten, andernfalls false.

Ausnahmen

Tritt auf, wenn stream oder dataFormat den Wert null hat.

Tritt auf, wenn das angegebene Datenformat nicht unterstützt wird. Wird auch möglicherweise ausgelöst, wenn aus stream geladener Inhalt für das angegebene Datenformat nicht geeignet ist.

Hinweise

Wenn preserveTextElements ist false, werden benutzerdefinierte TextElement Objekte als bekannte TextElement Typen gespeichert. Angenommen, Sie erstellen eine benutzerdefinierte TextElement namens Heading1, die von Paragrapherbt. Wenn Sie diese Methode mit preserveTextElements festgelegt auf falseaufrufen, Heading1 wird in ein Paragraph konvertiert, wenn gespeichert TextRange wird. Wenn Sie diese Methode mit preserveTextElements festgelegt auf trueaufrufen, Heading1 wird gespeichert, ohne konvertiert zu werden. Um benutzerdefinierte Textelemente beizubehalten, dataFormat muss auf DataFormats.Xamlfestgelegt werden.

Save(Stream, String, Boolean)wird in der .NET Framework Version 3.5 eingeführt. Weitere Informationen finden Sie unter Versionen und Abhängigkeiten.

Gilt für: