Blazor为什么我可以将void传递给组件而不是操作?

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

blazor why i can pass void to component and not action?

问题

我有一些Blazor组件对话框,如下所示:

<MudDialog>
    <DialogContent>
        <MudText>@ContentText</MudText>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel">@CancelButtonText</MudButton>
        <MudButton Color="@Color" Variant="Variant.Filled" OnClick="@InvokeOkAction">@ButtonText</MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] MudDialogInstance? MudDialog { get; set; }
    [Parameter] public string? ContentText { get; set; }
    [Parameter] public string? ButtonText { get; set; }
    [Parameter] public string? CancelButtonText { get; set; } = "Anuluj";
    [Parameter] public Color Color { get; set; }
    [Parameter] public Action? OkAction { get; set; }

    private void InvokeOkAction()
    {
        OkAction?.Invoke();
        MudDialog?.Close(DialogResult.Ok(true));
    }

    void Cancel() => MudDialog?.Cancel();
}

现在当我想要使用它时:

private void SaveAsProd()
{
    var parameters = new DialogParameters();
    parameters.Add("ContentText", "Do you really want to delete these records?");
    parameters.Add("ButtonText", "Delete");
    parameters.Add("OkAction", okaction!);
    var options = new DialogOptions() { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall };

    DialogService.Show<SimpleDialogComponent>("????", parameters, options);
}

private void okaction() { Console.WriteLine("asd"); }

private Action okaction2() { return () => Console.WriteLine("asd"); }

我遇到了传递此Action/OkAction的问题。如果我传递okaction,它可以正常工作,但如果我传递okaction2,我会得到以下错误:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Unable to set property 'OkAction' on object of type 'xx.Client.Components.SimpleDialogComponent'. The error was: Specified cast is not valid.
System.InvalidOperationException: Unable to set property 'OkAction' on object of type 'xx.Client.Components.SimpleDialogComponent'. The error was: Specified cast is not valid.
---> System.InvalidCastException: Specified cast is not valid.

为什么会这样?我无法传递某个Action吗?即使通过lambda表达式传递也会出现相同的错误?感谢和问候!

英文:

i have some blazor component dialog as

 &lt;MudDialog&gt;
&lt;DialogContent&gt;
    &lt;MudText&gt;@ContentText&lt;/MudText&gt;
&lt;/DialogContent&gt;
&lt;DialogActions&gt;
    &lt;MudButton OnClick=&quot;Cancel&quot;&gt;@CancelButtonText&lt;/MudButton&gt;
    &lt;MudButton Color=&quot;@Color&quot; Variant=&quot;Variant.Filled&quot; OnClick=&quot;@InvokeOkAction&quot;&gt;@ButtonText&lt;/MudButton&gt;
&lt;/DialogActions&gt;
&lt;/MudDialog&gt;

   @code {
    [CascadingParameter] MudDialogInstance? MudDialog { get; set; }
    [Parameter] public string? ContentText { get; set; }
    [Parameter] public string? ButtonText { get; set; }
    [Parameter] public string? CancelButtonText { get; set; } = &quot;Anuluj&quot;;
    [Parameter] public Color Color { get; set; }

[Parameter] public Action? OkAction { get; set; }

    private void InvokeOkAction()
    {
        OkAction?.Invoke();
        MudDialog?.Close(DialogResult.Ok(true));
    }


void Cancel() =&gt; MudDialog?.Cancel();

and now when i want to use it

 private void SaveAsProd()
{
    var parameters = new DialogParameters();
    parameters.Add(&quot;ContentText&quot;, &quot;Do you really want to delete these records?&quot;);
    parameters.Add(&quot;ButtonText&quot;, &quot;Delete&quot;);
    parameters.Add(&quot;OkAction&quot;, okaction!);
    var options = new DialogOptions() { CloseButton = true, MaxWidth = MaxWidth.ExtraSmall };

    DialogService.Show&lt;SimpleDialogComponent&gt;(&quot;????&quot;, parameters, options);

}
   private  void okaction() { Console.WriteLine(&quot;asd&quot;);}

   private  Action  okaction2(){ return ()=&gt;  Console.WriteLine(&quot;asd&quot;);}

and i have and issue with passing this action/OkAction
if i pass void okaction it works fine
but if i pass it as action / okaction2
i get

 Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
  Unhandled exception rendering component: Unable to set property &#39;OkAction&#39; on object of type &#39;xx.Client.Components.SimpleDialogComponent&#39;. The error was: Specified cast is not valid.
 System.InvalidOperationException: Unable to set property &#39;OkAction&#39; on object of type &#39;xx.Client.Components.SimpleDialogComponent&#39;. The error was: Specified cast is not valid.
 ---&gt; System.InvalidCastException: Specified cast is not valid.

why is that? canot i pass some action? it would be easier to pass it via lambda even but error is the same?

thanks and regards !

答案1

得分: 1

[参数] public Action? OkAction { get; set; }

OkAction 是一个 Action,期望一个返回 void 的方法。

private void okaction() { Console.WriteLine("asd");}

是这样的一个方法。所以 parameters.Add("OkAction", okaction); 可以工作。不需要 !

private Action okaction2() { return () => Console.WriteLine("asd");}

定义了一个返回 Action 的方法。

parameters.Add("OkAction", okaction2); // 不会工作
parameters.Add("OkAction", okaction2()); // 应该工作

英文:
[Parameter] public Action? OkAction { get; set; }

OkAction is an Action and expects a method that returns void.

private  void okaction() { Console.WriteLine(&quot;asd&quot;);}

is such a method. So parameters.Add(&quot;OkAction&quot;, okaction); works. No need for the !

private  Action  okaction2(){ return ()=&gt;  Console.WriteLine(&quot;asd&quot;);}

defines a method that returns an Action.

parameters.Add(&quot;OkAction&quot;, okaction2);    // won&#39;t work
parameters.Add(&quot;OkAction&quot;, okaction2());  // should work

huangapple
  • 本文由 发表于 2023年3月7日 22:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663560.html
匿名

发表评论

匿名网友

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

确定