Reputation: 131
I am building an app using a small SQLLite based database. This worked well on Windows Phone. Now I am trying to get it to work with Xamarin Forms (Portable). 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. 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:
<?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#:
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. What am I doing wrong?
System.NullReferenceException: Object reference not set to an instance of an object.
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
Upvotes: 0
Views: 745
Reputation: 74144
System.NullReferenceException: Object reference not set to an instance of an object. at 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.
So surround your Label
with at least ViewCell
\ ViewCell.View
,
Example:
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Horizontal">
Label Text="{Binding Note}" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
Upvotes: 1