关于Blazor服务器的页面更新



很抱歉,我的问题很简单。

我正在为Blazor服务器上的生产设备编写一个简单的管理应用程序。我想在Blazor页面上查看这些数据,因为完成的零件数量每秒从生产设备更新到数据库。

具体来说,我希望在数据库刷新后或每隔几秒(例如,每10秒)刷新页面。

然而,我不知道如何使这些。应该如何考虑和实施这一点?

我很抱歉提出这个非常抽象的问题,但我想要一些建议。谢谢你。

所以除非有一个事件从你的数据库发布,你的应用程序可以监听,那么你将需要查询数据库设置间隔

这个解决方案也适合你的情况:Blazor Timer调用异步API任务来更新UI

基本上你会设置一个计时器。以下是微软的通用文档:https://learn.microsoft.com/en-us/dotnet/api/system.threading.timer?view=net-5.0

实际上,当组件初始化时,你将把你的代码。

我个人在@code部分有以下内容:

private System.Timers.Timer DelayTimer;
async void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
await CheckForNotifications();
}
async Task CheckForNotifications()
{
// Your Code goes here.
StateHasChanged();
}
protected override async Task OnInitializedAsync()
{
_log.Information("Component is Initialized.");
await CheckForNotifications();
DelayTimer = new System.Timers.Timer((double)(60 * 1000 * 3)); // 60 seconds * 3 is 3 minutes.
DelayTimer.Elapsed += timer_Elapsed;
DelayTimer.AutoReset = true;
DelayTimer.Start();
_log.Debug("Timer Started.");
}

最新更新