Skip to content

Commit a33e8b0

Browse files
committed
Update blazor version + apply some best practices
1 parent ee0fb1a commit a33e8b0

File tree

8 files changed

+58
-61
lines changed

8 files changed

+58
-61
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
powershell -NoProfile -ExecutionPolicy unrestricted -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; &([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) -JSonFile src/global.json -InstallDir ./dotnet -NoPath"
2+
powershell -NoProfile -ExecutionPolicy unrestricted -Command "./dotnet/dotnet.exe workload install wasm-tools"

frameworks/keyed/blazor-wasm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"description": "Blazor WebAssembly demo",
55
"js-framework-benchmark": {
6-
"frameworkVersion": "6.0.100",
6+
"frameworkVersion": "6.0.1",
77
"customURL": "/bundeled-dist/wwwroot/"
88
},
99
"scripts": {

frameworks/keyed/blazor-wasm/src/App.razor

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@
3232
</div>
3333
<table class="table table-hover table-striped test-data">
3434
<tbody id="tbody">
35-
<Virtualize Items="@data" Context="item">
36-
<tr @key="item.Id" class="@((item.Id == selected ? "danger" : ""))">
35+
@foreach (var item in Data)
36+
{
37+
<tr @key="item.Id" class="@((item.Id == Selected ? "danger" : ""))">
3738
<td class="col-md-1">@item.Id</td>
3839
<td class="col-md-4">
39-
<a href="#" @onclick="(_ => Select(item))" @onclick:preventDefault>
40+
<a href="#" @onclick="item.Select" @onclick:preventDefault>
4041
@item.Label
4142
</a>
4243
</td>
4344
<td class="col-md-1">
44-
<a href="#" @onclick="(_ => Delete(item))" @onclick:preventDefault>
45+
<a href="#" @onclick="item.Remove" @onclick:preventDefault>
4546
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
4647
</a>
4748
</td>

frameworks/keyed/blazor-wasm/src/App.razor.cs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,87 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
5-
namespace blazor_wasm
1+
namespace blazor_wasm
62
{
73
public partial class App
84
{
9-
List<Data> data = new List<Data>();
10-
int selected;
11-
int id = 1;
12-
Random random = new Random(0);
5+
public List<Data> Data { get; set; } = new();
6+
public int Selected { get; set; }
137

14-
string[] adjectives = new string[]
8+
private readonly string[] adjectives = new string[]
159
{
1610
"pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", "cheap", "expensive", "fancy"
1711
};
18-
19-
string[] colours = new string[]
12+
13+
private readonly string[] colours = new string[]
2014
{
2115
"red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"
2216
};
23-
24-
string[] nouns = new string[]
17+
18+
private readonly string[] nouns = new string[]
2519
{
2620
"table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"
2721
};
2822

23+
private int _rowId = 1;
24+
2925
List<Data> BuildData(int count = 1000)
3026
{
31-
var result = new List<Data>();
27+
List<Data> result = new(count);
3228
for (int i = 0; i < count; i++)
3329
{
34-
result.Add(new Data
30+
Data data = new()
3531
{
36-
Id = this.id++,
37-
Label = adjectives[this.random.Next(adjectives.Length)] + " " + colours[this.random.Next(colours.Length)] + " " + nouns[this.random.Next(nouns.Length)]
38-
});
32+
Id = _rowId++,
33+
Label = $"{adjectives[Random.Shared.Next(adjectives.Length)]} {colours[Random.Shared.Next(colours.Length)]} {nouns[Random.Shared.Next(nouns.Length)]}",
34+
};
35+
36+
data.Select = (e) => Select(data);
37+
data.Remove = (e) => Remove(data);
38+
result.Add(data);
3939
}
4040

4141
return result;
4242
}
4343

4444
public void Select(Data item)
4545
{
46-
this.selected = item.Id;
46+
Selected = item.Id;
4747
}
4848

49-
void Delete(Data item)
49+
void Remove(Data item)
5050
{
51-
this.data.Remove(item);
51+
Data.Remove(item);
5252
}
5353

5454
void Run()
5555
{
56-
this.data = this.BuildData();
56+
Data = BuildData();
5757
}
5858
void Runlots()
5959
{
60-
this.data = this.BuildData(10000);
60+
Data = BuildData(10000);
6161
}
6262
void Add()
6363
{
64-
this.data.AddRange(this.BuildData(1000));
64+
Data.AddRange(BuildData(1000));
6565
}
6666
void Update()
6767
{
68-
for (var i = 0; i < this.data.Count; i += 10)
68+
for (var i = 0; i < Data.Count; i += 10)
6969
{
70-
this.data[i].Label += " !!!";
70+
Data[i].Label += " !!!";
7171
}
7272
}
7373
void Clear()
7474
{
75-
this.data = new List<Data>();
76-
this.selected = 0;
75+
Data.Clear();
76+
Selected = 0;
7777
}
7878
void SwapRows()
7979
{
80-
if (this.data.Count > 998)
80+
if (Data.Count > 998)
8181
{
82-
var a = this.data[1];
83-
this.data[1] = this.data[998];
84-
this.data[998] = a;
82+
var a = Data[1];
83+
Data[1] = Data[998];
84+
Data[998] = a;
8585
}
8686
}
8787
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
using Microsoft.AspNetCore.Components.Web;
2+
13
namespace blazor_wasm
24
{
35
public class Data
46
{
5-
public int Id { get; set; }
6-
public string Label { get; set; }
7+
public int Id { get; set; } = default!;
8+
public string Label { get; set; } = default!;
9+
10+
public Action<MouseEventArgs> Remove { get; set; } = default!;
11+
public Action<MouseEventArgs> Select { get; set; } = default!;
712
}
813
}
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
using System.Threading.Tasks;
1+
using blazor_wasm;
22
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
33

4-
namespace blazor_wasm
5-
{
6-
public class Program
7-
{
8-
public static async Task Main(string[] args)
9-
{
10-
var builder = WebAssemblyHostBuilder.CreateDefault(args);
11-
builder.RootComponents.Add<App>("app");
4+
var builder = WebAssemblyHostBuilder.CreateDefault(args);
5+
builder.RootComponents.Add<App>("app");
126

13-
await builder.Build().RunAsync();
14-
}
15-
}
16-
}
7+
await builder.Build().RunAsync();

frameworks/keyed/blazor-wasm/src/blazor-wasm.csproj

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
5-
</PropertyGroup>
6-
7-
<PropertyGroup Condition="'$(Configuration)'=='Release'">
8-
<RunAOTCompilation>True</RunAOTCompilation>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<BlazorEnableTimeZoneSupport>false</BlazorEnableTimeZoneSupport>
8+
<InvariantGlobalization>true</InvariantGlobalization>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.0" />
13-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.0" PrivateAssets="all" />
14-
<PackageReference Include="System.Net.Http.Json" Version="6.0.0" />
12+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.1" />
13+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.1" PrivateAssets="all" />
1514
</ItemGroup>
1615

1716
</Project>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "6.0.100"
3+
"version": "6.0.101"
44
}
55
}

0 commit comments

Comments
 (0)