Oct 10, 2024

MAUI: show a page as dialog

In MAUI there are standard dialogs like DisplayPromptAsync, but those are very limited. What if I need a full-fledged form to process user input, in modal dialog fashion? Like this:

    private async void OnCounterClicked(object sender, EventArgs e)
    {
        var dp = new DialogPage();
        await this.PushDialog(dp);
        //processing results...
    }

I have come up with a simple helper method:

public static class Helper
{
    public static Task PushDialog(this Page page, Page dialog)
    {
        return Task.Run(() => {
            dialog.NavigatingFrom += delegate { lock (dialog) Monitor.Pulse(dialog); };
            page.Dispatcher.Dispatch(async () => await page.Navigation.PushModalAsync(dialog));
            lock (dialog) Monitor.Wait(dialog);
        });
    }
}