Nikita Romanenko
5 years ago
23 changed files with 149 additions and 133 deletions
@ -1,15 +0,0 @@ |
|||
using System; |
|||
|
|||
namespace blazor_lifetime.Data |
|||
{ |
|||
public class WeatherForecast |
|||
{ |
|||
public DateTime Date { get; set; } |
|||
|
|||
public int TemperatureC { get; set; } |
|||
|
|||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); |
|||
|
|||
public string Summary { get; set; } |
|||
} |
|||
} |
@ -1,25 +0,0 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace blazor_lifetime.Data |
|||
{ |
|||
public class WeatherForecastService |
|||
{ |
|||
private static readonly string[] Summaries = new[] |
|||
{ |
|||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" |
|||
}; |
|||
|
|||
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate) |
|||
{ |
|||
var rng = new Random(); |
|||
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast |
|||
{ |
|||
Date = startDate.AddDays(index), |
|||
TemperatureC = rng.Next(-20, 55), |
|||
Summary = Summaries[rng.Next(Summaries.Length)] |
|||
}).ToArray()); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,10 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace blazor_lifetime.Models |
|||
{ |
|||
public delegate Task RenderHandleAsync(); |
|||
public interface IAsyncComponentBase |
|||
{ |
|||
RenderHandleAsync RenderChildContentAsync { get; set; } |
|||
} |
|||
} |
@ -0,0 +1,5 @@ |
|||
@page "/async" |
|||
|
|||
<h1>Async Content Rendering</h1> |
|||
|
|||
<FirstLevelComponentAsync /> |
@ -1,19 +0,0 @@ |
|||
@page "/counter" |
|||
|
|||
<h1>Counter</h1> |
|||
|
|||
<p>Current count: @currentCount</p> |
|||
|
|||
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button> |
|||
<br /> |
|||
|
|||
<FirstLevelComponentAsync /> |
|||
|
|||
@code { |
|||
private int currentCount = 0; |
|||
|
|||
private void IncrementCount() |
|||
{ |
|||
currentCount++; |
|||
} |
|||
} |
@ -1,46 +0,0 @@ |
|||
@page "/fetchdata" |
|||
|
|||
@using blazor_lifetime.Data |
|||
@inject WeatherForecastService ForecastService |
|||
|
|||
<h1>Weather forecast</h1> |
|||
|
|||
<p>This component demonstrates fetching data from a service.</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 ForecastService.GetForecastAsync(DateTime.Now); |
|||
} |
|||
} |
@ -0,0 +1,5 @@ |
|||
@page "/async_improved" |
|||
|
|||
<h1>Improved Async Content Rendering</h1> |
|||
|
|||
<FirstLevelAdvancedComponentAsync /> |
@ -1,5 +1,5 @@ |
|||
@page "/" |
|||
|
|||
<h1>Hello, world!</h1> |
|||
<h1>HELLO!</h1> |
|||
|
|||
Welcome to your new app. |
|||
Welcome to async content test app. |
|||
|
@ -0,0 +1,5 @@ |
|||
@page "/sync" |
|||
|
|||
<h1>Sync Content Rendering</h1> |
|||
|
|||
<FirstLevelComponent /> |
@ -0,0 +1,31 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Components; |
|||
using blazor_lifetime.Models; |
|||
|
|||
namespace blazor_lifetime.Shared |
|||
{ |
|||
public class AdvancedComponentBase : OwningComponentBase, IAsyncComponentBase |
|||
{ |
|||
[Parameter] public virtual RenderFragment ChildContent { get; set; } |
|||
public RenderHandleAsync RenderChildContentAsync { get; set; } |
|||
|
|||
protected override async Task OnAfterRenderAsync(bool firstRender) |
|||
{ |
|||
if (firstRender) |
|||
{ |
|||
await OnComponentMountAsync().ConfigureAwait(false); |
|||
if (RenderChildContentAsync != null) |
|||
{ |
|||
await RenderChildContentAsync(); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
await Task.CompletedTask; |
|||
} |
|||
} |
|||
|
|||
protected virtual Task OnComponentMountAsync() |
|||
=> Task.CompletedTask; |
|||
} |
|||
} |
@ -0,0 +1,36 @@ |
|||
@using blazor_lifetime.Models |
|||
|
|||
@if (_isContentVisible) |
|||
{ |
|||
@ChildContent |
|||
} |
|||
else |
|||
{ |
|||
@if (LoadingContent != null) |
|||
{ |
|||
@LoadingContent |
|||
} |
|||
} |
|||
|
|||
@code{ |
|||
private bool _isContentVisible = false; |
|||
|
|||
[Parameter] public RenderFragment ChildContent { get; set; } |
|||
[Parameter] public RenderFragment LoadingContent { get; set; } |
|||
[Parameter] public IAsyncComponentBase Component { get; set; } |
|||
|
|||
protected override void OnInitialized() |
|||
{ |
|||
if (Component == null) |
|||
{ |
|||
throw new Exception(nameof(Component)); |
|||
} |
|||
Component.RenderChildContentAsync += OnRenderChildContentAsync; |
|||
} |
|||
|
|||
protected async Task OnRenderChildContentAsync() |
|||
{ |
|||
_isContentVisible = true; |
|||
await InvokeAsync(StateHasChanged); |
|||
} |
|||
} |
@ -0,0 +1,12 @@ |
|||
@inherits FirstLevelAdvancedComponentAsyncModel |
|||
|
|||
<AsyncContentSupport RenderHandler="@this"> |
|||
<LoadingContent> |
|||
Please wait... Content is loading... |
|||
</LoadingContent> |
|||
<ChildContent> |
|||
<div>First Component Start</div> |
|||
<SecondLevelComponentAsync Parameter=@AsyncParameter /> |
|||
<div>First Component End</div> |
|||
</ChildContent> |
|||
</AsyncContentSupport> |
@ -0,0 +1,19 @@ |
|||
using blazor_lifetime.Models; |
|||
using Microsoft.AspNetCore.Components; |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace blazor_lifetime.Shared |
|||
{ |
|||
public class FirstLevelAdvancedComponentAsyncModel : AdvancedComponentBase |
|||
{ |
|||
protected string AsyncParameter { get; set; } |
|||
protected override async Task OnComponentMountAsync() |
|||
{ |
|||
Console.WriteLine($"{GetType().Name}.OnComponentMountAsync.Start"); |
|||
await Task.Delay(500); |
|||
AsyncParameter = "value 123"; |
|||
Console.WriteLine($"{GetType().Name}.OnComponentMountAsync.End"); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue