英語で読む

次の方法で共有


WindowsRuntimeStreamExtensions.AsInputStream(Stream) メソッド

定義

重要

この API は CLS 準拠ではありません。

Windows ストア アプリ用 .NET のマネージド ストリームを Windows ランタイムの入力ストリームに変換します。

C#
[System.CLSCompliant(false)]
public static Windows.Storage.Streams.IInputStream AsInputStream (this System.IO.Stream stream);

パラメーター

stream
Stream

変換するストリーム。

戻り値

変換されたストリームを表す Windows ランタイム IInputStream オブジェクト。

属性

例外

streamnullです。

ストリームは読み取りをサポートしません。

次の例では、 メソッドと AsOutputStream メソッドを使用AsInputStreamして、Windows ランタイム内のストリームとの間でマネージド ストリームを変換する方法を示します。

C#
using System;
using System.IO;
using System.Text;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace ExampleApplication
{
    public sealed partial class BlankPage : Page
    {
        System.Text.UnicodeEncoding ue;
        byte[] bytesToWrite;
        byte[] bytesToAdd;
        int totalBytes;

        public BlankPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ue = new System.Text.UnicodeEncoding();
            bytesToWrite = ue.GetBytes("example text to write to memory stream");
            bytesToAdd = ue.GetBytes("text added through datawriter");
            totalBytes = bytesToWrite.Length + bytesToAdd.Length;
        }

        private async void CreateButton_Click(object sender, RoutedEventArgs e)
        {
            byte[] bytesRead = new byte[totalBytes];
            using (MemoryStream memStream = new MemoryStream(totalBytes))
            {
                await memStream.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);

                DataWriter writer = new DataWriter(memStream.AsOutputStream());
                writer.WriteBytes(bytesToAdd);
                await writer.StoreAsync();

                memStream.Seek(0, SeekOrigin.Begin);

                DataReader reader = new DataReader(memStream.AsInputStream());
                await reader.LoadAsync((uint)totalBytes);
                reader.ReadBytes(bytesRead);
                Results.Text = ue.GetString(bytesRead, 0, bytesRead.Length);
            }
        }
    }
}

前の例に関連付けられている XAML コードを次に示します。

XAML
<Page
    x:Class="ExampleApplication.BlankPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ExampleApplication"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Background="{StaticResource ApplicationPageBackgroundBrush}" VerticalAlignment="Center" HorizontalAlignment="Center">
        <Button Name="CreateButton" Content="Write and read with stream" Click="CreateButton_Click"></Button>
        <TextBlock Name="Results"></TextBlock>
    </StackPanel>
</Page>

注釈

注意

Visual Basic および C# では、 型 Streamの任意のオブジェクトのインスタンス メソッドとしてこのメソッドを呼び出すことができます。 インスタンス メソッド構文を使用してこのメソッドを呼び出す場合は、最初のパラメーターを省略します。 詳細については、「 拡張メソッド (Visual Basic)」 または「 拡張メソッド (C# プログラミング ガイド)」を参照してください。

適用対象

製品 バージョン
UWP 10.0