简体   繁体   English

将 Setter 从父级传递给子级 Blazor

[英]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.

相关问题 如何将可选的 RenderFragment 从父组件传递给 Blazor 中的子组件? - How to pass an optional RenderFragment from a parent component to a child component in Blazor? Blazor 值从动态呈现的子级传递给父级 - Blazor value pass to the parent from dynamically rendered child c# blazor 服务器 - 将 class object 从孩子传给父母 - c# blazor server - pass class object from child to parent 在 Blazor 中将 Type 从父组件传递给子用户控件 - Pass Type from parent component to a child user control in Blazor 如何将数据从父级传递给子级 blazor 组件 - How to pass data from parent to child blazor component 如何将值从子组件(RenderFragment)传递到 C# Blazor 中的父页面? - How to pass value from Child component (RenderFragment) to Parent page in C# Blazor? 如何将列表从子组件传递到 blazor 中的父组件? - How can I pass List from child component to parent component in blazor? 如何将 function 结果从父组件传递给子组件 C# Mud Blazor - How to pass function result from parent component to child with C# Mud Blazor Blazor 将数据从父级传递给子级 - Blazor Passing Data from Parent to Child 在 blazor 中渲染之前,将一些数据从子组件发送到父组件 - Send some data from child to parent component before render in blazor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM