Add chapter 12 blazor project

This commit is contained in:
Alvin Ashcraft
2023-08-27 14:35:28 -04:00
parent 7ba92b1abd
commit 48f651f395
34 changed files with 1411 additions and 0 deletions
@@ -0,0 +1,18 @@
@page "/counter"
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
@@ -0,0 +1,57 @@
@page "/fetchdata"
@inject HttpClient Http
<PageTitle>Weather forecast</PageTitle>
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
@code {
private WeatherForecast[]? forecasts;
protected override async Task OnInitializedAsync()
{
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
}
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}
@@ -0,0 +1,9 @@
@page "/"
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
@@ -0,0 +1,25 @@
@page "/tasks"
<h3>Tasks - (@taskList.Count(task => !task.IsComplete)) incomplete</h3>
<ul>
@foreach (var task in taskList)
{
<li>
<input type="checkbox" @bind="task.IsComplete" />
<input @bind="task.Name" />
</li>
}
</ul>
<input placeholder="Enter new task..." @bind="newTask" />
<button @onclick="AddTask">Add task</button>
@code {
private IList<TaskItem> taskList = new List<TaskItem>();
private string? newTask;
private void AddTask()
{
if (!string.IsNullOrWhiteSpace(newTask))
{
taskList.Add(new TaskItem { Name = newTask });
newTask = string.Empty;
}
}
}