【问题标题】:(Blazor ) Child Component not rendering(Blazor)子组件未呈现
【发布时间】:2020-11-04 06:40:03
【问题描述】:

我使用 Syncfusion 创建了一个 Dialog 组件,我试图从父组件调用它,但它没有呈现。

这是我的父组件,我在我的子组件(ModalAcceptCancel)中使用@ref,所以当我单击删除按钮(在我的网格中)时,我能够调用一个名为 OpenDialog 的方法来呈现或显示我的模态。

@using App.Dtos
@using App.Data
@using Syncfusion.Blazor.Buttons
@inject NavigationManager NavigationManager

//This is my child component
<ModalAcceptCancel @ref="Modal"/>
//
<SfGrid @ref="SfGridProfesores" DataSource="@DsProfesores" AllowPaging="true">
    <GridColumns>
        <GridColumn Field=@nameof(ProfesorDto.Id) HeaderText="Identificador" TextAlign="TextAlign.Right" Width="120">
        </GridColumn>
        <GridColumn Field=@nameof(ProfesorDto.Nombres) HeaderText="Nombre" Width="200"></GridColumn>
        <GridColumn Field=@nameof(ProfesorDto.CorreoElectronico) HeaderText="Correo Electrónico" Width="150"></GridColumn>
        <GridColumn HeaderText="Acciones" TextAlign="TextAlign.Center" Width="120">
            <Template>
                @{
                    var profesor = (context as ProfesorDto);             
                    <SfButton @onclick="@(() => NavigationManager.NavigateTo("Profesor/"+profesor.Id))" CssClass="e-info">Editar</SfButton>
                    <SfButton CssClass="e-danger" @onclick="(() => DeleteProfesor(profesor.Id))">Borrar</SfButton>                                  
                }
            </Template>
        </GridColumn>
    </GridColumns>
</SfGrid>

@code {
    [Parameter]
    public IReadOnlyList<ProfesorDto> DsProfesores { get; set; }

    [Inject]
    public IProfesorService ProfesorService { get; set; }

    SfGrid<ProfesorDto> SfGridProfesores;

    ModalAcceptCancel Modal; 

    //Here i try to open my Modal (ChildComponent)
    private void DeleteProfesor(int id)
    {
        Modal.OpenDialog();
        //ProfesorService.BorraProfesor(id);
       // SfGridProfesores.Refresh();
    }
}

这是来自我的子组件的代码。我的 Syncfusion 对话框可见性绑定到“Is Visible 属性”,当调用 OpenDialog 方法时,该属性更改为 true,它应该显示我的模式。

@using Syncfusion.Blazor.Popups
@using Syncfusion.Blazor.Buttons


<SfDialog Width="500px" ShowCloseIcon="true" CloseOnEscape="true" @bind-Visible="@IsVisible">
    <DialogTemplates>
        <Header> Atención </Header>
        <Content> ¿Estás seguro que deseas eliminar este elemento? </Content>
        <FooterTemplate>
            <SfButton CssClass="e-primary e-flat" @onclick="@CloseDialog">
                Aceptar
            </SfButton>
            <SfButton CssClass="e-flat" @onclick="@CloseDialog">
                Cancelar
            </SfButton>
        </FooterTemplate>
    </DialogTemplates>
</SfDialog>

@code {
    private bool IsVisible { get; set; } = false;

    public void OpenDialog()
    {
        this.IsVisible = true;
    }

    public void CloseDialog()
    {
        this.IsVisible = false;
    }
}

¿我做错了什么? ¿ 这样做有更好的做法吗?

我对开发真的很陌生。对不起我的英语。

【问题讨论】:

    标签: render blazor syncfusion


    【解决方案1】:

    来自 Syncfusion 支持的问候,

    我们已经验证了您报告的查询和共享代码块。 IsVisible 属性是从另一个 razor 页面调用的,因此属性更改不受 SfDialog 影响,因此不会显示。我们建议您调用属性更新旁边的 StateHasChanged() 来解决报告的问题。我们还准备了一个样本,试图满足您的要求。

    示例:https://www.syncfusion.com/downloads/support/directtrac/general/ze/SFDIAL~22087980339

    如果解决方案有帮助,请告诉我们,

    问候,

    英德拉吉特

    【讨论】:

    • 很抱歉,您的解释完全错误。 SfDialog 的 Visible 属性绑定到父级的 IsVisible 属性,它应该可以工作。问题是他使用 async void 而不是 async Task,因此运行时无法知道操作何时完成(没有 Task 对象表示操作已完成),因此应该自动调用 StateHasChanged 方法(UI点击事件,对吗?)被跳过,因此不会发生重新渲染。
    • 是的,这似乎是确切的原因。感谢您的澄清..!
    【解决方案2】:

    这是应该调用模态的代码,对吧?

     @onclick="(() => BorrarProfesor(profesor.Id))"
    

    你需要这个代码:

    @onclick="(() => DeleteProfesor(profesor.Id))"
    

    调用 DeleteProfesor 方法,将教授 id 传递给它,Modal.OpenDialog(); 从中执行...

    定义为 DeleteProfesor 如下:

    //Here i try to open my Modal (ChildComponent)
        private async void DeleteProfesor(int id)
        {
            Modal.OpenDialog();
            //ProfesorService.BorraProfesor(id);
           // SfGridProfesores.Refresh();
        }
    

    希望对您有所帮助!如果您遇到困难,请告诉我

    【讨论】:

    • 它不起作用:(。我尝试使用来自单个页面的同步融合对话框,它工作正常。我还尝试将我的 IsVisible 属性初始化为 True 并查看模态是否从开始吧。我不知道我是否遗漏了什么,或者它是否是同步融合的错误,因为对话框文档有一些问题。
    • 谢谢!我改变了它的代码,但问题仍然存在:(当我调试它时,Modal.OpenDialog() 干净地执行,但由于某种原因没有显示同步对话框。但是当我将我的 IsVisible 属性初始化为 True 并查看模式是否显示从一开始就是这样。也许是同步融合的问题?
    • 我再次更新了我的答案,向您展示如何定义 DeleteProfesor。试试看,如果有效,请报告我。
    • 不,这不是同步融合的问题。我认为这是因为您使用 void 而不是 async void。无论如何,如果这不起作用,请放置 StateHasChanged();在 Modal.OpenDialog() 之后的下一行;
    猜你喜欢
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2020-01-04
    • 2021-04-01
    • 2017-11-04
    • 2023-03-25
    • 2020-06-20
    相关资源
    最近更新 更多