英文:
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
<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();
and now when i want to use it
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");}
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 '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.
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("asd");}
is such a method. So parameters.Add("OkAction", okaction);
works. No need for the !
private Action okaction2(){ return ()=> Console.WriteLine("asd");}
defines a method that returns an Action.
parameters.Add("OkAction", okaction2); // won't work
parameters.Add("OkAction", okaction2()); // should work
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论