Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从SQLite数据库绑定ListView图像,Xamarin表单_C#_Image_Sqlite_Xaml_Xamarin.forms - Fatal编程技术网

C# 从SQLite数据库绑定ListView图像,Xamarin表单

C# 从SQLite数据库绑定ListView图像,Xamarin表单,c#,image,sqlite,xaml,xamarin.forms,C#,Image,Sqlite,Xaml,Xamarin.forms,我有一个人模型: public class Person { [PrimaryKey, AutoIncrement] public int Id { get; set; } public string FirstName{ get; set; } public string LastName{ get; set; } public byte[] Image { get; set; } } 在我的SQLite数据库中,表中存储了图片。我正在尝试在列表

我有一个人模型:

 public class Person
 {
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string FirstName{ get; set; }
    public string LastName{ get; set; }
    public byte[] Image { get; set; }
 }
在我的SQLite数据库中,表中存储了图片。我正在尝试在列表视图中显示图像。因此实现了一个图像转换器:

  public class ImageConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ImageSource retSource = null;
        if (value != null)
        {
            byte[] imageAsBytes = (byte[])value;
            var stream = new MemoryStream(imageAsBytes);
            retSource = ImageSource.FromStream(() => stream);
        }
        return retSource;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo     culture)
    {
        throw new NotImplementedException();
    }

}
在Xaml代码中,我尝试绑定och,并将图像转换为如下所示:

<Page.Resources>
    <converters:ImageConverter x:Key="ImageConverter" />
</Page.Resources>
<ContentPage.Content>
    <ListView x:Name="PersonListView">

        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell Height="220">
                  <Image Source="{Binding Image, Converter={StaticResource ImageConverter}}"/>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>

当我运行该页面时,会出现以下错误:

System.NullReferenceException:对象引用未设置为对象的实例


我不明白我的代码哪里错了。如何查看数据库中的图片

在转换器中尝试此代码,并告诉我它是否有效:

public class ImageConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {                        
        return "data:image;base64," + Convert.ToBase64String(value);;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo     culture)
    {
        throw new NotImplementedException();
    }

}

异常具体发生在哪里?检查调用堆栈。当我试图查看ContentPage时会发生这种情况,我是指代码中的位置。异常有一个调用堆栈。当我打开调用堆栈时,它指向:Views.personpage.initializeComponent然后查看InnerException。请看这里: