Skip to content

Commit d59724c

Browse files
feat: upgrade blazor client sample to net8.0 (#819)
Co-authored-by: Chris Pulman <[email protected]>
1 parent 5376a9b commit d59724c

File tree

10 files changed

+115
-125
lines changed

10 files changed

+115
-125
lines changed

Directory.build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<LangVersion>latest</LangVersion>
88
<EnableNETAnalyzers>True</EnableNETAnalyzers>
99
<AnalysisLevel>latest</AnalysisLevel>
10-
<ReactiveUIVersion>19.*</ReactiveUIVersion>
10+
<ReactiveUIVersion>20.*</ReactiveUIVersion>
1111
</PropertyGroup>
1212
<PropertyGroup>
1313
<SolutionDir Condition="'$(SolutionDir)' == ''">$(MSBuildThisFileDirectory)</SolutionDir>

blazor/ClientSideExample/ClientSideExample/ClientSideExample.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<RazorLangVersion>3.0</RazorLangVersion>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.14" />
10-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.0" PrivateAssets="all" />
11-
<PackageReference Include="System.Net.Http.Json" Version="7.0.1" />
9+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.4" />
10+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.4" PrivateAssets="all" />
11+
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
1212
<PackageReference Include="ReactiveUI.Blazor" Version="$(ReactiveUIVersion)" />
1313
</ItemGroup>
1414

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using System;
22

3-
namespace ClientSideExample.Data
3+
namespace ClientSideExample.Data;
4+
5+
public class WeatherForecast
46
{
5-
public class WeatherForecast
6-
{
7-
public DateTime Date { get; set; }
7+
public DateTime Date { get; set; }
88

9-
public int TemperatureC { get; set; }
9+
public int TemperatureC { get; set; }
1010

11-
public string Summary { get; set; }
11+
public string Summary { get; set; }
1212

13-
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
14-
}
15-
}
13+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
14+
}

blazor/ClientSideExample/ClientSideExample/Program.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
66
using Microsoft.Extensions.DependencyInjection;
77

8-
namespace ClientSideExample
8+
namespace ClientSideExample;
9+
10+
public static class Program
911
{
10-
public static class Program
12+
public static async Task Main(string[] args)
1113
{
12-
public static async Task Main(string[] args)
13-
{
14-
var builder = WebAssemblyHostBuilder.CreateDefault(args);
15-
builder.RootComponents.Add<App>("app");
14+
var builder = WebAssemblyHostBuilder.CreateDefault(args);
15+
builder.RootComponents.Add<App>("app");
1616

17-
builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
18-
builder.Services.AddSingleton<FetchDataViewModel>();
17+
builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
18+
builder.Services.AddSingleton<FetchDataViewModel>();
1919

20-
await builder.Build().RunAsync();
21-
}
20+
await builder.Build().RunAsync();
2221
}
2322
}

blazor/ClientSideExample/ClientSideExample/ViewModels/CounterViewModel.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@
22
using System.Threading.Tasks;
33
using ReactiveUI;
44

