我正在开发我的第一个 Windows Phone 8.1 应用程序。我需要在 ListView 中绑定(bind)图像。图像采用bytes[] 格式。我已经使用此功能转换为位图图像
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;
}
}
现在我需要将此图像绑定(bind)到 ListView 项中的图像控件。
这是我的 XAML 代码。图像控件名称是 (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>
提前致谢
编辑: 以下是为澄清更多内容所做的工作:
1- 我使用图像文件名通过远程 Web 服务获取字节数组。
2- 我使用返回的 bytes[]
获取 bitmap
对象。
如何使用此数组或位图进行绑定(bind)?
我尝试了 here 中的示例, 但它对我不起作用,因为调用 Web 服务需要 async
调用,这在实现 IValueConverter
接口(interface)后是不可能的
最佳答案
假设您的 BitmapImage 位于属性中,准备绑定(bind)。您可以将图像源绑定(bind)到 BitmapImage
<Image x:Name="img_test" Source="{Binding MyBitmapImage}" Grid.Column="0" Grid.RowSpan="3" Margin="10,10,10,10" />
如果属性中没有 BitmapImage,那么您应该只设置对象的源
img_test.Source = myBitmapImage
关于c# - 从 ListView 中的字节绑定(bind)图像(Windows Phone 8.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34790604/