简体   繁体   English

Xamarin Forms中的ListView绑定到数据库错误

[英]ListView in Xamarin Forms binding to database error

I am building an app using a small SQLLite based database. 我正在使用基于SQLLite的小型数据库来构建应用程序。 This worked well on Windows Phone. 这在Windows Phone上效果很好。 Now I am trying to get it to work with Xamarin Forms (Portable). 现在,我试图使其与Xamarin Forms(便携式)一起使用。 The database building and filling of the tables is working. 数据库的建立和表的填充正在工作。 Now I am trying to show elements of an table in ListView, but I am having problems getting the items of my Table Notes in the ListView. 现在,我试图在ListView中显示表的元素,但是在ListView中获取表注释的项目时遇到了问题。 My Table Notes is set up and is filled with two Items: 我的表格注释已设置并包含两个项目:

public class Notes
{
//The Id property is marked as the Primary Key
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int Id { get; set; }
public string Note { get; set; }
public DateTimeOffset NoteDate { get; set; }
public TimeSpan NoteStart { get; set; }
public Notes()
{
}
public Notes(string remark, DateTimeOffset notedate, TimeSpan noteStart)
{
Note = remark;
NoteDate = notedate;
NoteStart = noteStart;
}
}
db.Insert(new Notes("Test", DateTimeOffset.Now.AddDays(0), TimeSpan.Parse("7:45")));
db.Insert(new Notes("Test", DateTimeOffset.Now.AddDays(1), TimeSpan.Parse("7:45")));

I am calling the Items with: 我打电话给这些物品:

public IEnumerable<Notes> GetItems()
{
{
return (from i in db.Table<Notes>() select i).ToList();
}
}

I have a XAML based Contentpage: 我有一个基于XAML的Contentpage:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Todo.Views.DatePage">
<ListView x:Name="Listnotes">
<ListView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Note}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>

The Binding of the ListView is performed in C#: ListView的绑定在C#中执行:

NoteHelper nh = new NoteHelper();
private IEnumerable<Notes> notelist = new List<Notes>();

notelist = nh.GetItems((DateTimeOffset.Now));
Listnotes.ItemsSource = notelist;

When I build the solution on my Windows Phone I am getting the following error statements. 在Windows Phone上构建解决方案时,出现以下错误声明。 What am I doing wrong? 我究竟做错了什么?

System.NullReferenceException: Object reference not set to an instance of an object. System.NullReferenceException:对象引用未设置为对象的实例。

at Xamarin.Forms.Platform.WinRT.CellControl.SetCell(Object newContext)
at Xamarin.Forms.Platform.WinRT.CellControl.MeasureOverride(Size availableSize)
at Windows.UI.Xaml.UIElement.Measure(Size availableSize)
at Xamarin.Forms.Platform.WinRT.VisualElementRenderer`2.MeasureOverride(Size availableSize)
at Windows.UI.Xaml.UIElement.Measure(Size availableSize)
at Xamarin.Forms.Platform.WinRT.VisualElementR

System.NullReferenceException: Object reference not set to an instance of an object. System.NullReferenceException:对象引用未设置为对象的实例。 at Xamarin.Forms.Platform.WinRT.CellControl.SetCell(Object newContext) 在Xamarin.Forms.Platform.WinRT.CellControl.SetCell(Object newContext)

Your DataTemplate needs to define the Cell ( TextCell , ViewCell ) that will be rendered otherwise you will receive an runtime exception when it tries to render the the first data bound item. 您的DataTemplate需要定义将要呈现的CellTextCellViewCell ),否则当它尝试呈现第一个数据绑定项时,您将收到运行时异常。

So surround your Label with at least ViewCell \\ ViewCell.View , 因此,至少用ViewCell \\ ViewCell.View包围您的Label

Example: 例:

  <DataTemplate>
    <ViewCell>
      <ViewCell.View>
        <StackLayout Orientation="Horizontal">
            Label Text="{Binding Note}" />
          </StackLayout>
      </ViewCell.View>
    </ViewCell>
  </DataTemplate>

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM