【发布时间】:2023-03-07 01:47:01
【问题描述】:
我对 xamarin 很陌生,我刚开始使用“使用 Xamarin 开始移动开发”使用 sqlite 数据库 由 Jesse Liberty 发表于复数视力。我很确定我的代码很好,它保存到我的数据库中,我已经使用数据库浏览器检查了这个,所以我知道数据在那里。但是当我转到我的列表视图时应用,数据不显示。
这是我的数据库代码
using System;
using SQLite;
using Xamarin.Forms;
using System.Collections.Generic;
using System.Linq;
namespace Todo
{
public class TodoDataBase
{
private SQLiteConnection database;
static object locker = new object();
public TodoDataBase ()
{
database = DependencyService.Get<ISQLite>().GetConnection();
database.CreateTable<TodoItem> ();
}
public TodoItem GetToDo(int id)
{
lock (locker)
{
return database.Table<TodoItem> ().Where (c => c.Id == id).FirstOrDefault ();
}
}
public IEnumerable<TodoItem> GetToDos()
{
lock (locker)
{
return (from c in database.Table<TodoItem>()
select c).ToList();
}
}
public int SaveToDo(TodoItem todothing)
{
lock (locker)
{
if (todothing.Id != 0)
{
database.Update (todothing);
return todothing.Id;
}
else
{
return database.Insert (todothing);
}
}
}
}
}
这是显示列表视图的页面背后的代码
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Todo
{
public partial class ReviewPage : ContentPage
{
public ReviewPage ()
{
InitializeComponent ();
}
public void onSelected(object o, ItemTappedEventArgs e)
{
var selectedItem = e.Item as TodoItem;
DisplayAlert ("You Selected", "Task " + selectedItem.TaskName, "Bye Bye");
}
public void onAppearing()
{
base.OnAppearing ();
TodoList.ItemsSource = App.Database.GetToDos ();
}
}
}
这是我的 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"
xmlns:local="clr-namespace:Todo;Assembly=Todo"
x:Class="Todo.ReviewPage">
<ContentPage.Resources>
<ResourceDictionary>
<local:DateTimeConverter x:Key = "dtConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout Padding = "15">
<ListView x:Name="TodoList" ItemTapped="onSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Padding="5" Spacing="2">
<Label x:Name="TaskNameDisplay" Text="{Binding TaskName}" FontSize="12" TextColor="Red"/>
<StackLayout Orientation="Horizontal">
<Label Text="Priority" FontSize="10"/>
<Label Text="{Binding Priority}" FontSize="10"/>
<Label Text="Due: " FontSize="12"/>
<Label Text="{Binding DueDate, Converter = {StaticResource dtConverter}}" FontSize="10"/>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
这是插入数据的主页面的 ViewModel 的代码 使用系统;
namespace Todo
{
public class CreatePageViewModel
{
public CreatePageViewModel ()
{
}
public void AddTask(string todo,string priority, DateTime duedate, int hour,int minute, int second, int Updateid, bool isDeleted)
{
var newtodo = new TodoItem
{
TaskName = todo,
Priority = priority,
Duedate = setDueDate (duedate, hour, minute, second),
IsDeleted = isDeleted,
Id = Updateid
};
App.Database.SaveToDo (newtodo);
}
private DateTime setDueDate( DateTime date, int hour,int minute, int second)
{
DateTime retVal = new DateTime (
date.Year, date.Month, date.Day,hour,minute, second);
return retVal;
}
}
}
【问题讨论】:
-
请更喜欢写下代码而不是捕获图像,您可能会因此而被否决,我建议您编辑您的问题并在文本中添加您的代码
-
GetTodos 执行时你确定你的数据库中有数据吗?我看到了创建数据库的代码,但没有插入任何种子数据。您的代码看起来正确。您可以尝试在创建数据库时插入一些测试数据。
-
@CDrosos 我已经写下了代码...
-
@Jason ,是的,我确信正在填充数据库。在我的模拟器上运行应用程序并从那里插入数据后,我使用 sqlite 数据浏览器检查了数据库,我可以在数据库文件中看到我的数据。
-
function GetToDos 是检索数据库中所有数据的函数
标签: c# sqlite xaml xamarin xamarin.forms