1

I am trying to load images from a server asynchronously in window phone using the code below but the InMemoryRandomAccessStream brings the error

"The type or namespace name 'InMemoryRandomAccessStream' could not be found (are you missing a using directive or an assembly reference?)"

private async Task GetImageTaskAsync()
    {
        HttpClient client = new HttpClient();

        HttpResponseMessage response = await client.GetAsync("http://ts1.mm.bing.net/images/thumbnail.aspx?q=1080606601222&id=e7d54ea3862e939e6fd414b8750d86bdimage/jpeg1601204364");

        byte[] img = await response.Content.ReadAsByteArrayAsync();

        InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();

        DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0));

        writer.WriteBytes(img);

        await writer.StoreAsync();

        BitmapImage b = new BitmapImage();

        b.SetSource(randomAccessStream);
    }

my using statements are as below

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using RaveCompass.Resources;
using System.Net;
using System.Windows;
using Windows.Storage.Streams;
using System.Xml.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

Kindly assist, and if there other ways i can load images from a website as asynchronously, kindly suggest

4

2 回答 2

1

InMemoryRandomAccessStream不适用于 Windows Phone,仅适用于 Windows 8。在MSDN上查看。

您不需要InMemoryRandomAccessStream通过 URL 设置图像BitmapImage。您可以将 URL 本身设置为源。

private void GetImageTaskAsync()
{
    BitmapImage b = new BitmapImage(new Uri("http://ts1.mm.bing.net/images/thumbnail.aspx?q=1080606601222&id=e7d54ea3862e939e6fd414b8750d86bdimage/jpeg1601204364"));
}
于 2013-11-07T02:37:46.890 回答
0

请注意 Windows Phone 7 使用 Silverlight 的“WP 版本”,因此:

  1. BitmapImage 的下载是异步操作

  2. 默认情况下,Silverlight 会尝试延迟下载或渲染内容,直到必要时

  3. Silverlight 不会渲染图像,直到它被添加到可视化树中,即当它即将被显示时。

请按照将http-image 加载到 WriteableImage中提供的信息进行操作。我知道它不如使用 await 优雅,但我没有找到任何其他方法来做到这一点。

如果有人知道更好的方法,可能使用异步等待模式,请告诉我们。

于 2014-02-03T20:10:36.047 回答