Bagikan melalui


TextRange.Load(Stream, String) Metode

Definisi

Memuat pilihan saat ini dalam format data tertentu dari aliran tertentu.

public:
 void Load(System::IO::Stream ^ stream, System::String ^ dataFormat);
public void Load (System.IO.Stream stream, string dataFormat);
member this.Load : System.IO.Stream * string -> unit
Public Sub Load (stream As Stream, dataFormat As String)

Parameter

stream
Stream

Aliran yang dapat dibaca yang berisi data untuk dimuat ke dalam pilihan saat ini.

dataFormat
String

Format data untuk memuat data sebagai. Format data yang saat ini didukung adalah Rtf, , TextXaml, dan XamlPackage.

Pengecualian

Terjadi ketika stream atau dataFormat adalah null.

Terjadi ketika format data yang ditentukan tidak didukung. Dapat juga dinaikkan jika konten yang dimuat dari stream tidak cocok dengan format data yang ditentukan.

Contoh

Contoh berikut menunjukkan penggunaan Load metode .

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

Keterangan

Jika CanSeek adalah true untuk stream, konten akan dimuat dari awal aliran ke akhir aliran. Jika tidak, konten akan dibaca dari saat ini Position ke akhir aliran. Ketika metode ini kembali, stream dibiarkan terbuka, dan posisi saat ini di dalam stream tidak terdefinisi.

Operasi pemuatan menggantikan pilihan saat ini dengan konten yang baru dimuat.

Berlaku untuk

Lihat juga