我的数据库数据未显示在xamarin的xaml页面上

时间:2015-09-19 19:06:37

标签: c# sqlite xaml xamarin xamarin.forms

我是xamarin的新手,我刚开始使用sqlite数据库“使用Xamarin开始移动开发” 通过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的代码,其中插入了数据 使用System;

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;
        }
    }
}

0 个答案:

没有答案