- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试打开一个带有父对话框回调的子对话框。如果我没有将任何参数从 child 传回给 parent ,我可以让它工作,但是当我尝试将 bool 值从 child 传递给 parent 时,我得到一个编译时错误。
The best overloaded Add method 'name' for the collection initializer has some invalid arguments.
现在很少有关于这个实现的细节来解决这个困难。
我正在使用 Blazor (Net 5) 和 Radzen 组件。 Specifically this one.
Dialog OpenAsync 的第二个参数是
但是,我正在尝试一些新的东西,因为我的“对象”是一个 EventCallback,它可以在没有任何额外参数的情况下工作,但带有参数似乎不太适合编译器。
父级
var retval = await DialogService.OpenAsync<ManageSubscriptionDialog>($"Subscription Management",
new Dictionary<string, object>() { { nameof(ManageSubscriptionDialog.SubscriptionStateChanged), UpdateSubscription } },
new DialogOptions() { Width = "600px", Height = "auto" });
private EventCallback UpdateSubscription => new(null, (Action)(async () =>
{
//do stuff
}));
child
[Parameter]
public EventCallback SubscriptionStateChanged { get; set; }
//throw back to parent to handle selection
await SubscriptionStateChanged.InvokeAsync();
父级
var retval = await DialogService.OpenAsync<ManageSubscriptionDialog>($"Subscription Management",
new Dictionary<string, object>() { { nameof(ManageSubscriptionDialog.SubscriptionStateChanged), UpdateSubscription } },
new DialogOptions() { Width = "600px", Height = "auto" });
private EventCallback<bool> UpdateSubscription(bool isCancelled) => new(null, (Action)(async () =>
{
//do stuff
}
child
[Parameter]
public EventCallback<bool> SubscriptionStateChanged { get; set; }
protected async Task InvokeSubscriptionParent(bool isCancelled)
{
//throw back to parent to handle selection
await SubscriptionStateChanged.InvokeAsync(isCancelled);
}
这甚至无法编译,所以我在其中添加了一些语法糖,试图让它开心。
父级
delegate EventCallback<bool> DelegateUpdateSubscription(bool isCancelled);
DelegateUpdateSubscription adfasdf = UpdateSubscription;
var retval = await DialogService.OpenAsync<ManageSubscriptionDialog>($"Subscription Management",
new Dictionary<string, object>() { { nameof(ManageSubscriptionDialog.SubscriptionStateChanged), adfasdf } },
new DialogOptions() { Width = "600px", Height = "auto" });
private EventCallback<bool> UpdateSubscription(bool isCancelled) => new(null, (Action)(async () =>
{
//do stuff
}
child
[Parameter]
public EventCallback<bool> SubscriptionStateChanged { get; set; }
protected async Task InvokeSubscriptionParent(bool isCancelled)
{
//throw back to parent to handle selection
await SubscriptionStateChanged.InvokeAsync(isCancelled);
}
现在可以编译,但在运行时对话框打开时会抛出以下错误。
Unable to set property 'SubscriptionStateChanged' on object of type 'MyApp.Shared.Components.Subscription.ManageSubscriptionDialog'. The error was: Unable to cast object of type 'DelegateUpdateSubscription' to type 'Microsoft.AspNetCore.Components.EventCallback`1[System.Boolean]'.
我不会列出我尝试过的所有内容,我现在正试图弄清楚如何将一个值传递回父级,但我很困惑。
所以在一天结束时,我要做的就是弄清楚如何将参数从子对话框传递回父对话框。
我现在可以在没有错误的情况下打开对话框,只需要弄清楚将变量从子项传回父项的最后一步。
我在这里更新了父代码行。当我应该传递父对象作为回调处理程序时,我传递了 null。
不正确
private EventCallback<bool> UpdateSubscription => new(null, (Action)
正确
private EventCallback<bool> UpdateSubscription => new(this, (Action)
正确
private EventCallback UpdateSubscription => new(this, (Action<bool>)(async (bool www) =>
最佳答案
所以编译器错误是由于一些缺失值和不匹配的参数造成的。
这是一个带有参数化回调的工作示例。
需要注意的重要一点是第一个(字符串)值需要与子项中的名称完全匹配。第二个(对象)是父级回调的名称。
ParentComponent.cs
var retval = await DialogService.OpenAsync<ManageSubscriptionDialog($"Subscription Management",
new Dictionary<string, object>() { { "SubscriptionStateChanged", UpdateSubscriptionHandler } },
new DialogOptions() { Width = "600px", Height = "auto" });
private EventCallback UpdateSubscriptionHandler => new(this, (Action<UpdateSubscriptionApiModel>)(async (UpdateSubscriptionApiModel apiModel) =>
{ await UpdateSubscription(apiModel); }));
UpdateSubscriptionApiModel 只是一个 poco,我不会在此处包含它。您可以轻松地使用 bool 或 string 或类似的东西。
此处需要注意 2 项。
lambda 的第一个参数 new(this,.... 在这里您将指定回调的接收者。通常这会在幕后为您处理,但您需要自己编写 lambda解析为参数字典的“对象”。
await UpdateSubscription(apiModel) 是处理回调的实际方法。
私有(private)异步任务 UpdateSubscription(UpdateSubscriptionApiModel apiModel){ ...做你需要做的.... }
Child.cs
[Parameter]
public EventCallback SubscriptionStateChanged { get; set; }
UpdateSubscriptionApiModel model = new UpdateSubscriptionApiModel();
async Task OnManageSubscriptionSubmit()
{
...do whatever you need on child....
await SubscriptionStateChanged.InvokeAsync(model);
}
就这样吧。如果我对某事不清楚,请发表评论,我会尽可能回复。
关于c# - Blazor Dialog EventCallback 在没有参数的情况下工作,但编译器会抛出带参数的 CS1950,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70463828/
运行 PostgreSQL(7.4 和 8.x),我认为这是可行的,但现在我遇到了错误。 我可以单独运行查询,它工作得很好,但如果我使用 UNION 或 UNION ALL,它会抛出错误。 这个错误:
我试图为我的应用程序创建一个导航,使用抽屉导航我的 fragment 之一(HomeFragment)有一个 ViewPager,可容纳 3 个 fragment (Bundy Clock、Annou
以我目前正在开发的应用为例: - 它有一个包含多个项目的抽屉导航;现在有两个项目让我感兴趣,我将它们称为 X 和 Y。 X 和 Y 都在单击时显示包含 x 元素或 y 元素列表的 fragment 选
我有一个形状为 (370,275,210) 的 NumPy 数组,我想将其重新整形为 (275,210,370)。我将如何在 Python 中实现这一点? 370是波段数,275是行数,210是图像包
我们如何与被子 UIViewController 阻止的父 UIViewController(具有按钮)交互。显然,触摸事件不会通过子 Nib 。 (启用用户交互) 注意:我正在加载默认和自定义 NI
我是 Jpa 新手,我想执行过程 我的代码如下 private static final String PERSISTENCE_UNIT_NAME = "todos"; private static
与安装了 LAMP 的 GCE 相比,选择与 Google Cloud SQL 链接的 GCE 实例有哪些优势? 我确定 GCE 是可扩展的,但是安装在其上的 mysql 数据库的可扩展性如何? 使用
这个问题在这里已经有了答案: Value receiver vs. pointer receiver (3 个答案) 关闭 3 年前。 我刚接触 golang。只是想了解为 Calc 类型声明的两种
我不小心按了一个快捷键,一个非常漂亮的断线出现在日期上。 有点像 # 23 Jun 2010 -------------------- 有人知道有问题的快捷方式吗?? (我在 mac 上工作!) 在
我正在Scala中编写正则表达式 val regex = "^foo.*$".r 这很好,但是如果我想做 var x = "foo" val regex = s"""^$x.*$""".r 现在我们有
以下 XML 文档在技术上是否相同? James Dean 19 和: James Dean 19 最佳答案 这两个文档在语义上是相同的。在 X
我在对数据帧列表运行稳健的线性回归模型(使用 MASS 库中的 rlm)时遇到问题。 可重现的示例: var1 <- c(1:100) var2 <- var1*var1 df1 <- data.f
好的,我有一个自定义数字键盘,可以在标签(numberField)中将数字显示为 0.00,现在我需要它显示 $0.00。 NSString *digit = sender.currentTitle;
在基于文档的应用程序中,使用 XIB 文件,创建新窗口时其行为是: 根据最后一个事件的位置进行定位和调整大小 window 。 如果最后一个事件窗口仍然可见,则新窗口 窗口应该是级联的,这样它就不会直
我想使用参数进行查询,如下所示: SELECT * FROM MATABLE WHERE MT_ID IN (368134, 181956) 所以我考虑一下 SELECT * FROM MATABLE
我遇到一些性能问题。 我有一个大约有 200 万行的表。 CREATE TABLE [dbo].[M8]( [M8_ID] [int] IDENTITY(1,1) NOT NULL,
我在 jquery 中的按键功能遇到问题。我不知道为什么按键功能不起作用。我已经使用了正确的 key 代码。在我的函数中有 2 个代码,其中包含 2 个事件键,按一个键表示 (+) 代码 107 和(
我想显示音频波形,我得到了此代码,它需要.raw音频输入并显示音频波形,但是当我放入.3gp,.mp3音频时,我得到白噪声,有人可以帮助我如何使其按需与.3gp一起使用使用.3gp音频运行它。 Inp
我无法让 stristr 函数返回真值,我相信这是因为我的搜索中有一个 $ 字符。 当我这样做时: var_dump($nopricecart); 完整的 $nopricecart 值是 $0 ,我得
如果我有这样的循环: for(int i=0;i O(n) 次。所以do some执行了O(n)次。如果做某事是线性时间,那么代码片段的复杂度是O(n^2)。 关于algorithm - 带 If 语
我是一名优秀的程序员,十分优秀!