Share via


TextRange.Load(Stream, String) 메서드

정의

지정된 스트림에서 현재 선택 영역을 지정된 데이터 형식으로 로드합니다.

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)

매개 변수

stream
Stream

현재 선택 영역에 로드할 데이터가 있는 읽을 수 있는 스트림입니다.

dataFormat
String

데이터를 로드할 데이터 형식입니다. 현재 지원되는 데이터 형식에는 Rtf, Text, XamlXamlPackage가 있습니다.

예외

stream 또는 dataFormatnull인 경우 발생합니다.

지정된 데이터 형식이 지원되지 않는 경우. stream에서 로드한 콘텐츠가 지정된 데이터 형식과 일치하지 않을 경우에도 발생할 수 있습니다.

예제

다음 예제에서는 Load 메서드를 사용하는 방법을 보여 줍니다.

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

설명

하는 경우 CanSeek 됩니다 true 에 대 한 stream, 스트림의 끝에 콘텐츠를 스트림 시작 부분에서 로드 됩니다. 현재에서 콘텐츠를 읽을 수는 고, 그렇지 Position 스트림의 끝에 있습니다. 이 메서드는 반환 될 때 stream 열고 내의 현재 위치에 그대로 stream 정의 되지 않습니다.

로드 작업을 새로 로드 된 콘텐츠를 사용 하 여 현재 선택 영역을 바꿉니다.

적용 대상

추가 정보