swi*_*ter 14 c# asynchronous microsoft-metro windows-runtime windows-store-apps
这是从资源文件获取Stream的正确/唯一方法吗?
Uri uri = new Uri(fullPath);
StorageFile storageFile =
await Windows.Storage.StorageFile.
GetFileFromApplicationUriAsync(uri);
IRandomAccessStreamWithContentType randomAccessStream =
await storageFile.OpenReadAsync();
IInputStream resourceStream = (IInputStream)
randomAccessStream.GetInputStreamAt(0);
Run Code Online (Sandbox Code Playgroud)
我的所有其他源(http和本地存储)都返回一个Stream对象,并且使用if-else代码使用其中一个或其他代码是很痛苦的.
我也尝试过创建一个MemoryStream,但我甚至找不到一个方法来获取字节...请帮助.
uint size = (uint)randomAccessStream.Size;
IBuffer buffer = new Windows.Storage.Streams.Buffer(size);
await randomAccessStream.ReadAsync(buffer, size,
InputStreamOptions.None);
Stream stream = new MemoryStream(buffer); // error takes byte[] not IBuffer
Run Code Online (Sandbox Code Playgroud)
从资源读取时的IInputStream.ReadAsync():http: //msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.iinputstream.readasync.aspx
而Stream.Read()和Stream.ReadAsync()看起来像这样:
http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx
和
http://msdn.microsoft.com/en-us/library/hh137813.aspx
谢谢
swi*_*ter 25
好的我找到了!
StorageFile storageFile =
await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
var randomAccessStream = await storageFile.OpenReadAsync();
Stream stream = randomAccessStream.AsStreamForRead();
Run Code Online (Sandbox Code Playgroud)
你也可以少一行:
Stream stream = await storageFile.OpenStreamForReadAsync();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14944 次 |
最近记录: |