此应用的数据源是SQLite数据库。除了一点之外,一切都很好。
我想用来自该数据库的数据填充项目网格,并使用该数据库中存储的图片作为背景图像。
public class Movie_Picture
{
[SQLite.AutoIncrement, SQLite.PrimaryKey]
public int id { get; set; }
public byte[] Picture { get; set; }
public int Movie_id { get; set; }
}
如果我用
查询此表 var query = connection.QueryAsync<Movie_Picture>("Select * from Movie_Picture");
我可以填充项目网格,该网格中的每个项目都从Movie_id和id获取其标签,但图像未显示
此网格的XAML看起来像
<GridView x:Name="DataGrid1" SelectionChanged="DataGrid1_SelectionChanged" ItemsSource="{Binding}">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Width="300" Height="300" removed="{x:Null}">
<TextBlock Text="{Binding id}" Margin="0,140,0,0"></TextBlock>
<TextBlock Text="{Binding Movie_id}" Margin="0,180,0,0"></TextBlock>
<Image x:Name="itemImage" Source="{Binding Picture, Converter={StaticResource BytesToImageConverter}}" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
编辑:
转换器代码
public class BytesToImageConverter : IValueConverter
{
public object Convert(object value, Type typeName, object parameter, string language)
{
if (value != null && value is byte[])
{
byte[] bytes = value as byte[];
Stream stream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.SetSource(new MemoryRandomAccessStream(stream));
return image;
}
return null;
}
public object ConvertBack(object value, Type typeName, object parameter, string language)
{
throw new NotImplementedException();
}
}
任何帮助都会得到满足。
答案 0 :(得分:1)
发现问题。这是转换器。
public class BytesToImageConverter : IValueConverter
{
public object Convert(object value, Type typeName, object parameter, string language)
{
if (value != null && value is byte[])
{
byte[] bytes = value as byte[];
Stream stream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.SetSourceAsync(new MemoryRandomAccessStream(stream));
return image;
}
return null;
}
public object ConvertBack(object value, Type typeName, object parameter, string language)
{
throw new NotImplementedException();
}
}
解决方案是将image.SetSource()
更改为image.SetSourceAsync