簡體   English   中英

將其連接到 sqlite 后無法構建我的應用程序

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

好的,所以當我運行我的應用程序時,我只得到一個:System.AggregateException,我試圖用調試器發現它,它把我帶到 AgendaDatabase.cs 文件中的這個 function:

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

} 

在使用調試器定位 System.AggregateException 之前,我在 AcceuilPage.xaml.cs 中也有一個 function 的 SystemAggregateException:

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

當我再執行一個時,它說:未處理的異常:SQLite.SQLiteException:沒有這樣的表:議程這很奇怪,因為如果它不存在,代碼應該創建它。

這是我正在關注的教程: https://learn.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;  }
    }
}

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>

訪問頁面.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();
        }
    }





}

首先,您必須指定數據庫 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;  }
    }
}

注意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