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

huangapple go评论45阅读模式
英文:

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

&lt;MudDialog&gt;
  &lt;TitleContent&gt;
    &lt;MudText Typo=&quot;Typo.h6&quot;&gt;Enter some text&lt;/MudText&gt;
  &lt;/TitleContent&gt;
  &lt;DialogContent&gt;
    &lt;MudTextField T=&quot;string&quot; @bind-Value=&quot;@this.input&quot; Immediate=&quot;@true&quot;/&gt;
  &lt;/DialogContent&gt;

  &lt;DialogActions&gt;
    &lt;MudButton
      Disabled=&quot;@string.IsNullOrWhiteSpace(this.input)&quot;
      OnClick=&quot;@this.OK&quot;
      Size=&quot;@Size.Small&quot;&gt;
      OK
    &lt;/MudButton&gt;
  &lt;/DialogActions&gt;
&lt;/MudDialog&gt;

@code {
  private string input = string.Empty;

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

  private void OK() =&gt; this.MudDialog.Close(DialogResult.Ok(this.input));
}

Use it like this:

Main.razor

&lt;MudGrid&gt;
  &lt;MudItem sm=&quot;12&quot;&gt;
    &lt;MudButton OnClick=&quot;@this.Prompt&quot; Color=&quot;Color.Default&quot;&gt;Prompt&lt;/MudButton&gt;
    &lt;MudText&gt;Input: @this.input&lt;/MudText&gt;
  &lt;/MudItem&gt;
&lt;/MudGrid&gt;

@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&lt;InputDialog&gt;(string.Empty, new DialogParameters(), options).Result;
    this.input = result.Data as string ?? string.Empty;
  }
}

Test it here: https://try.mudblazor.com/snippet/wOwRuQvUyTIflERS

huangapple
  • 本文由 发表于 2023年2月16日 19:46:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定