Share via


TextRange.Save Metoda

Definice

Uloží aktuální výběr do zadaného datového proudu v zadaném formátu dat.

Přetížení

Save(Stream, String)

Uloží aktuální výběr do zadaného datového proudu v zadaném formátu dat.

Save(Stream, String, Boolean)

Uloží aktuální výběr do zadaného datového proudu v zadaném formátu dat s možností zachování vlastních TextElement objektů.

Save(Stream, String)

Uloží aktuální výběr do zadaného datového proudu v zadaném formátu dat.

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)

Parametry

stream
Stream

Prázdný datový proud s možností zápisu, do které se uloží aktuální výběr.

dataFormat
String

Formát dat pro uložení aktuálního výběru. Aktuálně podporované formáty dat jsou Rtf, Text, Xamla XamlPackage.

Výjimky

stream nebo dataFormat je null.

Zadaný formát dat není podporován.

-Nebo

Obsah načtený z stream neodpovídá zadanému formátu dat.

Příklady

Následující příklad ukazuje použití Save metody .

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

Poznámky

Když tato metoda vrátí, stream je ponechána otevřená a aktuální pozice uvnitř stream není definována.

V rámci operace uložení může být obsah aktuálního výběru převeden do formátu dat určeného parametrem dataFormat.

Viz také

Platí pro

Save(Stream, String, Boolean)

Uloží aktuální výběr do zadaného datového proudu v zadaném formátu dat s možností zachování vlastních TextElement objektů.

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)

Parametry

stream
Stream

Prázdný datový proud s možností zápisu, do které se uloží aktuální výběr.

dataFormat
String

Formát dat pro uložení aktuálního výběru. Aktuálně podporované formáty dat jsou Rtf, Text, Xamla XamlPackage.

preserveTextElements
Boolean

true pro zachování vlastních TextElement objektů, falsejinak .

Výjimky

Vyvolá se, když stream je nullnebo dataFormat .

Vyvolá se v případě, že zadaný formát dat není podporován. Může také být vyvolána, pokud obsah načtený z stream neodpovídá zadanému formátu dat.

Poznámky

Pokud preserveTextElements je false, vlastní TextElement objekty se ukládají jako známé TextElement typy. Předpokládejme například, že vytvoříte vlastní TextElement s názvem Heading1, který dědí z Paragraph. Při volání této metody s preserveTextElements nastaveným na falseHeading1 , se při uložení převede na ParagraphTextRange . Při volání této metody s nastaven na preserveTextElementstrue, Heading1 je uložena bez převodu. Chcete-li zachovat vlastní textové prvky, dataFormat musí být nastavena na DataFormats.Xamlhodnotu .

Save(Stream, String, Boolean) je zaveden v rozhraní .NET Framework verze 3.5. Další informace najdete v tématu Verze a závislosti.

Platí pro