how to get conditional returns from DisplayAlert in MAUI/Blazor?

Viewed 250

not sure if it is me or the documentation being obtuse on this one. but, can't find an answer anywhere.

previous experience with toasts, alerts, modals, etc seem to not correspond well here. i'd like to pop up this little alert and say, hey brother, are you sure that's right before you submit it and regret it later? then if the user says yes, allow the submission to go through, but if the user chooses no, drop back and let him make changes.

SomePage.razor

<EditForm Model="sampleModel" OnSubmit="@HandleValidSubmit">
lots of form stuff
<input type="submit" class="btn btn-success" value="Submit">
</EditForm>

@code{
    public async void HandleValidSubmit()
    {
        await DialogService.DisplayConfirm("HEY!", "Double check that info!", "It's correct", "Oops, one sec...");
        //want to have affirmative answer run the if block below
        //and negative answer to give user chance to make changes
        if (!string.IsNullOrWhiteSpace(sampleModel.IncidentNumber))
        {
            await Database.Init();
            Record subRecord = new Record();
            await Database.SaveRecordAsync(subRecord);
            {
                //submit things to database
            };            
        }
    }
}

IDialogService.cs

        internal class IDialogService
    {
        public async Task<bool> DisplayConfirm(string title, string message, string ok , string oops)
        {
                return await Application.Current.MainPage.DisplayAlert(title, message, ok,  oops);
        }
    }

DialogService.cs

    internal class DialogService : IDialogService
    {
    }

MauiProgram.cs

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            });
        builder.Services.AddMauiBlazorWebView();
        builder.Services.AddSingleton<IDialogService, DialogService>();
        return builder.Build();
    }
}
1 Answers
Related