我尝试过PeriodicTimer和jsinterop,但两者都没有达到预期的行为
这是我的 @code 块,它不输出任何错误、异常或预期的行为
@code {
public List<Station> Stations = new List<Station>();
private List<Connection> Connections = new List<Connection>();
private List<CurvedConnection> CurvedConnections = new List<CurvedConnection>();
private List<string> LineElements = new List<string>();
private PeriodicTimer periodicTimer;
private CancellationTokenSource _cts = new CancellationTokenSource();
private DateTime currentTime = DateTime.Now;
private bool isInitialized = false;
protected override void OnInitialized()
{
SetupInitialStations();
UpdateConnections();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
isInitialized = true;
periodicTimer = new PeriodicTimer(TimeSpan.FromSeconds(15));
while (await periodicTimer.WaitForNextTickAsync(_cts.Token))
{
await JS.InvokeVoidAsync("reloadPage");
await JS.InvokeVoidAsync("console.log", "Periodic work done", currentTime);
}
}
public void Dispose()
{
_cts.Cancel();
periodicTimer?.Dispose();
}}
在这种情况下我所做的是导航到一个空页面,传递要返回的剃刀页面名称的参数。这会触发页面的重新加载。 因此,在页面顶部注入导航管理器:
@inject NavigationManager nav;
然后在您希望重新加载的代码中:
nav.NavigateTo("/EmptyPage?firstPage");
查看此问题:[https://stackoverflow.com/questions/70539158/refresh-blazor-page]