This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95cc86c
commit 6b6d4e9
Showing
14 changed files
with
228 additions
and
163 deletions.
There are no files selected for viewing
29 changes: 15 additions & 14 deletions
29
...ampleApp/BitzArt.Blazor.MVVM.SampleApp.Client/BitzArt.Blazor.MVVM.SampleApp.Client.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> | ||
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile> | ||
<StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile> | ||
<StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode> | ||
<RootNamespace>BitzArt.Blazor.MVVM.SampleApp.Client</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.2" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\BitzArt.Blazor.MVVM\BitzArt.Blazor.MVVM.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\BitzArt.Blazor.MVVM\BitzArt.Blazor.MVVM.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
8 changes: 8 additions & 0 deletions
8
...tzArt.Blazor.MVVM.SampleApp/BitzArt.Blazor.MVVM.SampleApp.Client/Components/Counter.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@inherits ComponentBase<CounterViewModel> | ||
|
||
@if (ViewModel.State is not null) | ||
{ | ||
<p role="status">Current count: @ViewModel.State.Count</p> | ||
|
||
<button class="btn btn-primary" @onclick="ViewModel.IncrementCount">Click me</button> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 17 additions & 20 deletions
37
...or.MVVM.SampleApp/BitzArt.Blazor.MVVM.SampleApp.Client/ViewModels/CounterPageViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,32 @@ | ||
namespace BitzArt.Blazor.MVVM.SampleApp; | ||
|
||
public class CounterPageViewModel : ComponentViewModel<CounterPageViewModelState> | ||
public class CounterPageViewModel( | ||
CounterViewModel counter1ViewModel, | ||
CounterViewModel counter2ViewModel, | ||
RenderingEnvironment renderingEnvironment) | ||
: ViewModel<CounterPageViewModelState> | ||
{ | ||
private readonly Timer _timer; | ||
|
||
public CounterPageViewModel() | ||
{ | ||
_timer = new Timer(TimerIncrementCount, null, 1000, 1000); | ||
} | ||
|
||
private void TimerIncrementCount(object? state) | ||
{ | ||
State.Count++; | ||
StateHasChanged(); | ||
} | ||
public CounterViewModel Counter1ViewModel { get; } = counter1ViewModel; | ||
public CounterViewModel Counter2ViewModel { get; } = counter2ViewModel; | ||
|
||
public override void InitializeState() | ||
{ | ||
State.Count = 0; | ||
State.Text = $"ViewModel State initialized on: {RenderingEnvironment}"; | ||
State.Text = $"ViewModel State initialized on: {renderingEnvironment}"; | ||
|
||
OnStateRestored(); | ||
Counter2ViewModel.State!.Count += 100; | ||
} | ||
|
||
public void IncrementCount() | ||
public override void OnStateRestored() | ||
{ | ||
State.Count++; | ||
Counter1ViewModel.State = State.Counter1State; | ||
Counter2ViewModel.State = State.Counter2State; | ||
} | ||
} | ||
|
||
public class CounterPageViewModelState | ||
{ | ||
public int? Count { get; set; } = null; | ||
|
||
public string? Text { get; set; } = "State not initialized"; | ||
public string Text { get; set; } = "State not initialized"; | ||
public CounterState Counter1State { get; set; } = new(); | ||
public CounterState Counter2State { get; set; } = new(); | ||
} |
34 changes: 34 additions & 0 deletions
34
...Blazor.MVVM.SampleApp/BitzArt.Blazor.MVVM.SampleApp.Client/ViewModels/CounterViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace BitzArt.Blazor.MVVM.SampleApp; | ||
|
||
public class CounterViewModel : ViewModel | ||
{ | ||
public CounterState? State { get; set; } | ||
|
||
private readonly Timer _timer; | ||
|
||
public CounterViewModel() | ||
{ | ||
_timer = new Timer(TimerIncrementCount, null, 1000, 1000); | ||
} | ||
|
||
private void TimerIncrementCount(object? state) | ||
{ | ||
if (State is null) return; | ||
|
||
State.Count++; | ||
StateHasChanged(); | ||
} | ||
|
||
public void IncrementCount() | ||
{ | ||
if (State is null) return; | ||
|
||
State.Count++; | ||
StateHasChanged(); | ||
} | ||
} | ||
|
||
public class CounterState | ||
{ | ||
public int Count { get; set; } = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 10 additions & 9 deletions
19
....Blazor.MVVM.SampleApp/BitzArt.Blazor.MVVM.SampleApp/BitzArt.Blazor.MVVM.SampleApp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<RootNamespace>BitzArt.Blazor.MVVM.SampleApp</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\BitzArt.Blazor.MVVM.SampleApp.Client\BitzArt.Blazor.MVVM.SampleApp.Client.csproj" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.2" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\BitzArt.Blazor.MVVM.SampleApp.Client\BitzArt.Blazor.MVVM.SampleApp.Client.csproj" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/BitzArt.Blazor.MVVM/Models/ComponentBase{TViewModel}.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.JSInterop; | ||
|
||
namespace BitzArt.Blazor.MVVM; | ||
|
||
public abstract class ComponentBase<TViewModel> : ComponentBase, IStateComponent | ||
where TViewModel : ViewModel | ||
{ | ||
[Parameter, EditorRequired] | ||
public TViewModel ViewModel { get; set; } = null!; | ||
|
||
[Inject] | ||
protected IServiceProvider ServiceProvider { get; set; } = null!; | ||
|
||
[Inject] | ||
protected IJSRuntime Js { get; set; } = default!; | ||
|
||
[Inject] | ||
protected RenderingEnvironment RenderingEnvironment { get; set; } = null!; | ||
|
||
/// <summary> | ||
/// Navigation manager. | ||
/// </summary> | ||
[Inject] | ||
protected NavigationManager NavigationManager { get; set; } = null!; | ||
|
||
/// <summary> | ||
/// Method invoked when the component is ready to start, having received its initial | ||
/// parameters from its parent in the render tree. Override this method if you will | ||
/// perform an asynchronous operation and want the component to refresh when that | ||
/// operation is completed. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing any asynchronous operation.</returns> | ||
protected override async Task OnInitializedAsync() | ||
{ | ||
await base.OnInitializedAsync(); | ||
ViewModel.OnStateHasChanged += () => InvokeAsync(StateHasChanged); | ||
await RestoreStateAsync(); | ||
} | ||
|
||
protected virtual Task RestoreStateAsync() => Task.CompletedTask; | ||
|
||
void IStateComponent.StateHasChanged() => InvokeAsync(StateHasChanged); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.