public class TodoItem { public TodoItem () { } public TodoItem (int id, string label, bool isDone) { Id = id; Label = label; IsDone = isDone; } public int Id { get; set; } public string Label { get; set; } public bool IsDone { get; set; } }
我们可以通过override重写初始化,并给Todos设置一些数据。
private IList<TodoItem> Todos; private int id = 0; protected override void OnInitialized () { Todos = new List<TodoItem> () { new TodoItem (++id, "Learn Blazor", false), new TodoItem (++id, "Code a todo list", false), new TodoItem (++id, "Learn something else", false) }; }
展示还有多少未完成的任务
<h1> Todo List(@Todos.Count(todo => !todo.IsDone)) <span>Get things done, one item at a time.</span> </h1>
当任务没有时,我们展示默认效果,提示用户无任务
<p class="emptylist" style="display: @(Todos.Count()>0?"none":"");">Your todo list is empty.</p>
新增一个任务
<form name="newform"> <label for="newitem">Add to the todo list</label> <input type="text" name="newitem" id="newitem" @bind-value="Label"> <button type="button" @onclick="AddItem">Add item</button> </form>
这里我们用了一个Label变量,一个onclick事件。
private string Label;
private void AddItem() { if (!string.IsNullOrWhiteSpace(Label)) { Todos.Add (new TodoItem { Id = ++id, Label = Label }); Label = string.Empty; } this.SortByStatus(); }