TextRange.Save メソッド

定義

指定したストリームに、指定したデータ形式で現在の選択範囲を保存します。

オーバーロード

Save(Stream, String)

指定したストリームに、指定したデータ形式で現在の選択範囲を保存します。

Save(Stream, String, Boolean)

カスタム TextElement オブジェクトを保存するオプションを指定して、指定したストリームに、指定したデータ形式で現在の選択範囲を保存します。

Save(Stream, String)

指定したストリームに、指定したデータ形式で現在の選択範囲を保存します。

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)

パラメーター

stream
Stream

現在の選択範囲を保存する空の書き込み可能なストリーム。

dataFormat
String

現在の選択範囲を保存する際に使用するデータ形式。 現在サポートされているデータ形式は、RtfTextXaml、および XamlPackage です。

例外

stream または dataFormatnull です。

指定したデータ形式がサポートされていません。

または

stream から読み込まれたコンテンツが、指定したデータ形式と一致しません。

Save メソッドの使用例を次に示します。

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

注釈

このメソッドが を返すと、 stream は開いたままになり、 内 stream の現在の位置は未定義です。

保存操作の一環として、現在の選択範囲のコンテンツは、 で dataFormat指定されたデータ形式に変換できます。

こちらもご覧ください

適用対象

Save(Stream, String, Boolean)

カスタム TextElement オブジェクトを保存するオプションを指定して、指定したストリームに、指定したデータ形式で現在の選択範囲を保存します。

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)

パラメーター

stream
Stream

現在の選択範囲を保存する空の書き込み可能なストリーム。

dataFormat
String

現在の選択範囲を保存する際に使用するデータ形式。 現在サポートされているデータ形式は、RtfTextXaml、および XamlPackage です。

preserveTextElements
Boolean

カスタム TextElement オブジェクトを保存する場合は true。それ以外の場合は false

例外

stream または dataFormatnull の場合に発生します。

指定したデータ形式がサポートされていない場合に発生します。 また、stream から読み込まれたコンテンツが指定したデータ形式と一致しない場合にも発生することがあります。

注釈

が のfalse場合preserveTextElements、カスタム TextElement オブジェクトは既知TextElementの型として保存されます。 たとえば、 をParagraph継承する という名前Heading1のカスタムTextElementを作成するとします。 を に設定してこのメソッドをpreserveTextElementsfalseHeading1呼び出すと、 が保存されるときに TextRange が にParagraph変換されます。 に設定してこのメソッドをpreserveTextElementstrueHeading1呼び出すと、 は変換されずに保存されます。 カスタム テキスト要素を保持するには、 dataFormat を に設定する DataFormats.Xaml必要があります。

Save(Stream, String, Boolean)は、.NET Framework バージョン 3.5 で導入されています。 詳細については、「.NET Framework のバージョンおよび依存関係」を参照してください。

適用対象