如何从 MAUI/Blazor 中的 DisplayAlert 获取条件 returns?

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

不确定是我的问题还是文档对这个问题的理解。 但是,到处都找不到答案。

以前在 toasts、警报、模态等方面的经验在这里似乎不太吻合。 我想弹出这个小警报并说,嘿,兄弟,你确定在提交之前是正确的然后后悔吗?然后,如果用户同意,则允许提交通过,但如果用户选择否,则退回并让他进行更改。

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();
    }
}
async void OnAlertYesNoClicked (object sender, EventArgs e)
{
  bool answer = await DisplayAlert ("Question?", "Would you like to play a game", "Yes", "No");
  Debug.WriteLine ("Answer: " + answer);
}

来自Display Pop-ups doc / Display an alert

Thanks to David Ortinau @davidortinau