TextRange.Load(Stream, String) Método

Definición

Carga la selección actual en un formato de datos especificado de una secuencia especificada.Loads the current selection in a specified data format from a specified stream.

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

Parámetros

stream
Stream

Secuencia legible que contiene datos para cargar en la selección actual.A readable stream that contains data to load into the current selection.

dataFormat
String

Formato de datos con el que cargar los datos.A data format to load the data as. Los formatos de datos actualmente compatibles son Rtf, Text, Xaml y XamlPackage.Currently supported data formats are Rtf, Text, Xaml, and XamlPackage.

Excepciones

Se produce cuando el valor de stream o dataFormat es null.Occurs when stream or dataFormat is null.

Se produce cuando el formato de datos especificado no es compatible.Occurs when the specified data format is unsupported. También puede producirse si el contenido cargado de stream no coincide con el formato de datos especificado.May also be raised if content loaded from stream does not match the specified data format.

Ejemplos

En el siguiente ejemplo se muestra el uso del método Load.The following example demonstrates the use of the Load method.

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

Comentarios

Si CanSeek se true para stream, el contenido se carga desde el principio del flujo hasta el final de la secuencia.If CanSeek is true for stream, content will be loaded from the beginning of the stream to the end of the stream. De lo contrario, el contenido se leerá desde el Position actual hasta el final de la secuencia.Otherwise, content will be read from the current Position to the end of the stream. Cuando este método devuelve, stream se deja abierto y la posición actual dentro de stream no está definida.When this method returns, stream is left open, and the current position within stream is undefined.

Una operación de carga reemplaza la selección actual por el contenido recién cargado.A load operation replaces the current selection with the newly loaded content.

Se aplica a

Consulte también: