I am developing my first windows phone 8.1 app. I need to bind an image in a list view. the image is in bytes[] format. I have already converted to a Bitmap image using this function
public async Task<BitmapImage> GetImageFromByteArray(string s_FileName)
{
using (InMemoryRandomAccessStream raStream = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(raStream))
{
byte[] data = await GetImageBytes(s_FileName);
writer.WriteBytes(data);
await writer.StoreAsync();
await writer.FlushAsync();
writer.DetachStream();
}
raStream.Seek(0);
BitmapImage bitMapImage = new BitmapImage();
bitMapImage.SetSource(raStream);
return bitMapImage;
}
}
now i need to bind this image to an image control in a listview item.
Here is my XAML Code. the image control name is (img_test)
<Grid>
<ListView x:Name="lst_Test" Background="White" Foreground="Black" SelectionChanged="lst_BestDrivers_SelectionChanged" Margin="10">
<ListView.Resources>
<DataTemplate x:Key="ItemsTest">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15*" />
<ColumnDefinition Width="15*" />
<ColumnDefinition Width="15*" />
<ColumnDefinition Width="30" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<Grid Grid.Column="0" Grid.RowSpan="4" />
<Grid Grid.Column="1" Grid.ColumnSpan="5" />
<Image x:Name="img_test" Grid.Column="0" Grid.RowSpan="3" Margin="10,10,10,10" />
<TextBlock Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="5" Text="{Binding Name}"></TextBlock>
<StackPanel Orientation="Horizontal" Grid.Column="2" Grid.Row="1">
<TextBlock Text="{Binding ID}" HorizontalAlignment="Left" VerticalAlignment="Center"></TextBlock>
<Image Source="ms-appx:///Assets/Icons/Icon1png" HorizontalAlignment="Right" VerticalAlignment="Center"></Image>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.Resources>
<ListView.ItemTemplate>
<StaticResource ResourceKey="ItemsTest"/>
</ListView.ItemTemplate>
</ListView>
</Grid>
Thanks in advance
EDIT: Here is what is done to clarify more:
1- I use the Image Filename to get an array of bytes using a remote web service.
2- I use the returned bytes[]
to get a bitmap
object.
How can I use this array or the bitmap for binding?
I tried the sample in here , but it did not work for me because the calling of the web service requires an async
call which is not possible after implementing the IValueConverter
interface