Blazing.Mvvm
1.0.0
See the version list below for details.
dotnet add package Blazing.Mvvm --version 1.0.0
NuGet\Install-Package Blazing.Mvvm -Version 1.0.0
<PackageReference Include="Blazing.Mvvm" Version="1.0.0" />
<PackageVersion Include="Blazing.Mvvm" Version="1.0.0" />
<PackageReference Include="Blazing.Mvvm" />
paket add Blazing.Mvvm --version 1.0.0
#r "nuget: Blazing.Mvvm, 1.0.0"
#:package Blazing.Mvvm@1.0.0
#addin nuget:?package=Blazing.Mvvm&version=1.0.0
#tool nuget:?package=Blazing.Mvvm&version=1.0.0
Blazor WASM Extension for the MVVM CommunityToolkit
This is an expansion of the blazor-mvvm repo by Kelly Adams that implements full MVVM support via the CommunityToolkit.Mvvm.
The library packages the support into a resuable library and includes a new MvvmNavigationManager class and the MvvmNavLink component.
Getting Started
Add the Nuget package to your project
Enable MvvmNavigation support in your
Program.csfile
builder.Services.AddMvvmNavigation();
- Create a
ViewModelinheriting theViewModelBaseclass
public partial class FetchDataViewModel : ViewModelBase
{
[ObservableProperty]
private ObservableCollection<WeatherForecast> _weatherForecasts = new();
public override async Task Loaded()
=> WeatherForecasts = new ObservableCollection<WeatherForecast>(Get());
private static readonly string[] Summaries =
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
public IEnumerable<WeatherForecast> Get()
=> Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
- Register the
ViewModelin yourProgram.csfile
builder.Services.AddTransient<FetchDataViewModel>();
- Create your Page inheriting the
MvvmComponentBase<TViewModel>component
@page "/fetchdata"
@inherits MvvmComponentBase<FetchDataViewModel>
<PageTitle>Weather forecast</PageTitle>
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
@if (!ViewModel.WeatherForecasts.Any())
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in ViewModel.WeatherForecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
- Optionally, modify the
NavMenu.razorto useMvvmNavLinkfor Navigation by ViewModel
<div class="nav-item px-3">
<MvvmNavLink class="nav-link" TViewModel=FetchDataViewModel>
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
</MvvmNavLink>
</div>
Now run the app.
Navigating by ViewModel using the MvvmNavigationManager from code, inject the class into your page or ViewModel, then use the NavigateTo method
mvvmNavigationManager.NavigateTo<FetchDataViewModel>();
The NavigateTo method works the same as the standard Blazor NavigationManager and also support passing of a Relative URL &/or QueryString.
If you are into abstraction, then you can also navigate by interface
mvvmNavigationManager.NavigateTo<ITestNavigationViewModel>();
The same principle worhs with the MvvmNavLink component
<div class="nav-item px-3">
<MvvmNavLink class="nav-link"
TViewModel=ITestNavigationViewModel
Match="NavLinkMatch.All">
<span class="oi oi-calculator" aria-hidden="true"></span>Test
</MvvmNavLink>
</div>
<div class="nav-item px-3">
<MvvmNavLink class="nav-link"
TViewModel=ITestNavigationViewModel
RelativeUri="this is a MvvmNavLink test"
Match="NavLinkMatch.All">
<span class="oi oi-calculator" aria-hidden="true"></span>Test + Params
</MvvmNavLink>
</div>
<div class="nav-item px-3">
<MvvmNavLink class="nav-link"
TViewModel=ITestNavigationViewModel
RelativeUri="?test=this%20is%20a%20MvvmNavLink%20querystring%20test"
Match="NavLinkMatch.All">
<span class="oi oi-calculator" aria-hidden="true"></span>Test + QueryString
</MvvmNavLink>
</div>
<div class="nav-item px-3">
<MvvmNavLink class="nav-link"
TViewModel=ITestNavigationViewModel
RelativeUri="this is a MvvmNvLink test/?test=this%20is%20a%20MvvmNavLink%20querystring%20test"
Match="NavLinkMatch.All">
<span class="oi oi-calculator" aria-hidden="true"></span>Test + Both
</MvvmNavLink>
</div>
How MVVM Works
There are two parts:
- The
ViewModelBase - The
MvvmComponentBase
The MvvmComponentBase handles wiring up the ViewModel to the component.
EditForm Validation and Messaging are also supported. See the sample code for examples of how to use.
How the MVVM Navigation Works
No more magic strings! Strongly-typed navigation is now possible. If the page URI changes, there is no more need to go hunting through your source code to make changes. It is auto-magically resolved at runtime for you!
When the MvvmNavigationManager is initialized by the IOC container as a Singleton, the class will examine all assemblies and internally caches all ViewModels (classes and interfaces) and the page it is associated with. Then when it comes time to navigate, a quick lookup is done and the Blazor NavigationManager is then used to navigate to the correct page. IF any Relative Uri &/or QueryString was passed in via the NavigateTo method call, that is passed too.
Note: The MvvmNavigationManager class is not a total replacement for the Blazor NavigationManager class, only support for MVVM is implemented.
The MvvmNavLink component is based on the balzor Navlink component and has an extra TViewModel and RelativeUri properties. Internally, uses the MvvmNavigationManager to do the navigation.
Support
If you find this library useful, then please consider buying me a coffee ☕.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- CommunityToolkit.Mvvm (>= 8.2.0)
- Microsoft.AspNetCore.Components.Web (>= 7.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial Release