【发布时间】:2021-09-01 00:43:53
【问题描述】:
首先,我很抱歉我的英语水平不好。
我目前正在通过 dotnet core 3.1 blazor 开发一个 web 项目。
如下面的源码,我使用IJSRuntime来调用一个Javascript函数,需要很长时间。
[Inject]
IJSRuntime JSRuntime { get; set; }
private async Task BtnDisplay()
await JSRuntime.InvokeVoidAsync("RefreshWebJS", Data);
}
Javascript函数需要很长时间,所以我在下面添加了源以添加微调器。
private async Task BtnDisplay()
{
showLoadingImg = true; // add
await JSRuntime.InvokeVoidAsync("RefreshWebJS", Data);
showLoadingImg = false;
}
剃须刀页面上的微调器定义如下:
@if (showLoadingImg == true)
{
<div style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; text-align: center;">
<img src="/images/LoadingImg.gif" style="position: absolute; top: 50%; left: 50%;" />
</div>
}
“StateHasChanged()”或“await InvokeAsync(() => StateHasChanged())”也不起作用。
当我使用 Task 而不是 JSRuntime 时效果很好。
await Task.Delay(1000);
为什么我使用 JSRuntime.InvokeVoidAsync 不起作用?
感谢您阅读难读的英文。
【问题讨论】:
-
您是否尝试过将调用 JSRuntime 放在自己的任务中并使用 continueWith 来显示Loading = false?
-
是的,我尝试了“continueWith”,但没有成功。 Magoo 先生建议的 'Task.delay(1)' 方法效果很好
标签: blazor asp.net-core-3.1 asp.net-core-signalr blazor-jsinterop