2016-04-28 67 views
0

我正在使用基於小型SQLLite的數據庫構建應用程序。這在Windows Phone上運行良好。現在我試圖讓它與Xamarin Forms(Portable)一起工作。數據庫的建立和填充工作正在進行。現在我試圖在ListView中顯示錶格的元素,但是我在ListView中獲取我的Table Notes的項目時遇到問題。 我的表格註釋設置並充滿了兩個項目:Xamarin中的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(); 
} 
} 

我有一個基於Contentpage一個XAML:

<?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 

回答

0

System.NullReferenceException:對象不設置爲一個對象的一個​​實例。 在Xamarin.Forms.Platform.WinRT.CellControl.SetCell(對象newContext)

DataTemplate需求來定義CellTextCellViewCell)要呈現否則當它試圖您會收到一個運行時異常呈現第一個數據綁定項。

因此環繞你Label至少ViewCell \ ViewCell.View

例子:

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

enter image description here

+0

或者,如果你需要的是一個標籤,嘗試做了更好的性能如下: '' – hvaughan3

+0

感謝您的貢獻ViewCell的工作非常完美 –

+0

@KeesDapperens沒問題,快樂的編碼 – SushiHangover