From 2984fa0d2c7b17aea60b720350b1fa83d07934fd Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Wed, 3 Jan 2024 12:03:18 -0500 Subject: [PATCH] Move examples to sample apps --- .../BlazorSample.csproj | 4 ++ .../Components/Pages/Daleks.razor | 23 ++++++++ .../Components/Pages/EmptyContent.razor | 24 +++++++++ .../Pages/PrerenderedCounter1.razor | 27 ++++++++++ .../Pages/PrerenderedCounter2.razor | 54 +++++++++++++++++++ .../Components/Pages/PromotionGrid.razor | 26 +++++++++ .../Components/Pages/VirtualizedTable.razor | 34 ++++++++++++ 8.0/BlazorSample_BlazorWebApp/Dalek.cs | 9 ++++ 8.0/BlazorSample_BlazorWebApp/Program.cs | 3 ++ .../WeatherForecastService.cs | 45 +++++++++------- .../BlazorSample.csproj | 1 + 8.0/BlazorSample_WebAssembly/Dalek.cs | 9 ++++ .../Pages/Daleks.razor | 23 ++++++++ .../Pages/EmptyContent.razor | 24 +++++++++ .../Pages/PromotionGrid.razor | 26 +++++++++ .../Pages/VirtualizedTable.razor | 34 ++++++++++++ 8.0/BlazorSample_WebAssembly/Program.cs | 2 + 17 files changed, 350 insertions(+), 18 deletions(-) create mode 100644 8.0/BlazorSample_BlazorWebApp/Components/Pages/Daleks.razor create mode 100644 8.0/BlazorSample_BlazorWebApp/Components/Pages/EmptyContent.razor create mode 100644 8.0/BlazorSample_BlazorWebApp/Components/Pages/PrerenderedCounter1.razor create mode 100644 8.0/BlazorSample_BlazorWebApp/Components/Pages/PrerenderedCounter2.razor create mode 100644 8.0/BlazorSample_BlazorWebApp/Components/Pages/PromotionGrid.razor create mode 100644 8.0/BlazorSample_BlazorWebApp/Components/Pages/VirtualizedTable.razor create mode 100644 8.0/BlazorSample_BlazorWebApp/Dalek.cs create mode 100644 8.0/BlazorSample_WebAssembly/Dalek.cs create mode 100644 8.0/BlazorSample_WebAssembly/Pages/Daleks.razor create mode 100644 8.0/BlazorSample_WebAssembly/Pages/EmptyContent.razor create mode 100644 8.0/BlazorSample_WebAssembly/Pages/PromotionGrid.razor create mode 100644 8.0/BlazorSample_WebAssembly/Pages/VirtualizedTable.razor diff --git a/8.0/BlazorSample_BlazorWebApp/BlazorSample.csproj b/8.0/BlazorSample_BlazorWebApp/BlazorSample.csproj index 86dfd32b..0987e9c0 100644 --- a/8.0/BlazorSample_BlazorWebApp/BlazorSample.csproj +++ b/8.0/BlazorSample_BlazorWebApp/BlazorSample.csproj @@ -10,4 +10,8 @@ + + + + diff --git a/8.0/BlazorSample_BlazorWebApp/Components/Pages/Daleks.razor b/8.0/BlazorSample_BlazorWebApp/Components/Pages/Daleks.razor new file mode 100644 index 00000000..9a601703 --- /dev/null +++ b/8.0/BlazorSample_BlazorWebApp/Components/Pages/Daleks.razor @@ -0,0 +1,23 @@ +@page "/daleks" + +Daleks + +

Root-level Cascading Value Example

+ + + +

+ Dalek© Terry Nation
+ Doctor Who© BBC +

+ +@code { + [CascadingParameter] + public Dalek? Dalek { get; set; } + + [CascadingParameter(Name = "AlphaGroup")] + public Dalek? AlphaGroupDalek { get; set; } +} diff --git a/8.0/BlazorSample_BlazorWebApp/Components/Pages/EmptyContent.razor b/8.0/BlazorSample_BlazorWebApp/Components/Pages/EmptyContent.razor new file mode 100644 index 00000000..b48c4099 --- /dev/null +++ b/8.0/BlazorSample_BlazorWebApp/Components/Pages/EmptyContent.razor @@ -0,0 +1,24 @@ +@page "/empty-content" + +Empty Content + +

