【问题标题】:Blazor - Compare Previous and Next StateBlazor - 比较上一个和下一个状态
【发布时间】: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


    【解决方案1】:

    我建议为学生行创建一个组件,如下所示:

    @foreach(var student in SS.GetStudents())
    {
          <StudentRow Student={student} />
    }
    

    然后在 StudentRow 组件中,您可以创建一个新的私有学生变量,您可以在延迟 3 秒后更新它并在那里进行比较,而不是将 id 保存在列表或另一个副本中:

    StudentRow.razor

      <tr>
          <td> Student.name </>
          <td> Student.section </>
          var color = "";
          if(StudentCopy.score != Student.score){
            color = red;
           }
          <td class="@color"> student.score </>
     </tr>
     @code{
             public StudentResponse StudentCopy { get; set; }
             [Parameter]
             public StudentResponse Student { get; set; }
    
             protected override async Task OnParametersSetAsync()
             {
              await Task.Delay(3000);
              StudentCopy = Student;
    
             }
          }
    

    当组件从渲染树中的父级接收到参数并且传入的值已分配给属性时调用的 OnParametersSetAsync 方法。 Read more here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-19
      • 2019-05-31
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2019-02-22
      相关资源
      最近更新 更多