繁体   English   中英

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

[英]ListView in Xamarin Forms binding to database error

我正在使用基于SQLLite的小型数据库来构建应用程序。 这在Windows Phone上效果很好。 现在,我试图使其与Xamarin Forms(便携式)一起使用。 数据库的建立和表的填充正在工作。 现在,我试图在ListView中显示表的元素,但是在ListView中获取表注释的项目时遇到了问题。 我的表格注释已设置并包含两个项目:

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")));

我打电话给这些物品:

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

我有一个基于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>

ListView的绑定在C#中执行:

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

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

在Windows Phone上构建解决方案时,出现以下错误声明。 我究竟做错了什么?

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:对象引用未设置为对象的实例。 在Xamarin.Forms.Platform.WinRT.CellControl.SetCell(Object newContext)

您的DataTemplate需要定义将要呈现的CellTextCellViewCell ),否则当它尝试呈现第一个数据绑定项时,您将收到运行时异常。

因此,至少用ViewCell \\ ViewCell.View包围您的Label

例:

  <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