System.TypeInitializationException:''SQLite.SQLiteConnection'的类型初始值设定项引发了异常。'

问题描述 投票:0回答:1

当我在我的xamarin应用程序中添加Sql-net-pcl库并进行编译时,它向我显示了一个异常并说您的应用程序处于中断模式。

Exception:

     `System.TypeInitializationException: 'The type initializer for 'SQLite.SQLiteConnection' threw an exception.'

ISqLiteDb:

public interface ISqLiteDb
{
    SQLiteAsyncConnection GetConnection();
}

SqLiteDb:

class SqLiteDb: ISqLiteDb
{
    public SQLiteAsyncConnection GetConnection()
    {
        var documePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        var path = Path.Combine(documePath, "Shopping.db");

        return new SQLiteAsyncConnection(path);
    }
}

这里我正在打开连接并创建表。

MainPage.Xaml.cs:

public partial class MainPage : ContentPage
{
    private ObservableCollection<Recipe> _recipes;

    private readonly SQLiteAsyncConnection _connection;
    public MainPage()
    {
        InitializeComponent();
        _connection = DependencyService.Get<ISqLiteDb>().GetConnection();
    }

    protected override async void OnAppearing()
    {
        await _connection.CreateTableAsync<Recipe>();
        var recipes = await _connection.Table<Recipe>().ToListAsync();

        _recipes = new ObservableCollection<Recipe>(recipes);

        listView.ItemsSource = _recipes;
    }

    private async void OnAdd(object sender, EventArgs e)
    {
        var recipe = new Recipe
        {
            Id = 1,
            Name = "Recipe1"
        };

        await _connection.InsertAsync(recipe);
        _recipes.Add(recipe);
    }
c# sql xamarin xamarin.forms xamarin.android
1个回答
0
投票

我发布自己的问题的答案,因为经过大量的研究和努力,我提出了解决方案。

我已通过将sql-net-pcl库从1.7.302-beta]更新到版本[[1.6.292]解决了问题]

© www.soinside.com 2019 - 2024. All rights reserved.