【问题标题】:My database data is not showing on my xaml page in xamarin我的数据库数据未显示在 xamarin 中的 xaml 页面上
【发布时间】: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


【解决方案1】:

可能是上下文的问题。正如教程所说,在这里查看非常好的tutorial on how to use SQLite

在我们可以调用任何数据库功能之前,我们在上面创建的 SQLiteOpenHelper 子类需要知道应用程序的上下文才能使其工作。在我们的例子中,我们需要将应用程序上下文设置为我们的子类 DataManagerHelper。

所以首先尝试在 ToDo 类上添加一种设置 Content 的方法,然后在 ReviewPage 上设置 Content 并重试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-20
    • 2020-01-17
    • 2016-04-13
    • 2017-07-29
    • 2019-09-08
    • 1970-01-01
    • 2020-07-27
    • 2012-06-11
    相关资源
    最近更新 更多