Когда я добавляю библиотеку Sql - net -pcl в мое приложение xamarin и компилирую, то он показывает мне исключение и говорит, что ваше приложение находится в режиме прерывания.
Исключение:
`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);
}