MudBlazor:如何将参数返回给调用者?

Ole*_*leg 3 blazor mudblazor

我有一个获取输入参数的 MudDialog。效果很好。如何从对话框返回值?即输出参数等。例如,它在内部设置字符串,我想在调用后获取。

Bar*_*ers 6

您可以调用MudDialog.Close(DialogResult.Ok(...))级联MudDialogInstance参数以将值返回给调用者。

一个快速演示:

输入对话框.razor

<MudDialog>
  <TitleContent>
    <MudText Typo="Typo.h6">Enter some text</MudText>
  </TitleContent>
  <DialogContent>
    <MudTextField T="string" @bind-Value="@this.input" Immediate="@true"/>
  </DialogContent>

  <DialogActions>
    <MudButton
      Disabled="@string.IsNullOrWhiteSpace(this.input)"
      OnClick="@this.OK"
      Size="@Size.Small">
      OK
    </MudButton>
  </DialogActions>
</MudDialog>

@code {
  private string input = string.Empty;

  [CascadingParameter]
  private MudDialogInstance MudDialog { get; set; } = default!;

  private void OK() => this.MudDialog.Close(DialogResult.Ok(this.input));
}
Run Code Online (Sandbox Code Playgroud)

像这样使用它:

主剃须刀

<MudGrid>
  <MudItem sm="12">
    <MudButton OnClick="@this.Prompt" Color="Color.Default">Prompt</MudButton>
    <MudText>Input: @this.input</MudText>
  </MudItem>
</MudGrid>

@code {
  private string input = string.Empty;

  [Inject]
  private IDialogService DialogService { get; set; } = default!;

  private async Task Prompt()
  {
    var options = new DialogOptions
    {
      CloseButton = true,
      DisableBackdropClick = false,
      MaxWidth = MaxWidth.Small
    };

    var result = await this.DialogService.Show<InputDialog>(string.Empty, new DialogParameters(), options).Result;
    this.input = result.Data as string ?? string.Empty;
  }
}
Run Code Online (Sandbox Code Playgroud)

在这里测试:https ://try.mudblazor.com/snippet/wOwRuQvUyTIflERS