async/await lets a .NET MAUI app start asynchronous work without blocking the UI while that work is incomplete. This matters for long-running or failing operations: a locked-up app creates a poor user experience. Use an async method when work such as database setup must finish, but the app should remain responsive. section 3.3
A constructor cannot be async. Put asynchronous initialization in a separate async method, then call it with a discard (`_`) from the constructor so initialization can begin without blocking the UI. Await asynchronous database calls when their results are needed before continuing. section 3.3
Load to-do items after page creation
A page creates its Database instance, starts `Initialize()` with `_ = Initialize()`, and makes `Initialize` return `Task`. Inside it, `await _database.GetTodos()` retrieves stored items before the page formats them for display. The same application uses asynchronous SQLite operations to create tables and insert items. section 3.3
Listing 3.11 Code for the MauiTodo main page
using MauiTodo.Data;
using MauiTodo.Models;
namespace MauiTodo;
public partial class MainPage : ContentPage
{
string _todoListData = string.Empty; #1
readonly Database _database; #2
public MainPage()
{
InitializeComponent();
_database = new Database(); #3
_ = Initialize(); #4
}
private async Task Initialize()
{
var todos = await _database.GetTodos(); #5
foreach (var todo in todos)
{
_todoListData += $"{todo.Title} - {todo.
Due:f}{Environment.NewLine}"; #6
}
TodosLabel.Text = _todoListData; #7
}
private async void Button_Clicked(object sender,EventArgs e)
{
var todo = new TodoItem #8
{
Due = DueDatepicker.Date,
Title = TodoTitleEntry.Text
};
var inserted = await _database.AddTodo(todo); #9
if (inserted != 0) #10
{
_todoListData += $"{todo.Title} - {todo.Due:f}{Environment.NewLine}"; #11
TodosLabel.Text = _todoListData; #12
TodoTitleEntry.Text = String.Empty; #13
DueDatepicker.Date = DateTime.Now; #13
}
}
}
Inspect the page constructor and `Initialize` method together to see a discarded initialization call followed by an awaited database query. section 3.3
















