不确定是我的问题还是文档的问题。但是,到处都找不到答案。
以前在敬酒、提醒、情态动词等方面的经验在这里似乎不太一致。我想弹出这个小提示说,嘿,兄弟,你确定这是对的吗然后再提交,然后再后悔?如果用户选择"是",则允许提交通过,但如果用户选择"否",则退回并让他进行更改。
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);
}
From Display弹出窗口doc/显示警告
感谢David Ortinau @davidortinau