WP 8.1从http请求绑定图像 [英] WP 8.1 Binding image from a http request
问题描述
我有一个包含从HTTP GET请求DATAS和图像的ListView项。我可以在ListView中显示所有的数据,除了照片。用于获取图像我必须做出一个单独的HTTP GET请求。我可以用这个code显示图像:
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使用绑定与该不起作用。
这里是code是我的尝试:
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 [](字节阵列)的形象,只有上面的code可以在WP8.1显示。所以,我必须使用 dataWriter.StoreAsync()
方法,但如果我使用它,我必须使用异步
,它必须是无效的。但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.
您可以看到原来的code是我的注释去掉,但我不能使用它,因为输入值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 的到的任务的并绑定到其的结果的。 Stepen克利里写道有关一个很好的文章。你还会发现在 href=\"http://stackoverflow.com/a/15007717/2681948\">他的回答。
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.
这是这个问题的答案我已经建立一个样品,我想你可以修改,以满足您的需求基础。写的转换的将返回的 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请求绑定图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!