我有一个 blazor Web 程序集项目和一个信号 r 服务项目,我想在添加学生时调用对 ui 的更改。目前我必须刷新页面才能看到添加内容。
学生服务.cs
public class StudentService
{
public HubConnection connection;
public StudentServicen()
{
connection = new HubConnectionBuilder()
.WithUrl(".../StudentsHub")
.Build();
connection.StartAsync();
}
public async Task<List<Students>> GetAllStudents() =>
await connection.InvokeAsync<List<Students>>("GetAllStudents"));
public async Task<Boolean> AddStudent(StudentData student) =>
await connection.InvokeAsync<Boolean>("AddStudent", student);
}
Run Code Online (Sandbox Code Playgroud)
学生.razor
@inject StudentService StudentService
<ul >
@foreach (var student in students)
{
<li>@student.Name</li>
}
</ul>
@code {
private List<Students> students = new List<Students>();
protected override async Task OnInitializedAsync()
{
students = await StudentService.GetAllStudents();
}
Run Code Online (Sandbox Code Playgroud)
学生集中在另一个项目中。
@inject …
Run Code Online (Sandbox Code Playgroud)