add-blazor-project #31

Merged
jleicher merged 5 commits from add-blazor-project into master 2020-12-29 21:04:02 +01:00
6 changed files with 101 additions and 13 deletions
Showing only changes of commit 700a06e7e0 - Show all commits

View File

@ -0,0 +1,22 @@
@page "/container"
@inject MyState State
@foreach (var name in State.Names)
{
<p>@name</p>
}
<Names />
<input @bind="@CurrentValue" />
<button @onclick="OnSend">Save</button>
@code {
private string CurrentValue;
private void OnSend()
{
State.AddName(CurrentValue);
CurrentValue = "";
}
}

View File

@ -1,23 +1,39 @@
@page "/" @page "/"
@inject LinksClient Links @inject LinksClient Links
<h1>Hello, world!</h1> <h1>Latest links</h1>
Welcome to your new app. @if (_loading)
@foreach (var link in _links)
{ {
<Link Item="@link" /> <p>Loading...</p>
}
else
{
<ul>
@foreach (var link in _links)
{
<li>
<Link Item="@link" />
</li>
}
</ul>
} }
<SurveyPrompt Title="How is Blazor working for you?" />
@code { @code {
private LinkDto[] _links = new LinkDto[] { }; private LinkDto[] _links = new LinkDto[] { };
private bool _loading = false;
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{
_loading = true;
try
{ {
_links = (await Links.GetLinksAsync()).ToArray(); _links = (await Links.GetLinksAsync()).ToArray();
} }
finally
{
_loading = false;
}
}
} }

View File

@ -0,0 +1,24 @@
@page "/links/{id:guid}"
@inject LinksClient Links
@if (Item == null)
{
<p>Loading...</p>
}
else
{
<h1>Showing link @Item.Url with @Item.UpVotes / @Item.DownVotes</h1>
}
@code {
[Parameter]
public Guid Id { get; set; }
private LinkDto Item;
protected override async Task OnInitializedAsync()
{
Item = await Links.GetLinkByIdAsync(Id);
}
}

View File

@ -2,14 +2,24 @@ using System;
using System.Net.Http; using System.Net.Http;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Client namespace Client
{ {
public class MyState
{
public List<string> Names { get; } = new List<string>();
public event Action OnChange;
public void AddName(string name)
{
Names.Add(name);
OnChange?.Invoke();
}
}
public class Program public class Program
{ {
public static async Task Main(string[] args) public static async Task Main(string[] args)
@ -19,6 +29,7 @@ namespace Client
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("http://localhost:8888/") }); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("http://localhost:8888/") });
builder.Services.AddScoped<LinksClient>(); builder.Services.AddScoped<LinksClient>();
builder.Services.AddSingleton<MyState>();
await builder.Build().RunAsync(); await builder.Build().RunAsync();
} }

View File

@ -1,4 +1,4 @@
<p>@Item.Url</p> <NavLink href=@($"/links/{Item.Id}")>@Item.Url</NavLink>
@code { @code {
[Parameter] [Parameter]

View File

@ -0,0 +1,15 @@
@inject MyState State
<p>Got @State.Names.Count</p>
@code {
protected override void OnInitialized()
{
State.OnChange += StateHasChanged;
}
public void Dispose()
{
State.OnChange -= StateHasChanged;
}
}