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

Unable to build my app after connecting it to sqlite

好的,所以当我 运行 我的应用程序时,我只得到一个:System.AggregateException 我试图用调试器发现它 它把我带到 AgendaDatabase.cs 文件中的这个函数:

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

} 

在使用调试器定位 System.AggregateException 之前,我在 AcceuilPage.xaml.cs:

中也有针对此函数的 SystemAggregateException
protected override async void OnAppearing()
{
   base.OnAppearing();
    AgendaCollection.ItemsSource = await App.Database.GetAgendasAsync();
}

当我再执行一个时,它说:未处理的异常:SQLite.SQLiteException:没有这样的 table:议程很奇怪,因为如果它不存在,代码应该创建它。

这是我正在关注的教程:https://docs.microsoft.com/en-us/xamarin/get-started/quickstarts/database?pivots=windows

感谢您的帮助。

我已经多次完成这些步骤,甚至听过类似的教程但没有解决方法:代码如下:

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 在模型文件夹中

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 在视图文件夹中

<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

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





}

首先,您必须指定数据库模型,因为 sqlite 无法从您的模型创建 table。

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

注意sqlite不支持

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