I have a ListView element that contains data and images from an HTTP GET request. I can display all the data in a ListView, except for the image. To get the image, I have to make a separate HTTP GET request. I can display the image using 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); }
But this does not work if I would like to use a ListView with Binding. Here is the code 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);
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 should be invalid. But the return value of void is not suitable for me because of the binding.
You can see the source code that I have uncommented, but I cannot use it, because the input value for image.SetSource() must be RandomAccessStream. Therefore, I do not know how I can solve this problem.
source share