英文:
MudBlazor: how to return parameter to a caller?
问题
我有一个MudDialog,它获取输入参数。它运行正常。
如何从对话框中返回值?
即输出参数等。
例如,它设置了内部的字符串,我想在调用后获取。
英文:
I have a MudDialog which get input parameters. It works fine.
How can I return values from the dialog?
I.e. output parameter etc.
For example it sets string inside and I'd like to get after call.
答案1
得分: 3
你可以在MudDialogInstance的级联参数上调用MudDialog.Close(DialogResult.Ok(...))来将值返回给调用者。
一个快速演示:
InputDialog.razor
<MudDialog>
  <TitleContent>
    <MudText Typo="Typo.h6">输入一些文本</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">
      确定
    </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));
}
像这样使用它:
Main.razor
<MudGrid>
  <MudItem sm="12">
    <MudButton OnClick="@this.Prompt" Color="Color.Default">提示</MudButton>
    <MudText>输入:@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;
  }
}
在此处测试它:https://try.mudblazor.com/snippet/wOwRuQvUyTIflERS
英文:
You can call MudDialog.Close(DialogResult.Ok(...)) on the MudDialogInstance cascading parameter to return a value back to the caller.
A quick demo:
InputDialog.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));
}
Use it like this:
Main.razor
<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;
  }
}
Test it here: https://try.mudblazor.com/snippet/wOwRuQvUyTIflERS
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论