gpt4 book ai didi

c# - StateHasChanged 第一次未检测到更改

转载 作者:行者123 更新时间:2023-12-03 17:12:21 27 4
gpt4 key购买 nike

看来StateHasChanged()仅在对任务执行等待操作后触发组件的重新渲染(找到 here)。

所以我只想使用 StateHasChanged用于重新渲染而不是使用 Task.delayTask.Run .

这是我的代码:

protected async Task ClearSearch()
{
SearchTestCoupon = new Mechanic.Shared.TestCouponSearch();
IsBusy = true;
TestCouponGroups = new List<Mechanic.Shared.TestCouponGroup>();
StateHasChanged(); // << -------- **FIRST TIME**
//await Task.Delay(TimeSpan.FromSeconds(1));
await GetTestCouponGroups(SearchTestCoupon);
}

protected async Task GetTestCouponGroups(object args)
{
TestCouponGroups = await TestCouponService.GetTestCouponGroupsAsync(SearchTestCoupon);
IsRowSelected = false;
IsBusy = false;
StateHasChanged(); // << -------- **SECOND TIME**
}

这里我使用加载器,它使用 IsBusy 激活。 .但是它第一次不工作 StateHasChanged如代码中所述调用。

但它第二次按预期呈现组件。

我用过 Task.Delay但它破坏了我的其他功能,如清除网格和显示搜索数据等。

最佳答案

StateHasChanged不会重新渲染组件,当事件委托(delegate) ( OnClick, OnMouseMouve... ) 完成时组件会重新渲染。

所以在你的情况下,如果你想在第一个 StateHasChanged 上重新渲染组件你的代码应该是:

protected void ClearSearch()
{
SearchTestCoupon = new Mechanic.Shared.TestCouponSearch();
IsBusy = true;
TestCouponGroups = new List<Mechanic.Shared.TestCouponGroup>();
GetTestCouponGroups(SearchTestCoupon); // don't await
StateHasChanged(); // << -------- **FIRST TIME**
}

protected async Task GetTestCouponGroups(object args)
{
TestCouponGroups = await TestCouponService.GetTestCouponGroupsAsync(SearchTestCoupon);
IsRowSelected = false;
IsBusy = false;
StateHasChanged(); // << -------- **SECOND TIME**
}

关于c# - StateHasChanged 第一次未检测到更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60630525/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com