简体   繁体   English

将其连接到 sqlite 后无法构建我的应用程序

[英]Unable to build my app after connecting it to sqlite

Ok so when i run my app i just get a: System.AggregateException which i tried to spot with the debuguer and it brings me to this function in the AgendaDatabase.cs file:好的,所以当我运行我的应用程序时,我只得到一个:System.AggregateException,我试图用调试器发现它,它把我带到 AgendaDatabase.cs 文件中的这个 function:

public AgendaDatabase(string dbPath)
{
    database = new SQLiteAsyncConnection(dbPath);
    database.CreateTableAsync<Agenda>().Wait();

} 

Before targetting the System.AggregateException with the debugger i also had a SystemAggregateException for this function in AcceuilPage.xaml.cs:在使用调试器定位 System.AggregateException 之前,我在 AcceuilPage.xaml.cs 中也有一个 function 的 SystemAggregateException:

protected override async void OnAppearing()
{
   base.OnAppearing();
    AgendaCollection.ItemsSource = await App.Database.GetAgendasAsync();
}

and when i execute one more it says: unhandled exception: SQLite.SQLiteException: no such table: Agenda which is weird cause the code is supposed to create it if it doesn't exist.当我再执行一个时,它说:未处理的异常:SQLite.SQLiteException:没有这样的表:议程这很奇怪,因为如果它不存在,代码应该创建它。

This is the tutorial i am following: https://learn.microsoft.com/en-us/xamarin/get-started/quickstarts/database?pivots=windows这是我正在关注的教程: https://learn.microsoft.com/en-us/xamarin/get-started/quickstarts/database?pivots=windows

Thanks for your help.谢谢你的帮助。

I've done the steps mutiple time, even listened to similar tutorial with no solution on how to fix it: heres the code:我已经多次完成这些步骤,甚至听过类似的教程,但没有关于如何修复它的解决方案:代码如下:

AgendaDatabase.cs (in Database folder) AgendaDatabase.cs(在数据库文件夹中)

using System;
using System.Collections.Generic;
using System.Text;
using SQLite;
using Calculette.Models;
using System.Threading.Tasks;

namespace Calculette.Database
{
    public class AgendaDatabase
    {
        readonly SQLiteAsyncConnection database;

        public AgendaDatabase(string dbPath)
        {

            database = new SQLiteAsyncConnection(dbPath);
            database.CreateTableAsync<Agenda>().Wait();



        }


        public Task<List<Agenda>> GetAgendasAsync()
        {
            return database.Table<Agenda>().ToListAsync();
        }

        public Task<Agenda> GetAgendaAsync(int id)
        {
            return database.Table<Agenda>()
                            .Where(i => i.ID == id)
                            .FirstOrDefaultAsync();
        }

        public Task<int> SaveAgendaAsync(Agenda agenda)
        {
            if (agenda.ID != 0)
            {
                return database.UpdateAsync(agenda);
            }
            else
            {
                return database.InsertAsync(agenda);
            }
        }

        public Task<int> DeleteAgendaAsync(Agenda agenda)
        {
            return database.DeleteAsync(agenda);
        }
    }
}

Agenda.cs in the Models folder模型文件夹中的 Agenda.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using SQLite;
using Calculette.Database;

namespace Calculette.Models
{
    public class Agenda
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public string Topic { get; set; }
        public string Duration { get; set; }
        public DateTime Date { get; set; }
        public ObservableCollection<Speaker> Speakers { get; set; }
        public string Color { get; set; }
        public string Name { get; set; }

        public string Time { get; set;  }
    }
}

AcceuilPage.xaml in the Views folder Views 文件夹中的 AcceuilPage.xaml

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Calculette"
            xmlns:pv="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView"
            x:Class="Calculette.MainPage"
            BarBackgroundColor = "White"
            BarTextColor="#008A00">