Empty Content Example

+ + + +

+ @context +

+
+ +

+ There are no strings to display. +

+
+
+ +@code { + private List? stringList; + + protected override void OnInitialized() => stringList ??= new(); +} diff --git a/8.0/BlazorSample_BlazorWebApp/Components/Pages/PrerenderedCounter1.razor b/8.0/BlazorSample_BlazorWebApp/Components/Pages/PrerenderedCounter1.razor new file mode 100644 index 00000000..2cf516c6 --- /dev/null +++ b/8.0/BlazorSample_BlazorWebApp/Components/Pages/PrerenderedCounter1.razor @@ -0,0 +1,27 @@ +@page "/prerendered-counter-1" +@rendermode @(new InteractiveServerRenderMode(prerender: true)) +@inject ILogger Logger + +Prerendered Counter 1 + +

Prerendered Counter 1

+ +

Current count: @currentCount

+ + + +@code { + private int currentCount; + private Random r = new Random(); + + protected override void OnInitialized() + { + currentCount = r.Next(100); + Logger.LogInformation("currentCount set to {Count}", currentCount); + } + + private void IncrementCount() + { + currentCount++; + } +} diff --git a/8.0/BlazorSample_BlazorWebApp/Components/Pages/PrerenderedCounter2.razor b/8.0/BlazorSample_BlazorWebApp/Components/Pages/PrerenderedCounter2.razor new file mode 100644 index 00000000..41c9ccf6 --- /dev/null +++ b/8.0/BlazorSample_BlazorWebApp/Components/Pages/PrerenderedCounter2.razor @@ -0,0 +1,54 @@ +@page "/prerendered-counter-2" +@rendermode @(new InteractiveServerRenderMode(prerender: true)) +@implements IDisposable +@inject ILogger Logger +@inject PersistentComponentState ApplicationState + +Prerendered Counter 2 + +

Prerendered Counter 2

+ +

Current count: @currentCount

+ + + +@code { + private int currentCount; + private Random r = new Random(); + private PersistingComponentStateSubscription persistingSubscription; + + protected override void OnInitialized() + { + persistingSubscription = + ApplicationState.RegisterOnPersisting(PersistCount); + + if (!ApplicationState.TryTakeFromJson( + "count", out var restoredCount)) + { + currentCount = r.Next(100); + Logger.LogInformation("currentCount set to {Count}", currentCount); + } + else + { + currentCount = restoredCount!; + Logger.LogInformation("currentCount restored to {Count}", currentCount); + } + } + + private Task PersistCount() + { + ApplicationState.PersistAsJson("count", currentCount); + + return Task.CompletedTask; + } + + void IDisposable.Dispose() + { + persistingSubscription.Dispose(); + } + + private void IncrementCount() + { + currentCount++; + } +} diff --git a/8.0/BlazorSample_BlazorWebApp/Components/Pages/PromotionGrid.razor b/8.0/BlazorSample_BlazorWebApp/Components/Pages/PromotionGrid.razor new file mode 100644 index 00000000..be50d196 --- /dev/null +++ b/8.0/BlazorSample_BlazorWebApp/Components/Pages/PromotionGrid.razor @@ -0,0 +1,26 @@ +@page "/promotion-grid" +@using Microsoft.AspNetCore.Components.QuickGrid + +Promotion Grid + +

Promotion Grid Example

+ + + + + + + +@code { + private record Person(int PersonId, string Name, DateOnly PromotionDate); + + private IQueryable people = new[] + { + new Person(10895, "Jean Martin", new DateOnly(1985, 3, 16)), + new Person(10944, "António Langa", new DateOnly(1991, 12, 1)), + new Person(11203, "Julie Smith", new DateOnly(1958, 10, 10)), + new Person(11205, "Nur Sari", new DateOnly(1922, 4, 27)), + new Person(11898, "Jose Hernandez", new DateOnly(2011, 5, 3)), + new Person(12130, "Kenji Sato", new DateOnly(2004, 1, 9)), + }.AsQueryable(); +} diff --git a/8.0/BlazorSample_BlazorWebApp/Components/Pages/VirtualizedTable.razor b/8.0/BlazorSample_BlazorWebApp/Components/Pages/VirtualizedTable.razor new file mode 100644 index 00000000..ff557f46 --- /dev/null +++ b/8.0/BlazorSample_BlazorWebApp/Components/Pages/VirtualizedTable.razor @@ -0,0 +1,34 @@ +@page "/virtualized-table" + +Virtualized Table + + + + + +

