我在Blazor应用程序中使用Mudblazor。以下代码打开一个对话框:
private async Task OpenDialog()
{
var options = new DialogOptions { CloseOnEscapeKey = true };
var dialog = DialogService.Show<AddNewAppDialog>("Adding new application", options);
// TODO refresh data on dialog close
}
AddNewAppDialog组件的代码非常简单:
<MudDialog>
<DialogContent>
<MudTextField @bind-Value="ApplicationName" Variant="Variant.Outlined" Label="Application Name" />
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Color="Color.Primary" OnClick="Submit">Ok</MudButton>
</DialogActions>
@code {
public string ApplicationName { get; set; }
[CascadingParameter]
MudDialogInstance MudDialog { get; set; }
async Task Submit()
{
await CreateApplication();
MudDialog.Close(DialogResult.Ok(true));
}
void Cancel() => MudDialog.Cancel();
}
对话框关闭时,如何在父组件中获取事件?
您必须使用await dialog.Result;
:
private async Task OpenDialog()
{
var options = new DialogOptions { CloseOnEscapeKey = true };
var dialog = DialogService.Show<AddNewAppDialog>("Adding new application", options);
// wait modal to close
var result = await dialog.Result;
// you can check if the modal was cancelled
var isCancelled = result.Cancelled;
// refresh your data
}
https://mudblazor.com/components/dialog#passing-数据