App developer noob here. I’m trying to build a simple .NET MAUI app using VS 2022 that has a SQLITE database as an embedded resource. I’m using the sqlite-net-pcl package.
I have a repository called WitbitsRepository with this class:
public class WitbitsRepository
{
private SQLiteAsyncConnection db;
public WitbitsRepository()
{
Debug.WriteLine("WitbitsRepository constructor called");
var databasePath = Path.Combine(FileSystem.AppDataDirectory, "Witbits.db");
Debug.WriteLine("Database path: " + databasePath);
if (!File.Exists(databasePath))
{
Debug.WriteLine("Database file not found at: " + databasePath);
CopyDatabaseFromResource(databasePath);
}
else
{
Debug.WriteLine("Database file found at: " + databasePath);
}
db = new SQLiteAsyncConnection(databasePath);
CreateTablesAsync().Wait();
}
private async Task CreateTablesAsync()
{
await db.CreateTableAsync<Book>();
}
My App.xaml.cs file has code to initialize the database before loading MainPage:
public partial class App : Application
{
public App()
{
InitializeComponent();
InitializeAppAsync();
}
private async void InitializeAppAsync()
{
await InitializeDatabaseAsync();
MainPage = new MainPage();
}
private async Task InitializeDatabaseAsync()
{
var repository = new WitbitsRepository();
await repository.InitializeDatabaseAsync();
}
}
}
When I run the app in debug (Android Pixel 5) mode, the MainPage never loads. What am I missing?
I put a button on the MainPage to check database connectivity, but the MainPage isn’t even appearing.
Andrew Dvorak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.