Skip to content

Commit e15a491

Browse files
authored
Move examples to sample apps (#154)
1 parent a00ba45 commit e15a491

17 files changed

+350
-18
lines changed

8.0/BlazorSample_BlazorWebApp/BlazorSample.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@
1010
<Folder Include="Development\unsafe_uploads\" />
1111
</ItemGroup>
1212

13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.AspNetCore.Components.QuickGrid" Version="8.0.0" />
15+
</ItemGroup>
16+
1317
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@page "/daleks"
2+
3+
<PageTitle>Daleks</PageTitle>
4+
5+
<h1>Root-level Cascading Value Example</h1>
6+
7+
<ul>
8+
<li>Dalek Units: @Dalek?.Units</li>
9+
<li>Alpha Group Dalek Units: @AlphaGroupDalek?.Units</li>
10+
</ul>
11+
12+
<p>
13+
Dalek© <a href="https://www.imdb.com/name/nm0622334/">Terry Nation</a><br>
14+
Doctor Who© <a href="https://www.bbc.co.uk/programmes/b006q2x0">BBC</a>
15+
</p>
16+
17+
@code {
18+
[CascadingParameter]
19+
public Dalek? Dalek { get; set; }
20+
21+
[CascadingParameter(Name = "AlphaGroup")]
22+
public Dalek? AlphaGroupDalek { get; set; }
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@page "/empty-content"
2+
3+
<PageTitle>Empty Content</PageTitle>
4+
5+
<h1>Empty Content Example</h1>
6+
7+
<Virtualize Items="@stringList">
8+
<ItemContent>
9+
<p>
10+
@context
11+
</p>
12+
</ItemContent>
13+
<EmptyContent>
14+
<p>
15+
There are no strings to display.
16+
</p>
17+
</EmptyContent>
18+
</Virtualize>
19+
20+
@code {
21+
private List<string>? stringList;
22+
23+
protected override void OnInitialized() => stringList ??= new();
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@page "/prerendered-counter-1"
2+
@rendermode @(new InteractiveServerRenderMode(prerender: true))
3+
@inject ILogger<PrerenderedCounter1> Logger
4+
5+
<PageTitle>Prerendered Counter 1</PageTitle>
6+
7+
<h1>Prerendered Counter 1</h1>
8+
9+
<p role="status">Current count: @currentCount</p>
10+
11+
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
12+
13+
@code {
14+
private int currentCount;
15+
private Random r = new Random();
16+
17+
protected override void OnInitialized()
18+
{
19+
currentCount = r.Next(100);
20+
Logger.LogInformation("currentCount set to {Count}", currentCount);
21+
}
22+
23+
private void IncrementCount()
24+
{
25+
currentCount++;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
@page "/prerendered-counter-2"
2+
@rendermode @(new InteractiveServerRenderMode(prerender: true))
3+
@implements IDisposable
4+
@inject ILogger<PrerenderedCounter2> Logger
5+
@inject PersistentComponentState ApplicationState
6+
7+
<PageTitle>Prerendered Counter 2</PageTitle>
8+
9+
<h1>Prerendered Counter 2</h1>
10+
11+
<p role="status">Current count: @currentCount</p>
12+
13+
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
14+
15+
@code {
16+
private int currentCount;
17+
private Random r = new Random();
18+
private PersistingComponentStateSubscription persistingSubscription;
19+
20+
protected override void OnInitialized()
21+
{
22+
persistingSubscription =
23+
ApplicationState.RegisterOnPersisting(PersistCount);
24+
25+
if (!ApplicationState.TryTakeFromJson<int>(
26+
"count", out var restoredCount))
27+
{
28+
currentCount = r.Next(100);
29+
Logger.LogInformation("currentCount set to {Count}", currentCount);
30+
}
31+
else
32+
{
33+
currentCount = restoredCount!;
34+
Logger.LogInformation("currentCount restored to {Count}", currentCount);
35+
}
36+
}
37+
38+
private Task PersistCount()
39+
{
40+
ApplicationState.PersistAsJson("count", currentCount);
41+
42+
return Task.CompletedTask;
43+
}
44+
45+
void IDisposable.Dispose()
46+
{
47+
persistingSubscription.Dispose();
48+
}
49+
50+
private void IncrementCount()
51+
{
52+
currentCount++;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@page "/promotion-grid"
2+
@using Microsoft.AspNetCore.Components.QuickGrid
3+
4+
<PageTitle>Promotion Grid</PageTitle>
5+
6+
<h1>Promotion Grid Example</h1>
7+
8+
<QuickGrid Items="@people">
9+
<PropertyColumn Property="@(p => p.PersonId)" Sortable="true" />
10+
<PropertyColumn Property="@(p => p.Name)" Sortable="true" />
11+
<PropertyColumn Property="@(p => p.PromotionDate)" Format="yyyy-MM-dd" Sortable="true" />
12+
</QuickGrid>
13+
14+
@code {
15+
private record Person(int PersonId, string Name, DateOnly PromotionDate);
16+
17+
private IQueryable<Person> people = new[]
18+
{
19+
new Person(10895, "Jean Martin", new DateOnly(1985, 3, 16)),
20+
new Person(10944, "António Langa", new DateOnly(1991, 12, 1)),
21+
new Person(11203, "Julie Smith", new DateOnly(1958, 10, 10)),
22+
new Person(11205, "Nur Sari", new DateOnly(1922, 4, 27)),
23+
new Person(11898, "Jose Hernandez", new DateOnly(2011, 5, 3)),
24+
new Person(12130, "Kenji Sato", new DateOnly(2004, 1, 9)),
25+
}.AsQueryable();
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@page "/virtualized-table"
2+
3+
<PageTitle>Virtualized Table</PageTitle>
4+
5+
<HeadContent>
6+
<style>
7+
html, body {
8+
overflow-y: scroll
9+
}
10+
</style>
11+
</HeadContent>
12+
13+
<h1>Virtualized Table Example</h1>
14+
15+
<table id="virtualized-table">
16+
<thead style="position: sticky; top: 0; background-color: silver">
17+
<tr>
18+
<th>Item</th>
19+
<th>Another column</th>
20+
</tr>
21+
</thead>
22+
<tbody>
23+
<Virtualize Items="@fixedItems" ItemSize="30" SpacerElement="tr">
24+
<tr @key="context" style="height: 30px;" id="row-@context">
25+
<td>Item @context</td>
26+
<td>Another value</td>
27+
</tr>
28+
</Virtualize>
29+
</tbody>
30+
</table>
31+
32+
@code {
33+
private List<int> fixedItems = Enumerable.Range(0, 1000).ToList();
34+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// "Dalek" ©Terry Nation https://www.imdb.com/name/nm0622334/
2+
// "Doctor Who" ©BBC https://www.bbc.co.uk/programmes/b006q2x0
3+
4+
namespace BlazorSample;
5+
6+
public class Dalek
7+
{
8+
public int Units { get; set; }
9+
}

8.0/BlazorSample_BlazorWebApp/Program.cs

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
builder.Services.AddScoped<IDataAccess, DataAccess>();
1616
builder.Services.AddScoped<IProductRepository, ProductRepository>();
1717
builder.Services.AddHttpClient();
18+
builder.Services.AddCascadingValue(sp => new Dalek { Units = 123 });
19+
builder.Services.AddCascadingValue("AlphaGroup", sp => new Dalek { Units = 456 });
20+
builder.Services.AddMemoryCache();
1821

1922
var app = builder.Build();
2023

Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
1+
using Microsoft.Extensions.Caching.Memory;
2+
13
namespace BlazorSample;
24

3-
public class WeatherForecastService
5+
public class WeatherForecastService(IMemoryCache memoryCache)
46
{
5-
private static readonly string[] Summaries =
7+
private static readonly string[] summaries =
68
[
7-
"Freezing",
8-
"Bracing",
9-
"Chilly",
10-
"Cool",
11-
"Mild",
12-
"Warm",
13-
"Balmy",
14-
"Hot",
15-
"Sweltering",
16-
"Scorching"
9+
"Freezing", "Bracing", "Chilly", "Cool", "Mild",
10+
"Warm", "Balmy", "Hot", "Sweltering", "Scorching"
1711
];
1812

19-
public Task<WeatherForecast[]> GetForecastAsync(DateOnly startDate)
13+
public IMemoryCache MemoryCache { get; } = memoryCache;
14+
15+
public Task<WeatherForecast[]?> GetForecastAsync(DateOnly startDate)
2016
{
21-
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
17+
return MemoryCache.GetOrCreateAsync(startDate, async e =>
2218
{
23-
Date = startDate.AddDays(index),
24-
TemperatureC = Random.Shared.Next(-20, 55),
25-
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
26-
}).ToArray());
19+
e.SetOptions(new MemoryCacheEntryOptions
20+
{
21+
AbsoluteExpirationRelativeToNow =
22+
TimeSpan.FromSeconds(30)
23+
});
24+
25+
var rng = new Random();
26+
27+
await Task.Delay(TimeSpan.FromSeconds(10));
28+
29+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
30+
{
31+
Date = startDate.AddDays(index),
32+
TemperatureC = rng.Next(-20, 55),
33+
Summary = summaries[rng.Next(summaries.Length)]
34+
}).ToArray();
35+
});
2736
}
2837
}

8.0/BlazorSample_WebAssembly/BlazorSample.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Components.QuickGrid" Version="8.0.0" />
1011
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
1112
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.0" PrivateAssets="all" />
1213
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />

8.0/BlazorSample_WebAssembly/Dalek.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// "Dalek" ©Terry Nation https://www.imdb.com/name/nm0622334/
2+
// "Doctor Who" ©BBC https://www.bbc.co.uk/programmes/b006q2x0
3+
4+
namespace BlazorSample;
5+
6+
public class Dalek
7+
{
8+
public int Units { get; set; }
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@page "/daleks"
2+
3+
<PageTitle>Daleks</PageTitle>
4+
5+
<h1>Root-level Cascading Value Example</h1>
6+
7+
<ul>
8+
<li>Dalek Units: @Dalek?.Units</li>
9+
<li>Alpha Group Dalek Units: @AlphaGroupDalek?.Units</li>
10+
</ul>
11+
12+
<p>
13+
Dalek© <a href="https://www.imdb.com/name/nm0622334/">Terry Nation</a><br>
14+
Doctor Who© <a href="https://www.bbc.co.uk/programmes/b006q2x0">BBC</a>
15+
</p>
16+
17+
@code {
18+
[CascadingParameter]
19+
public Dalek? Dalek { get; set; }
20+
21+
[CascadingParameter(Name = "AlphaGroup")]
22+
public Dalek? AlphaGroupDalek { get; set; }
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@page "/empty-content"
2+
3+
<PageTitle>Empty Content</PageTitle>
4+
5+
<h1>Empty Content Example</h1>
6+
7+
<Virtualize Items="@stringList">
8+
<ItemContent>
9+
<p>
10+
@context
11+
</p>
12+
</ItemContent>
13+
<EmptyContent>
14+
<p>
15+
There are no strings to display.
16+
</p>
17+
</EmptyContent>
18+
</Virtualize>
19+
20+
@code {
21+
private List<string>? stringList;
22+
23+
protected override void OnInitialized() => stringList ??= new();
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@page "/promotion-grid"
2+
@using Microsoft.AspNetCore.Components.QuickGrid
3+
4+
<PageTitle>Promotion Grid</PageTitle>
5+
6+
<h1>Promotion Grid Example</h1>
7+
8+
<QuickGrid Items="@people">
9+
<PropertyColumn Property="@(p => p.PersonId)" Sortable="true" />
10+
<PropertyColumn Property="@(p => p.Name)" Sortable="true" />
11+
<PropertyColumn Property="@(p => p.PromotionDate)" Format="yyyy-MM-dd" Sortable="true" />
12+
</QuickGrid>
13+
14+
@code {
15+
private record Person(int PersonId, string Name, DateOnly PromotionDate);
16+
17+
private IQueryable<Person> people = new[]
18+
{
19+
new Person(10895, "Jean Martin", new DateOnly(1985, 3, 16)),
20+
new Person(10944, "António Langa", new DateOnly(1991, 12, 1)),
21+
new Person(11203, "Julie Smith", new DateOnly(1958, 10, 10)),
22+
new Person(11205, "Nur Sari", new DateOnly(1922, 4, 27)),
23+
new Person(11898, "Jose Hernandez", new DateOnly(2011, 5, 3)),
24+
new Person(12130, "Kenji Sato", new DateOnly(2004, 1, 9)),
25+
}.AsQueryable();
26+
}

0 commit comments

Comments
 (0)