RandomAccessStreamReference是WinRT中的一个类,从API文档中,可以得知,它的作用是为一个文件提供随机访问的输入或者输出流。对比发现,只要Windows.Storage.Streams空间中带个Reference的,都是称为提供某某流,相当于一个流生成类,目前除了接口定义出的IInputStreamReference、IRandomAccessStreamReference以及上诉的RandomAccessStreamReference(对IRandomAccessStreamReference的实现),就没有看见其他的了,奇怪的是没有对应的IOutputStreamRefrence。
具体来看RandomAccessStreamReference类,它提供了静态函数三个:CreateFromFile、CreateFromStream、CreateFromUri,以及一个实现了IRandomAccessStreamReference接口的OpenReadAsync。三个静态函数的意思就是使用不同的方式,将file变成输入输出流,当然,使用之前,你需要打开它,使用范例如下:
//Uri uri = new Uri("http://www.charlespetzold.com/pw6/PetzoldJersey.jpg");
Uri uri = new Uri("http://www.google.com.hk");
RandomAccessStreamReference streamRef = RandomAccessStreamReference.CreateFromUri(uri);
// Create a buffer for reading the stream
Windows.Storage.Streams.Buffer buffer = null;
// Read the entire file
using (IRandomAccessStreamWithContentType fileStream = await streamRef.OpenReadAsync())
{
buffer = new Windows.Storage.Streams.Buffer((uint)fileStream.Size);
await fileStream.ReadAsync(buffer, (uint)fileStream.Size, InputStreamOptions.None);
}
注意以上代码,Open返回的是一个IRandomAccessStreamWithContentType 接口,该接口继承了流输入输出接口和随机访问接口(你就可以把它看成一个.NET中的Stream),以及IContentTypeProvider接口,最后的这个Content有关的接口,其实里面只有一个string类型的属性,叫作ContentType,顾名思义,也就是表示你加载的这个文件流的类型是什么。看我上面的那段代码,你可以断点查看fileStream中的ContentType的字串是什么,当我们使用jpg的Uri时,它的字符串是 "image/pjpeg",若是一个google的首页网址,那么字符串是"text/html",所以,竟然它说是为文件提供流,那么也就是标志这个文件是什么类型的,仅此而已,在开发过程中,我们往往是已经知道返回的文件应该是什么类型的了。
分享到:
相关推荐
在Windows Store app中如何复用传统Win32 dll
WinRT 环境下很多API没有了,比如socket相关的WSASocket,WSAConnect,线程相关的CreateThread等等,导致很多库不能使用,比如boost,基于这些库的应用如果要移植到WinRT环境下,将不得不修改很多代码,甚至需要重新...
ComponentOne Studio for WinRT XAML 简单易学,功能全面,WinRT XAML 顶级用户界面开发控件套包; ComponentOne Studio for WinRT XAML 新增的界面呈现控件专为数据可视化、布局、输入以及更多用途而设计。源于...
在Win8和WinRT上通过POP使用电子邮件帐户.docx
WinRT Revealed, 该书详细介绍Windows Runtime原理以及开发技巧和资源。适合Windows 8开发人员参考阅读。
VLC for the Windows Store Environment, Windows Phone
ComponentOne Studio for WinRT XAML2012 v3 Powerful WinRT XAML controls for building better Windows Store apps. Powerful UI and Data Visualization Controls for the Windows Store Get UI controls ...
目前尚不支持使用继承创建自定义WinRT类,因此当前无法使用XAML创建用户界面。 先决条件 使用此板条箱至少需要Rust 1.28。 nightly货物功能可以启用其他夜间功能(例如,使用专业化功能)。 设计 所有定义都是从...
可以实现日期选择功能,有winphone,iphone等多种样式可供选择。...使用时可以在你的项目中引用DatePicker文件夹,具体用法可以参考我在http://blog.csdn.net/f10_s/article/details/10404025上写的。
c/c++ windows 通过winrt操作ble 蓝牙 #include #include #include <winrt/Windows.Foundation.h> #include <winrt/Windows.Foundation.Collections.h> #include <winrt/Windows.Devices.Bluetooth.h> #include ...
ComponentOne Studio for WinRT XAML 2013 v1 Part of: Ultimate | Enterprise Powerful WinRT XAML controls for building better Windows Store apps. Reduce Development Time Studio for WinRT XAML...
开发windows phone 8必备的书籍,权威、全面的帮助你掌握windows phone 8.
温特通知 WinRT Toast API的包装不完整已在Windows 10和8.1中测试。 在Windows 8中未经测试可能会起作用。去做: 添加对自适应内容的支持添加对动作的支持生成有效应用ID的方法已知的问题: 不适用于Windows 7。 ...
sqlite-winrt-3340100.vsix
使用Window Runtime OCR的WPF中的光学字符识别
迅捷/ WinRT 桥接到WinRT(Windows运行时)到 。例子Windows运行时的基本同步调用: import WinRTdo { RoInitialize () // ...
这是什么使Windows运行时(WinRT)的C ++“绑定”更多的C ++和更少的Microsoft的修补程序和脚本。 特别是使其与MinGW一起使用。 几乎没有任何努力(如果有的话)来保证MSVC可以使用该代码(我并不反对这个想法,但是...
白火石使用C ++ / Winrt的dx12引擎使用左手和Y轴上坐标。
桥接到WinRT(Windows运行时)到Swift。 Swift / WinRT桥接到WinRT(Windows Runtime)到Swift。 Windows运行时的示例基本同步调用:import WinRT do {RoInitialize()// ...
ComponentOne Studio for WinRT XAML 2013 v1 注册码 注册机 在 Visual Studio.NET 2012 上测试。 在 2012 V3 下测试可以使用。如果发现安装过程中有错误,建议由 2012 升级安装。