5-
namespace ClientSideExample.ViewModels
5+
namespace ClientSideExample.ViewModels;
6+
7+
public class CounterViewModel : ReactiveObject
68
{
7-
public class CounterViewModel : ReactiveObject
8-
{
9-
private int _currentCount;
9+
private int _currentCount;
1010

11-
private readonly ObservableAsPropertyHelper<int> _count;
11+
private readonly ObservableAsPropertyHelper<int> _count;
1212

13-
public CounterViewModel()
14-
{
13+
public CounterViewModel()
14+
{
1515
Increment = ReactiveCommand.CreateFromTask(IncrementCount);
1616

1717
_count = Increment.ToProperty(this, x => x.CurrentCount, scheduler: RxApp.MainThreadScheduler);
1818
}
1919

20-
public int CurrentCount => _count.Value;
20+
public int CurrentCount => _count.Value;
2121

2222

23-
public ReactiveCommand<Unit, int> Increment { get; }
23+
public ReactiveCommand<Unit, int> Increment { get; }
2424

25-
private Task<int> IncrementCount()
26-
{
25+
private Task<int> IncrementCount()
26+
{
2727
_currentCount++;
2828
return Task.FromResult(_currentCount);
2929
}
30-
}
31-
}
30+
}

blazor/ClientSideExample/ClientSideExample/ViewModels/FetchDataViewModel.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,29 @@
66
using ReactiveUI;
77

88

9-
namespace ClientSideExample.ViewModels
10-
{
11-
public class FetchDataViewModel : ReactiveObject
12-
{
13-
private readonly ObservableAsPropertyHelper<WeatherForecast[]> _forecasts;
9+
namespace ClientSideExample.ViewModels;
1410

15-
private readonly HttpClient _http;
16-
public FetchDataViewModel(HttpClient http)
17-
{
18-
_http = http;
19-
LoadForecasts = ReactiveCommand.CreateFromTask(LoadWeatherForecastsAsync);
11+
public class FetchDataViewModel : ReactiveObject
12+
{
13+
private readonly ObservableAsPropertyHelper<WeatherForecast[]> _forecasts;
2014

21-
_forecasts = LoadForecasts.ToProperty(this, x => x.Forecasts, scheduler: RxApp.MainThreadScheduler);
22-
}
15+
private readonly HttpClient _http;
16+
public FetchDataViewModel(HttpClient http)
17+
{
18+
_http = http;
19+
LoadForecasts = ReactiveCommand.CreateFromTask(LoadWeatherForecastsAsync);
2320

24-
public ReactiveCommand<Unit, WeatherForecast[]> LoadForecasts { get; }
21+
_forecasts = LoadForecasts.ToProperty(this, x => x.Forecasts, scheduler: RxApp.MainThreadScheduler);
22+
}
2523

26-
public WeatherForecast[] Forecasts => _forecasts.Value;
24+
public ReactiveCommand<Unit, WeatherForecast[]> LoadForecasts { get; }
2725

26+
public WeatherForecast[] Forecasts => _forecasts.Value;
2827

29-
private async Task<WeatherForecast[]> LoadWeatherForecastsAsync()
30-
{
31-
return await _http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
32-
}
3328

29+
private async Task<WeatherForecast[]> LoadWeatherForecastsAsync()
30+
{
31+
return await _http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
3432
}
33+
3534
}

blazor/ClientSideExample/ClientSideExample/ViewModels/GreetingViewModel.cs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,41 @@
22
using System.Reactive.Linq;
33
using ReactiveUI;
44

5-
namespace ClientSideExample.ViewModels
5+
namespace ClientSideExample.ViewModels;
6+
7+
public class GreetingViewModel : ReactiveObject
68
{
7-
public class GreetingViewModel : ReactiveObject
8-
{
9-
private string _name;
9+
private string _name;
1010

11-
private readonly ObservableAsPropertyHelper<bool> _canClear;
12-
private readonly ObservableAsPropertyHelper<string> _greeting;
11+
private readonly ObservableAsPropertyHelper<bool> _canClear;
12+
private readonly ObservableAsPropertyHelper<string> _greeting;
1313

14-
public ReactiveCommand<Unit, Unit> Clear { get; }
14+
public ReactiveCommand<Unit, Unit> Clear { get; }
1515

16-
public bool CanClear => _canClear.Value;
16+
public bool CanClear => _canClear.Value;
1717

18-
public string Greeting => _greeting.Value;
19-
20-
public string Name
21-
{
22-
get => _name;
23-
set => this.RaiseAndSetIfChanged(ref _name, value);
24-
}
25-
26-
public GreetingViewModel()
27-
{
28-
var canClear = this.WhenAnyValue(x => x.Name)
29-
.Select(name => !string.IsNullOrEmpty(name));
30-
31-
Clear = ReactiveCommand.Create(
32-
() => { Name = string.Empty; },
33-
canClear);
34-
35-
_canClear = Clear.CanExecute
36-
.ToProperty(this, x => x.CanClear);
37-
38-
_greeting = this.WhenAnyValue(x => x.Name)
39-
.Select(x => string.IsNullOrWhiteSpace(x) ? string.Empty : $"Hello, {x}!")
40-
.ToProperty(this, x => x.Greeting);
41-
}
18+
public string Greeting => _greeting.Value;
19+
20+
public string Name
21+
{
22+
get => _name;
23+
set => this.RaiseAndSetIfChanged(ref _name, value);
24+
}
25+
26+
public GreetingViewModel()
27+
{
28+
var canClear = this.WhenAnyValue(x => x.Name)
29+
.Select(name => !string.IsNullOrEmpty(name));
30+
31+
Clear = ReactiveCommand.Create(
32+
() => { Name = string.Empty; },
33+
canClear);
34+
35+
_canClear = Clear.CanExecute
36+
.ToProperty(this, x => x.CanClear);
37+
38+
_greeting = this.WhenAnyValue(x => x.Name)
39+
.Select(x => string.IsNullOrWhiteSpace(x) ? string.Empty : $"Hello, {x}!")
40+
.ToProperty(this, x => x.Greeting);
4241
}
43-
}
42+
}

blazor/ClientSideExample/ClientSideExample/Views/CounterView.razor.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
using System.Threading.Tasks;
33
using ClientSideExample.ViewModels;
44

5-
namespace ClientSideExample.Views
5+
namespace ClientSideExample.Views;
6+
7+
public partial class CounterView
68
{
7-
public partial class CounterView
9+
public CounterView()
810
{
9-
public CounterView()
10-
{
11-
ViewModel = new CounterViewModel();
12-
}
11+
ViewModel = new CounterViewModel();
12+
}
1313

14-
private async Task IncrementCount()
15-
{
16-
await ViewModel.Increment.Execute().ToTask();
17-
}
14+
private async Task IncrementCount()
15+
{
16+
await ViewModel.Increment.Execute().ToTask();
1817
}
19-
}
18+
}
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
using System.Reactive.Threading.Tasks;
1+
using System.Reactive.Linq;
22
using System.Threading.Tasks;
33
using ClientSideExample.ViewModels;
44
using Microsoft.AspNetCore.Components;
55

6+
namespace ClientSideExample.Views;
67

7-
namespace ClientSideExample.Views
8+
public partial class FetchDataView
89
{
9-
public partial class FetchDataView
10+
[Inject]
11+
public FetchDataViewModel FetchViewModel
1012
{
11-
[Inject]
12-
public FetchDataViewModel FetchViewModel
13-
{
14-
get => ViewModel;
15-
set => ViewModel = value;
16-
17-
}
13+
get => ViewModel;
14+
set => ViewModel = value;
15+
}
1816

19-
protected override async Task OnInitializedAsync()
20-
{
21-
await ViewModel.LoadForecasts.Execute().ToTask();
22-
}
17+
protected override async Task OnInitializedAsync()
18+
{
19+
await ViewModel!.LoadForecasts.Execute();
2320
}
2421
}

blazor/ClientSideExample/ClientSideExample/Views/GreetingView.razor.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
using System.Threading.Tasks;
33
using ClientSideExample.ViewModels;
44

5-
namespace ClientSideExample.Views
5+
namespace ClientSideExample.Views;
6+
7+
public partial class GreetingView
68
{
7-
public partial class GreetingView
9+
public GreetingView()
810
{
9-
public GreetingView()
10-
{
11-
ViewModel = new GreetingViewModel();
12-
}
11+
ViewModel = new GreetingViewModel();
12+
}
1313

14-
public async Task Clear()
15-
{
16-
await ViewModel.Clear.Execute().ToTask();
17-
}
14+
public async Task Clear()
15+
{
16+
await ViewModel.Clear.Execute().ToTask();
1817
}
19-
}
18+
}

0 commit comments

Comments
 (0)