Virtualized Table Example

+ + + + + + + + + + + + + + + + +
ItemAnother column
Item @contextAnother value
+ +@code { + private List fixedItems = Enumerable.Range(0, 1000).ToList(); +} diff --git a/8.0/BlazorSample_BlazorWebApp/Dalek.cs b/8.0/BlazorSample_BlazorWebApp/Dalek.cs new file mode 100644 index 00000000..88247178 --- /dev/null +++ b/8.0/BlazorSample_BlazorWebApp/Dalek.cs @@ -0,0 +1,9 @@ +// "Dalek" ©Terry Nation https://www.imdb.com/name/nm0622334/ +// "Doctor Who" ©BBC https://www.bbc.co.uk/programmes/b006q2x0 + +namespace BlazorSample; + +public class Dalek +{ + public int Units { get; set; } +} diff --git a/8.0/BlazorSample_BlazorWebApp/Program.cs b/8.0/BlazorSample_BlazorWebApp/Program.cs index 6c9a7bcc..025b1e38 100644 --- a/8.0/BlazorSample_BlazorWebApp/Program.cs +++ b/8.0/BlazorSample_BlazorWebApp/Program.cs @@ -15,6 +15,9 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddHttpClient(); +builder.Services.AddCascadingValue(sp => new Dalek { Units = 123 }); +builder.Services.AddCascadingValue("AlphaGroup", sp => new Dalek { Units = 456 }); +builder.Services.AddMemoryCache(); var app = builder.Build(); diff --git a/8.0/BlazorSample_BlazorWebApp/WeatherForecastService.cs b/8.0/BlazorSample_BlazorWebApp/WeatherForecastService.cs index 38d354c8..a7b5cce8 100644 --- a/8.0/BlazorSample_BlazorWebApp/WeatherForecastService.cs +++ b/8.0/BlazorSample_BlazorWebApp/WeatherForecastService.cs @@ -1,28 +1,37 @@ +using Microsoft.Extensions.Caching.Memory; + namespace BlazorSample; -public class WeatherForecastService +public class WeatherForecastService(IMemoryCache memoryCache) { - private static readonly string[] Summaries = + private static readonly string[] summaries = [ - "Freezing", - "Bracing", - "Chilly", - "Cool", - "Mild", - "Warm", - "Balmy", - "Hot", - "Sweltering", - "Scorching" + "Freezing", "Bracing", "Chilly", "Cool", "Mild", + "Warm", "Balmy", "Hot", "Sweltering", "Scorching" ]; - public Task GetForecastAsync(DateOnly startDate) + public IMemoryCache MemoryCache { get; } = memoryCache; + + public Task GetForecastAsync(DateOnly startDate) { - return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast + return MemoryCache.GetOrCreateAsync(startDate, async e => { - Date = startDate.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }).ToArray()); + e.SetOptions(new MemoryCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = + TimeSpan.FromSeconds(30) + }); + + var rng = new Random(); + + await Task.Delay(TimeSpan.FromSeconds(10)); + + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = startDate.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = summaries[rng.Next(summaries.Length)] + }).ToArray(); + }); } } diff --git a/8.0/BlazorSample_WebAssembly/BlazorSample.csproj b/8.0/BlazorSample_WebAssembly/BlazorSample.csproj index de584843..e5385c6f 100644 --- a/8.0/BlazorSample_WebAssembly/BlazorSample.csproj +++ b/8.0/BlazorSample_WebAssembly/BlazorSample.csproj @@ -7,6 +7,7 @@ + diff --git a/8.0/BlazorSample_WebAssembly/Dalek.cs b/8.0/BlazorSample_WebAssembly/Dalek.cs new file mode 100644 index 00000000..88247178 --- /dev/null +++ b/8.0/BlazorSample_WebAssembly/Dalek.cs @@ -0,0 +1,9 @@ +// "Dalek" ©Terry Nation https://www.imdb.com/name/nm0622334/ +// "Doctor Who" ©BBC https://www.bbc.co.uk/programmes/b006q2x0 + +namespace BlazorSample; + +public class Dalek +{ + public int Units { get; set; } +} diff --git a/8.0/BlazorSample_WebAssembly/Pages/Daleks.razor b/8.0/BlazorSample_WebAssembly/Pages/Daleks.razor new file mode 100644 index 00000000..9a601703 --- /dev/null +++ b/8.0/BlazorSample_WebAssembly/Pages/Daleks.razor @@ -0,0 +1,23 @@ +@page "/daleks" + +Daleks + +

