add-blazor-project #31
22
Apps/Client/Pages/Container.razor
Normal file
22
Apps/Client/Pages/Container.razor
Normal 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 = "";
|
||||
}
|
||||
}
|
||||
@ -1,23 +1,39 @@
|
||||
@page "/"
|
||||
@inject LinksClient Links
|
||||
|
||||
<h1>Hello, world!</h1>
|
||||
|
||||
Welcome to your new app.
|
||||
<h1>Latest links</h1>
|
||||
|
||||
@if (_loading)
|
||||
{
|
||||
<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 {
|
||||
private LinkDto[] _links = new LinkDto[] { };
|
||||
private bool _loading = false;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
_loading = true;
|
||||
|
||||
try
|
||||
{
|
||||
_links = (await Links.GetLinksAsync()).ToArray();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_loading = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
Apps/Client/Pages/Show.razor
Normal file
24
Apps/Client/Pages/Show.razor
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -2,14 +2,24 @@ using System;
|
||||
using System.Net.Http;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
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 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<LinksClient>();
|
||||
builder.Services.AddSingleton<MyState>();
|
||||
|
||||
await builder.Build().RunAsync();
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<p>@Item.Url</p>
|
||||
<NavLink href=@($"/links/{Item.Id}")>@Item.Url</NavLink>
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
|
||||
15
Apps/Client/Shared/Names.razor
Normal file
15
Apps/Client/Shared/Names.razor
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user