这段代码似乎并没有从UI中删除电影。模型确实发生了变化。只有当我显式调用StateHasChanged()
时,UI才会发生变化。
代码:
@code{
string name = "Guy";
public bool display { get; set; } = false;
[Parameter] public List<Movie> Movies { get; set; }
private async void Delete(Movie movie)
{
bool confirm = await js.InvokeAsync<bool>("confirm", "Sure?");
if (confirm == true)
{
Movies.Remove(movie);
Console.WriteLine(Movies.Count);
//StateHasChanged();
}
}
}
UI很简单:
@inject IJSRuntime js
<h1>Hello, world!</h1>
Welcome ,@StringsHelpers.toUpper(name) , to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
<div>
<h3>Movie</h3>
<input type="checkbox" @bind="display" />
<GenericList List="Movies">
<ElementTemplate>
<IndMovie IsDisplayed="display" Movie="context" Delete="Delete"></IndMovie>
</ElementTemplate>
</GenericList>
<div>
@foreach (var item in Movies)
{
@item.Title
}
</div>
<br />
</div>
似乎私有异步void Delete(电影(应该返回Task而不是
试试这个:
public async Task Delete(Movie movie)
{
bool confirm = await js.InvokeAsync<bool>("confirm", "Sure?");
if (confirm == true)
{
Movies.Remove(movie);
Console.WriteLine(Movies.Count);
//StateHasChanged();
}
}
其他问题可能是,如果电影列表没有在父级中更新取消状态。
如果代码没有让你尝试将Delete方法放在父组件中,并传递类似的参数