问题描述
我有一个 ListView 项目,其中包含来自 http GET 请求的数据和图像.我可以在 ListView 中显示所有数据,除了图片.为了获取图像,我必须发出一个单独的 http GET 请求.我可以使用此代码显示图像:
I have a ListView item which contains datas and images from a http GET request. I can display all of data in the ListView, except the picture. For getting the image I have to make a separate http GET request. I can display an image with this code:
private async void DisplayPicture()
{
var ims = new InMemoryRandomAccessStream();
var dataWriter = new DataWriter(ims);
dataWriter.WriteBytes(App.answer.picture);
await dataWriter.StoreAsync();
ims.Seek(0);
BitmapImage bitmap = new BitmapImage();
bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bitmap.SetSource(ims);
}
但是如果我想在带有绑定的 ListView 中使用,这将不起作用.这是我尝试的代码:
But this doesn't work if I would like to use in a ListView with Binding.Here is the code what I tried:
public class BinaryToImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value != null && value is byte[])
{
var bytes = value as byte[];
var ims = new InMemoryRandomAccessStream();
var dataWriter = new DataWriter(ims);
dataWriter.WriteBytes(bytes);
//await dataWriter.StoreAsync();
ims.Seek(0);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(ims);
//var ims = new MemoryStream(bytes);
//var image = new BitmapImage();
//image.SetSource(stream);
//stream.Close();
return bitmap;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
主要问题是我从服务器获取到byte[](bytearray)中的图片,只有上面的代码才能在WP8.1上显示出来.所以我必须使用dataWriter.StoreAsync()
方法,但是如果我使用它,我必须使用async
,它必须是void.但是由于绑定,void 返回值对我不利.
The main problem is that I get the image in byte[] (bytearray) from the server, and only the above code can display it on WP8.1. So I have to use the dataWriter.StoreAsync()
method, but if I use it, I have to use async
, which must be void. But the void return value is not good for me due to the binding.
你可以看到我取消注释的原始代码,但我不能使用它,因为image.SetSource()
的输入值必须是一个RandomAccessStream.所以我不知道如何解决这个问题.
You can see the original code what I uncommented, but I cannot use it, because the input value for image.SetSource()
must be a RandomAccessStream. So I don't have any idea how I can solve this problem.
推荐答案
如果要进行绑定并使用异步方法,那么实现的一种方法是将 DataContext 设置为 任务并绑定到它的Result.Stepen Cleary 写了一篇关于此的好文章.您还可以在他的回答中找到一些有用的信息.
If you want to make binding and use asynchronous method, then one way to make it work is to set DataContext to Task and bind to its Result. Stepen Cleary wrote a nice article about that. You will also find some useful information in his answer here.
基于该答案,我构建了一个示例,我认为您可以对其进行修改以满足您的需求.编写一个 Converter 它将返回 TaskCompletionNotifier(参见上面斯蒂芬的回答):
Basing on that answer I've build a sample, which I think you can modify to fulfill your needs. Write a Converter which will return TaskCompletionNotifier (see Stephen's answer above):
public class WebPathToImage : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null) return null;
// the below class you will find in Stephen's answer mentioned above
return new TaskCompletionNotifier<BitmapImage>(GetImage((String)value));
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{ throw new NotImplementedException(); }
private async Task<BitmapImage> GetImage(string path)
{
HttpClient webCLient = new HttpClient();
var responseStream = await webCLient.GetStreamAsync(path);
var memoryStream = new MemoryStream();
await responseStream.CopyToAsync(memoryStream);
memoryStream.Position = 0;
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(memoryStream.AsRandomAccessStream());
return bitmap;
}
}
然后您可以在 XAML 中定义绑定:
then you can define binding in XAML:
<Image DataContext="{Binding ImageFromWeb, Converter={StaticResource WebPathToImage}}" Stretch="Uniform"
Source="{Binding Result}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="2"/>
当您设置 ImageFromWeb
时,一切都应该正常工作:
Everything should work when you set ImageFromWeb
:
ImageFromWeb = @"http://www.onereason.org/wp-content/uploads/2012/02/universe-300x198.jpg";
这篇关于WP 8.1 绑定来自 http 请求的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!