【发布时间】:2020-05-29 14:06:42
【问题描述】:
我在 Blazor 中有一个学生表,它来自一个 api,我还收到一个推送数据来更新学生信息,这基本上是数据库更改后的分数,推送工作正常并且分数正在更新,但我还想在分数更改为红色时将表中已更新的字段的背景颜色更改为 td 标签几秒钟,我的代码如下:
@foreach(var student in SS.GetStudents()){
<tr>
<td> student.name </>
<td> student.section </>
// trying to compare between the previous and next state
var stud = SS.GetStuentsCopy().SingleOrDefault(s =>s.Id == student.Id);
var color = "";
if(stud.score != student.score){
color = red;
}
<td class="@color"> student.score </>
</tr>
}
@code{
[Inject]
public StudentsStates SS { get; set;}
public StudentsResponse Students { get; set; }
protected override async Task OnInitializedAsync()
{
// Subscribe to the StateChanged EventHandler
SS.StateChanged +=
SSAdvancedStateChanged;
Students = await Service.GetStudents();
// update the students and the copy together
SS.UpdateStudents(Students)
SS.UpdateStudentsCopy(Students)
//upon receiving students updated score
hubConnection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/studhub"))
.Build();
hubConnection.On<StudentsResponse>("ReceiveMessage", s =>
{
// update the students after 3 sec update the copy
SS.UpdateStudents(s);
//Here the state is not being updated
// unless there is a new push
// or the issue might be in rendering
// FYI without the sleep also I can see the changes in the color
System.Threading.Thread.Sleep(3000);
SS.UpdateStudentsCopy(s);
}
}}
StudentsStates.cs
namespace Ctrl.Web.Data
{
public class StudentsStates
{
public StudentsResponse Students { get; set; }
public StudentsResponse StudentsCopy { get; set; }
public StudentsResponse GetStudents(){return Students;}
public StudentsResponse GetStudentsCopy(){return StudentsCopy;}
public void UpdateStudents(Students students){ Students = students;}
public void UpdateStudentsCopy(Students students){ StudentsCopy = students;}
}}
正如我上面所说的一切正常,除了在一秒钟内多次推送时,第一次推送的学生分数的背景颜色变化太快,有时你甚至不会注意到它,因为推送的数据和状态正在更新,我想要的是在不影响下一个推送学生分数的情况下减慢背景颜色,或者如果有更好的方法来解决这种情况,我们非常感谢您的回答。
【问题讨论】:
标签: c# signalr blazor blazor-server-side