<ContentPage Icon="icontache.png" BackgroundColor="#F6F8F9">
    <ContentPage.Content>
                <!-- ScrollView nous permet d'avoir une page scrollable-->

                    <ScrollView Orientation="Vertical">

                    <CollectionView Grid.Row="2" Margin="25" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
                            SelectionMode="None" x:Name="AgendaCollection">
                        <CollectionView.Header>
                            <StackLayout Orientation="Horizontal" Spacing="220">

                                <Label Text="Agenda" TextColor="Black" FontSize="18"/>

                                <ImageButton Source="iconplus.png"  HeightRequest="30" WidthRequest="30" Clicked="GoToNewFormPage"></ImageButton>



                            </StackLayout>



                        </CollectionView.Header>

                        <CollectionView.ItemsLayout>
                            <LinearItemsLayout Orientation="Vertical" ItemSpacing="20"/>
                        </CollectionView.ItemsLayout>
                        <CollectionView.ItemTemplate >
                            <DataTemplate>
                                <pv:PancakeView HasShadow="True" BackgroundColor="White" VerticalOptions="StartAndExpand " 
                                        HorizontalOptions="FillAndExpand" >
                                    <Grid VerticalOptions="StartAndExpand" HorizontalOptions="FillAndExpand">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <BoxView BackgroundColor="{Binding Color}" WidthRequest="3" HorizontalOptions="Start"
                                         VerticalOptions="FillAndExpand"/>
                                        <Expander Grid.Column="1">
                                            <Expander.Header>
                                                <Grid HorizontalOptions="FillAndExpand">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="*"/>
                                                        <ColumnDefinition Width="Auto"/>
                                                        <ColumnDefinition Width="3.5*"/>
                                                    </Grid.ColumnDefinitions>
                                                    <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
                                                        <Label Text="{Binding Date, StringFormat='{0:dd}'}" TextColor="#008A00" FontSize="27" 
                                                       HorizontalOptions="Center"/>

                                                        <Label Text="{Binding Date, StringFormat='{0:MMMM}'}" TextColor="Black" FontSize="10" 
                                                       HorizontalOptions="Center" Margin="0,-10,0,0" FontAttributes="Bold"/>
                                                        <ImageButton Source="iconplus.png" HorizontalOptions="Center" HeightRequest="30" WidthRequest="30" Clicked="GoToFormPage"></ImageButton>
                                                    </StackLayout>
                                                    <BoxView Grid.Column="1" BackgroundColor="#F2F4F8" WidthRequest="1" HorizontalOptions="Start" 
                                                     VerticalOptions="FillAndExpand"/>
                                                    <StackLayout Grid.Column="2" HorizontalOptions="Start" VerticalOptions="Center" Margin="20">
                                                        <Label Text="{Binding Topic}" TextColor="#008A00" FontSize="15" FontAttributes="Bold"/>
                                                        <Label Text="{Binding Duration}" TextColor="#2F3246" FontSize="12" Margin="0,-10,0,0"/>
                                                    </StackLayout>
                                                </Grid>
                                            </Expander.Header>
                                            <Grid HorizontalOptions="FillAndExpand">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="*"/>
                                                    <ColumnDefinition Width="Auto"/>
                                                    <ColumnDefinition Width="3.5*"/>
                                                </Grid.ColumnDefinitions>
                                                <BoxView Grid.Column="1" BackgroundColor="#F2F4F8" WidthRequest="1" HorizontalOptions="Start" 
                                                 VerticalOptions="FillAndExpand"/>
                                                <StackLayout Grid.Column="2" Spacing="10">
                                                    <Label Text="Tâches" TextColor="Black" FontSize="15" Margin="20,0"/>
                                                    <StackLayout BindableLayout.ItemsSource="{Binding Speakers}" HorizontalOptions="Start" VerticalOptions="Center" Margin="20,0,0,20">
                                                        <BindableLayout.ItemTemplate>
                                                            <DataTemplate>
                                                                <Label TextColor="#2F3246" FontSize="12">
                                                                    <Label.FormattedText>
                                                                        <FormattedString>
                                                                            <FormattedString.Spans>
                                                                                <Span Text="{Binding Time}"/>
                                                                                <Span Text=" - "/>
                                                                                <Span Text="{Binding Name}" FontAttributes="Bold"/>
                                                                            </FormattedString.Spans>
                                                                        </FormattedString>
                                                                    </Label.FormattedText>
                                                                </Label>
                                                            </DataTemplate>

                                                        </BindableLayout.ItemTemplate>

                                                    </StackLayout>


                                                </StackLayout>
                                            </Grid>
                                        </Expander>
                                    </Grid>
                                </pv:PancakeView>
                            </DataTemplate>
                        </CollectionView.ItemTemplate>
                    </CollectionView>



                </ScrollView>

            </ContentPage.Content>
        </ContentPage>

AcceuilPage.xaml.cs访问页面.xaml.cs

using Calculette.ViewModel;
using Calculette.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.PancakeView;

namespace Calculette
{
    public partial class MainPage : TabbedPage
    {
        public MainPage()
        {
            InitializeComponent();
            this.BindingContext = this;

        }
        protected async void GoToFormPage(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new Views.AgendaItemDetailPage());
        }
        protected async void GoToNewFormPage(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new Views.NewFormPage());
        }



        protected override async void OnAppearing()
        {
           base.OnAppearing();
            AgendaCollection.ItemsSource = await App.Database.GetAgendasAsync();
        }
    }





}

First of all, you must specify database model, because sqlite is not able to create table from your model.首先,您必须指定数据库 model,因为 sqlite 无法从您的 model 创建表。

namespace Calculette.Models
{
    [Table("Agenda")]
    public class Agenda
    {
        [PrimaryKey, AutoIncrement, Column("ID")]
        public int ID { get; set; }
        [Column("Topic")]
        public string Topic { get; set; }
        [Column("Duration")]
        public string Duration { get; set; }
        //public DateTime Date { get; set; }
        //public ObservableCollection<Speaker> Speakers { get; set; }
        [Column("Color")]
        public string Color { get; set; }
        [Column("Name")]
        public string Name { get; set; }
        [Column("Time")]
        public string Time { get; set;  }
    }
}

Be aware that sqlite does not support注意sqlite不支持

    public DateTime Date { get; set; }
    public ObservableCollection<Speaker> Speakers { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM