[英]Pass Setter from Parent to Child Blazor
In Blazor Server Side, how can I pass a setter method from a parent component to a child?在 Blazor 服务器端,如何将 setter 方法从父组件传递给子组件? This seems super simple but I'm just not making it work.
这看起来超级简单,但我只是没有让它工作。
In the parent (a Syncfusion tabs "wizard" component) there's vars to determine whether or not a tab should be enabled.在父级(一个 Syncfusion 选项卡“向导”组件)中,有 vars 来确定是否应启用选项卡。 So in the parent there's something like
所以在父母有类似的东西
<SfTab CssClass="BlazorTab" @bind-SelectedItem="SelectedPage" LoadOn="ContentLoad.Demand">
<TabItems>
<TabItem>
<ChildContent>
<TabHeader Text="Select Media"></TabHeader>
</ChildContent>
<ContentTemplate>
<div id="mediaSelect">
<div class="wizard-title">Media Selection</div>
<ImageUploader SetMediaOption="@SetMediaOption" />
</div>
<div class="btn-container">
<SfButton >Upload media</SfButton>
</div>
</div>
</div>
</ContentTemplate>
</TabItem>
<TabItem Disabled="@MediaOptions">
<ChildContent>
<TabHeader Text="Select Options"></TabHeader>
</ChildContent>
<ContentTemplate>
<div id="mediaOptions">
<div class="wizard-title">Select media import options</div>
<br />
<div class="btn-container">
<SfButton @onclick="@SelectMediaPrevious">Previous</SfButton>
<SfButton @onclick="@SelectMediaNext">Next</SfButton>
</div>
</div>
</ContentTemplate>
</TabItem>
@code{
public bool MediaOption { get; set; } = true; // disabled initially
public void SetMediaOption(bool Value)
{
MediaOption = Value;
}
}
The child component ( ImageUploader
) needs to change the value of MediaOption
from true
(disabled) to false
(enabled) when there's at least one image uploaded.当至少上传一张图片时,子组件(
ImageUploader
)需要将MediaOption
的值从true
(禁用)更改为false
(启用)。 However, I can't seem to get the right type
defined for the app to compile.但是,我似乎无法为要编译的应用程序定义正确的
type
。 Is it possible to just pass in the actual MediaOption
getter / setter, or do I need to do the SetMediaOption
method?是否可以只传入实际的
MediaOption
getter / setter,还是我需要执行SetMediaOption
方法? If so, how do I define the parameter in the child component?如果是这样,我如何在子组件中定义参数?
I've tried many variations of我尝试了很多变体
[Parameter]
public EventCallback<bool> SetMediaOption { get; set; }
but the parent variable isn't changed (or it just fails to compile since the params type is incorrect)但是父变量没有改变(或者它只是因为参数类型不正确而无法编译)
You haven't shared the child, but I'm assuming you call SetMediaOption?.InvokeAsync(true);
您还没有共享孩子,但我假设您调用
SetMediaOption?.InvokeAsync(true);
at some point in your child.在你孩子的某个时候。 Your event handler is void, which (I believe) will not trigger
StateHasChanged
authomatically.您的事件处理程序是无效的,(我相信)不会自动触发
StateHasChanged
。 So you should add StateHasChanged()
at the end of the SetMediaOption
method.因此,您应该在
SetMediaOption
方法的末尾添加StateHasChanged()
。
Another option, that I didn't mention in my original post, is that you can change your event-handling method to async Task
rather than void
.我在原始帖子中没有提到的另一种选择是,您可以将事件处理方法更改为
async Task
而不是void
。
Last thing, if you DO use an async Task
, and you want to call StateHasChanged
in the middle for some reason, use await InvokeAsync(StateHasChanged)
instead of StateHasChanged()
最后一件事,如果您确实使用
async Task
,并且由于某种原因想要在中间调用StateHasChanged
,请使用await InvokeAsync(StateHasChanged)
而不是StateHasChanged()
In the Syncfusion blazor release 20.2.39 , an issue with Disable state of the tab item not working properly has been fixed.在 Syncfusion blazor版本 20.2.39中,已修复选项卡项的禁用状态无法正常工作的问题。 So, you can resolve the problem by upgrading the Syncfusion blazor packages version to the latest.
因此,您可以通过将 Syncfusion blazor 软件包版本升级到最新版本来解决问题。
Release notes: https://blazor.syncfusion.com/documentation/release-notes/20.2.39?type=all#tab发行说明: https ://blazor.syncfusion.com/documentation/release-notes/20.2.39?type=all#tab
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.