有没有办法到的System.IO.Stream转换为Windows.Storage.Streams.IRandomAccessStream?没有办法、转换为、IO、System

2023-09-02 10:43:41 作者:毀滅 world

在Windows 8中;我想一个MemoryStream的内容传递给接受型Windows.Storage.Streams.IRandomAccessStream的参数的类。有什么办法来此的MemoryStream转换为IRandomAccessStream?

解决方案

要使用的扩展名:必须添加使用System.IO

c system.io.stream 读取html文件,C 中IO简单操作 获取文件详情

在Windows8中,.NET和WinRT的类型通常是转换为/从引擎盖下兼容的类型,所以你不必在意它。

对于流,但是,也有辅助的方法来的WinRT和.NET流之间转换: 对于来自WinRT的流转换 - > .NET流:

  InMemoryRandomAccessStream win8Stream =的GetData(); //从某处获取的数据流。
的System.IO.Stream的InputStream = win8Stream.AsStream()
 

有关转换从.NET流 - > WinRT的流:

  Windows.Storage.Streams.IInputStream inStream中= stream.AsInputStream();
Windows.Storage.Streams.IOutputStream outStream = stream.AsOutputStream();
 

更新:2013年9月1日

让这岂不是说,微软不听它的开发者社区;)

在announcement对于.NET FX 4.5.1 ,微软表示:

  

很多你一直想要的方式为.NET流转换为Windows运行时IRandomAccessStream的。就让我们把它叫做AsRandomAccessStream扩展方法。我们没能得到这个功能到Windows 8,但它是我们的第一次补充到Windows 8.1 preVIEW之一。的

     

现在,您可以编写以下code,下载一个图像HttpClient的,则将其放入一个BitmapImage的,然后设置为源将XAML图像控件。的

通过网络I / O

  //获取图像
    VAR IMAGEURL =htt​​p://www.microsoft.com/global/en-us/news/publishingimages/logos/MSFT_logo_Web.jpg;
    VAR的客户=新的HttpClient();
    流流=等待client.GetStreamAsync(IMAGEURL);
    VAR memStream =新的MemoryStream();
    等待stream.CopyToAsync(memStream);
    memStream.Position = 0;
    VAR位图=新的BitmapImage();
    bitmap.SetSource(memStream.AsRandomAccessStream());
    image.Source =位图;
 

HTH。

In Windows 8; I would like to pass the contents of a MemoryStream to a class that accepts a parameter of type Windows.Storage.Streams.IRandomAccessStream. Is there any way to convert this MemoryStream to an IRandomAccessStream?

解决方案

To use the extensions: you must add "using System.IO"

In Windows8, .NET and WinRT types are generally converted to/from compatible types under the hood so you don't have to care about it.

For streams, however, there are helper methods to convert between WinRT and .NET streams: For converting from WinRT streams -> .NET streams:

InMemoryRandomAccessStream win8Stream = GetData(); // Get a data stream from somewhere.
System.IO.Stream inputStream = win8Stream.AsStream()

For converting from .NET streams -> WinRT streams:

Windows.Storage.Streams.IInputStream inStream = stream.AsInputStream();
Windows.Storage.Streams.IOutputStream outStream = stream.AsOutputStream();

UPDATE: 2013-09-01

Let it not be said that Microsoft doesn't listen to it's developer community ;)

In the announcement for .NET FX 4.5.1, Microsoft states that:

Many of you have been wanting a way to convert a .NET Stream to a Windows Runtime IRandomAccessStream. Let’s just call it an AsRandomAccessStream extension method. We weren't able to get this feature into Windows 8, but it was one of our first additions to Windows 8.1 Preview.

You can now write the following code, to download an image with HttpClient, load it in a BitmapImage and then set as the source for a Xaml Image control.

    //access image via networking i/o
    var imageUrl = "http://www.microsoft.com/global/en-us/news/publishingimages/logos/MSFT_logo_Web.jpg";
    var client = new HttpClient();
    Stream stream = await client.GetStreamAsync(imageUrl);
    var memStream = new MemoryStream();
    await stream.CopyToAsync(memStream);
    memStream.Position = 0;
    var bitmap = new BitmapImage();
    bitmap.SetSource(memStream.AsRandomAccessStream());
    image.Source = bitmap;

HTH.