Root-level Cascading Value Example

+ +
    +
  • Dalek Units: @Dalek?.Units
  • +
  • Alpha Group Dalek Units: @AlphaGroupDalek?.Units
  • +
+ +

+ Dalek© Terry Nation
+ Doctor Who© BBC +

+ +@code { + [CascadingParameter] + public Dalek? Dalek { get; set; } + + [CascadingParameter(Name = "AlphaGroup")] + public Dalek? AlphaGroupDalek { get; set; } +} diff --git a/8.0/BlazorSample_WebAssembly/Pages/EmptyContent.razor b/8.0/BlazorSample_WebAssembly/Pages/EmptyContent.razor new file mode 100644 index 00000000..b48c4099 --- /dev/null +++ b/8.0/BlazorSample_WebAssembly/Pages/EmptyContent.razor @@ -0,0 +1,24 @@ +@page "/empty-content" + +Empty Content + +

Empty Content Example

+ + + +

+ @context +

+
+ +

+ There are no strings to display. +

+
+
+ +@code { + private List? stringList; + + protected override void OnInitialized() => stringList ??= new(); +} diff --git a/8.0/BlazorSample_WebAssembly/Pages/PromotionGrid.razor b/8.0/BlazorSample_WebAssembly/Pages/PromotionGrid.razor new file mode 100644 index 00000000..be50d196 --- /dev/null +++ b/8.0/BlazorSample_WebAssembly/Pages/PromotionGrid.razor @@ -0,0 +1,26 @@ +@page "/promotion-grid" +@using Microsoft.AspNetCore.Components.QuickGrid + +Promotion Grid + +

Promotion Grid Example

+ + + + + + + +@code { + private record Person(int PersonId, string Name, DateOnly PromotionDate); + + private IQueryable people = new[] + { + new Person(10895, "Jean Martin", new DateOnly(1985, 3, 16)), + new Person(10944, "António Langa", new DateOnly(1991, 12, 1)), + new Person(11203, "Julie Smith", new DateOnly(1958, 10, 10)), + new Person(11205, "Nur Sari", new DateOnly(1922, 4, 27)), + new Person(11898, "Jose Hernandez", new DateOnly(2011, 5, 3)), + new Person(12130, "Kenji Sato", new DateOnly(2004, 1, 9)), + }.AsQueryable(); +} diff --git a/8.0/BlazorSample_WebAssembly/Pages/VirtualizedTable.razor b/8.0/BlazorSample_WebAssembly/Pages/VirtualizedTable.razor new file mode 100644 index 00000000..ff557f46 --- /dev/null +++ b/8.0/BlazorSample_WebAssembly/Pages/VirtualizedTable.razor @@ -0,0 +1,34 @@ +@page "/virtualized-table" + +Virtualized Table + + + + + +

Virtualized Table Example

+ + + + + + + + + + + + + + + + +
ItemAnother column
Item @contextAnother value
+ +@code { + private List fixedItems = Enumerable.Range(0, 1000).ToList(); +} diff --git a/8.0/BlazorSample_WebAssembly/Program.cs b/8.0/BlazorSample_WebAssembly/Program.cs index 12749ea7..328baf06 100644 --- a/8.0/BlazorSample_WebAssembly/Program.cs +++ b/8.0/BlazorSample_WebAssembly/Program.cs @@ -15,6 +15,8 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddCascadingValue(sp => new Dalek { Units = 123 }); +builder.Services.AddCascadingValue("AlphaGroup", sp => new Dalek { Units = 456 }); var vehicleData = new Dictionary() {