程序问答   发布时间:2022-05-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了调用带有 IJSRuntime 方法的 Task 时 Blazor 渲染两次大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决调用带有 IJSRuntime 方法的 Task 时 Blazor 渲染两次?

开发过程中遇到调用带有 IJSRuntime 方法的 Task 时 Blazor 渲染两次的问题如何解决?下面主要结合日常开发的经验,给出你关于调用带有 IJSRuntime 方法的 Task 时 Blazor 渲染两次的解决方法建议,希望对你解决调用带有 IJSRuntime 方法的 Task 时 Blazor 渲染两次有所启发或帮助;

我正在构建这个测试程序来学习 Blazor WASM,并且我构建了一个通用表,该表采用在给定行上运行函数的渲染片段。该函数只是一个简单的 Js 警报,显示指定行的某个字段的详细信息。

我的问题是这个调用导致 UI 呈现两次(我根本不希望它呈现)。我现在找到了一种解决方法,即使用对 ShouldRender() 的覆盖并将渲染布尔值在 Js 调用之前和之后都切换为 false。但是,我真的不喜欢这种方法。

有没有人知道为什么会发生这种情况?我可以避免使用不同的方法来执行此操作吗?

这是方法,它位于表的父组件中。您可以看到布尔值被设置了两次。这很重要,因为可以对表进行排序,并且每当单击警报时我都会丢失排序(如果我没有设置两次布尔值,也就是说。如果我在 Js 调用之前或之后设置一次,它会重新呈现一次,并且如果我根本不设置它,它会渲染两次)。

`private async Task HandleClick(WeatherForecast forcast)
{
    Render = false;
    await Js.InvokeVoIDAsync("alert",$"{forcast.TemperatureF}");
    Render = false;
}`

这里是调用方法的动作

<button @onclick="@(() => SELEctedItem.InvokeAsync(item))">@renderFragment</button>

@typeparam GenericType
@code {
[Parameter] public EventCallBACk<GenericType> SELEctedItem { get; set; }
[Parameter] public RenderFragment renderFragment { get; set; }
[Parameter] public GenericType item { get; set; }
}

这是点击按钮的表格。

@using System.ComponentModel;
@using System.Reflection;

@typeparam GenericType

@if (Items == null)
{
    <img src="https://media4.giphy.com/media/3oEjI6SIIHBdRxXI40/giphy.gif" />
}
else
{
    <table class="table">
        <thead>
            <tr>

                @foreach (var prop in Props)
                {
                    //Find the displayname,if it exists.
                    var name = PDesctirptions.Find(prop.name,truE)?.displayname ?? prop.name;

                    <td @onclick="@(() => OrderBy(prop.Name))">@name</td>
                }

                @* if we have actions,show them. *@
                @if(ActionFragment != null)
                {
                    <td>Actions</td>
                }

            </tr>
        </thead>
        <tbody>

            @foreach (var item in Items)
            {
                <tr>

                    @foreach (var p in Props)
                    {
                        //Get's the property value of the item to display in the table.
                        <td>@item.GetType().GetProperty(p.Name).GetValue(item).ToString()</td>
                    }

                    @if (ActionFragment != null)
                    {
                        <td>@ActionFragment(item)</td>
                    }

                </tr>
            }

        </tbody>
    </table>

}

@code {

    private bool desc = false;
    [Parameter] public IEnumerable<GenericType> Items { get; set; }
    [Parameter] public RenderFragment<GenericType> ActionFragment { get; set; }
    private Type T = typeof(GenericTypE);
    private PropertyInfo[] Props { get; set; }
    private PropertyDescriptorCollection PDesctirptions { get; set; }


    protected overrIDe voID OnInitialized()
    {

        Props = T.GetPropertIEs();
        PDesctirptions = TypeDescriptor.GetPropertIEs(T);

    }


    voID OrderBy(String Name)
    {
        PropertyDescriptor pDescriptor = TypeDescriptor.GetPropertIEs(T).Find(name,truE);

        if (desc)
        {
            Items = Items.OrderBy(d => pDescriptor.GetValue(d)).ToList();
            desc = false;
        }
        else
        {
            Items = Items.OrderByDescending(d => pDescriptor.GetValue(d)).ToList();
            desc = true;
        }
    }
}

这是表格的可视化,如果有帮助的话。 enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的调用带有 IJSRuntime 方法的 Task 时 Blazor 渲染两次全部内容,希望文章能够帮你解决调用带有 IJSRuntime 方法的 Task 时 Blazor 渲染两次所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。