WindowsRuntimeStreamExtensions.AsOutputStream(Stream) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
重要
此 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
要轉換的資料流。
傳回
代表已轉換之資料流的 Windows 執行階段 IOutputStream 物件。
- 屬性
例外狀況
stream
為 null
。
資料流不支援讀取。
範例
下列範例示範如何使用 AsInputStream 和 AsOutputStream
> 方法,將 Managed 數據流轉換成 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# 程式設計手冊 (的擴充方法) 。