【发布时间】:2022-02-01 09:55:15
【问题描述】:
我有一个 Blazor 弹出窗口,它需要一个模板来显示他的内容:
CustomPopup.razor
<Popup
@bind-Visible="@CreatePopupVisible"
HorizontalAlignment="HorizontalAlignment.Center"
VerticalAlignment="VerticalAlignment.Center"
ShowFooter="true"
HeaderText="@("Create" + Label)">
<BodyTemplate>
@EditForm
</BodyTemplate>
<FooterContentTemplate>
</FooterContentTemplate>
</Popup>
@code {
[Parameter]
public string? Label { get; set; }
[Parameter]
public RenderFragment EditForm { get; set; }
bool CreatePopupVisible { get; set; }
}
我向这个组件传递了一个带有 2 个按钮的 EditForm,该按钮采用一个函数来处理取消选项:
CustomEditForm.razor
<EditForm Model="Entity" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="d-flex flex-column">
<InputNumber id="factory-id" class="mt-2 mb-2" @bind-Value="Entity.Id" />
<InputText id="factory-businessname" class="mt-2 mb-2" @bind-Value="Entity.BusinessName" />
<InputText id="factory-address" class="mt-2 mb-2" @bind-Value="Entity.Address" />
<div class="d-flex flex-row">
<button class="btn btn-primary mt-2 mb-2 flex-grow-1" type="submit">Modifica</button>
<button @onclick="HandleCancel" class="btn btn-secondary mt-2 mb-2 flex-grow-1" type="submit">Annulla</button>
</div>
</div>
</EditForm>
@code {
[Parameter]
public Action HandleCancel { get; set; }
[Parameter]
public Factory Entity { get; set; }
private void HandleValidSubmit()
{
Logger.LogInformation("HandleValidSubmit called");
}
}
如果我像这样正常使用 EditForm:
SomeFile.razor
<CustomEditForm HandleCancel="()=>{}">
一切都很好,但是当我在模板化组件中使用它时,如何从 CustomPupup.razor 文件中将函数传递给他?
当有人点击取消按钮时,我需要传递一个函数将 CreatePopupVisible 设置为 false
提前寻求帮助!
编辑
寻求帮助,但我认为我提供的信息不足,无法得到正确答案,我的错
所以,我希望能够正常调用 CustomEditForm 并传递由父级定义的函数来管理 HandleCancel,如下所示:
MainPage.razor
@page ...
<Grid @ref="Grid">
//the grid has a method that closes this CustomEditForm
<Template>
<CustomEditForm HandleCancel="() => Grid.CancelRowEdit()"/>
</Template>
</Grid>
但我也想使用自己内部定义的函数来关闭弹出窗口,如下所示:
另一个页面.razor
<CustomPopup>
<EditForm>
<CustomEditForm Entity="new Factory()"/>
</EditForm>
</CustomPopup>
在这种情况下,编辑表单应该通过调用 CustomPopup 定义的方法来处理取消
【问题讨论】:
标签: blazor blazor-webassembly asp.net-blazor