WindowsRuntimeStreamExtensions.AsOutputStream(Stream) 메서드

정의

중요

이 API는 CLS 규격이 아닙니다.

Windows 스토어 앱용 .NET의 관리형 스트림을 Windows 런타임의 출력 스트림으로 변환합니다.

public:
[System::Runtime::CompilerServices::Extension]
 static Windows::Storage::Streams::IOutputStream ^ AsOutputStream(System::IO::Stream ^ stream);
[System.CLSCompliant(false)]
public static Windows.Storage.Streams.IOutputStream AsOutputStream (this System.IO.Stream stream);
[<System.CLSCompliant(false)>]
static member AsOutputStream : System.IO.Stream -> Windows.Storage.Streams.IOutputStream
<Extension()>
Public Function AsOutputStream (stream As Stream) As IOutputStream

매개 변수

stream
Stream

변환할 스트림입니다.

반환

IOutputStream

변환된 스트림을 나타내는 Windows 런타임 IOutputStream 개체입니다.

특성

예외

stream이(가) null인 경우

스트림이 읽기를 지원하지 않습니다.

예제

다음 예제에서는 메서드 및 AsOutputStream> 사용하여 AsInputStream Windows 런타임 스트림에서 관리되는 스트림을 변환하는 방법을 보여 줍니다.

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);
            }
        }
    }
}
Imports Windows.Storage.Streams

Public NotInheritable Class BlankPage
    Inherits Page

    Dim ue As System.Text.UnicodeEncoding
    Dim bytesToWrite() As Byte
    Dim bytesToAdd() As Byte
    Dim totalBytes As Integer


    Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
        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
    End Sub

    Private Async Sub CreateButton_Click(sender As Object, e As RoutedEventArgs)
        Dim bytesRead(totalBytes - 1) As Byte
        Using memStream As MemoryStream = New MemoryStream(totalBytes)
            Await memStream.WriteAsync(bytesToWrite, 0, bytesToWrite.Length)

            Dim writer As DataWriter = New DataWriter(memStream.AsOutputStream())
            writer.WriteBytes(bytesToAdd)
            Await writer.StoreAsync()

            memStream.Seek(0, SeekOrigin.Begin)

            Dim reader As DataReader = New DataReader(memStream.AsInputStream())
            Await reader.LoadAsync(CType(totalBytes, UInteger))
            reader.ReadBytes(bytesRead)
            Results.Text = ue.GetString(bytesRead, 0, bytesRead.Length)
        End Using
    End Sub
End Class

다음은 이전 예제와 연결된 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# 프로그래밍 가이드)를 참조하세요.

적용 대상