MvvmBlazor 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package MvvmBlazor --version 1.0.5
NuGet\Install-Package MvvmBlazor -Version 1.0.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MvvmBlazor" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MvvmBlazor --version 1.0.5
#r "nuget: MvvmBlazor, 1.0.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MvvmBlazor as a Cake Addin
#addin nuget:?package=MvvmBlazor&version=1.0.5

// Install MvvmBlazor as a Cake Tool
#tool nuget:?package=MvvmBlazor&version=1.0.5

MvvmBlazor

Build Status NuGet

A lightweight Blazor MVVM Library. The main goal is to achieve a MVVM Pattern in Blazor and Serverside Blazor. Plus, it aims to mitigate the need to manually force the component to rerender if binding values have changed changed without user interaction.

Get started

MvvmBlazor is available on NuGet. You will need .NET Core 3.0 preview-5 or later to use this library.

Usage

Components

Components need to inherit the base class MvvmBlazor.Components.ComponentBaseMvvm.
Layout components need to inherit the base class MvvmBlazor.Components.LayoutComponentBaseMvvm.

BindingSource

The binding source is the default source object a binding will be made to. It needs to be set by overriding the SetBindingContext method:

protected override ViewModelBase SetBindingContext()
{
    return FetchDataViewModel;
}

The BindingSource will be disposed automatically when the component is disposed.

Bindings

Bindings are achieved via the Bind method in the component. If the value of the bound property has changed the component will be told to rerender automatically.

@if (Bind(() => FetchDataViewModel.Forecasts) == null)
{
    <p>
        <em>Loading...</em>
    </p>
}

@foreach (var forecast in Bind(() => FetchDataViewModel.Forecasts))
{
    <tr>
        <td>@forecast.Date.ToShortDateString()</td>
        <td>@forecast.TemperatureC</td>
        <td>@forecast.TemperatureF</td>
        <td>@forecast.Summary</td>
    </tr>
}

Bindings can also be done by specifying the binding source explicitly:

@if (Bind(FetchDataViewModel, () => FetchDataViewModel.Forecasts) == null)
{
    <p>
        <em>Loading...</em>
    </p>
}

@foreach (var forecast in Bind(FetchDataViewModel, () => FetchDataViewModel.Forecasts))
{
    <tr>
        <td>@forecast.Date.ToShortDateString()</td>
        <td>@forecast.TemperatureC</td>
        <td>@forecast.TemperatureF</td>
        <td>@forecast.Summary</td>
    </tr>
}

Bound view models are also disposed when the component is disposed.

They are not suited for real two-way bindings (e.g. for inputs). Two-way bindings can be done in the usual way by binding to the property directly.

ViewModel

View models need to inherit the base class MvvmBlazor.ViewModel.ViewModelBase.

Property implementation

Bindable properties need to raise the PropertyChanged event on the ViewModel.

The Set-Method is performing an equality check and is raising this event if needed. An example implementation could look like this:

private int _currentCount;
public int CurrentCount
{
    get => _currentCount;
    set => Set(ref _currentCount, value);
}
Lifecycle methods

View models have access to the same lifecycle methods as a component. They are documented here.

Dispose

View models are implementing the IDisposable pattern of Blazor. Inside ViewModels, Disposing is achieved by overriding the Cleanup method. It will be called when the component is disposed.

Examples

Examples for Blazor and Serverside Blazor can be found here.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on MvvmBlazor:

Package Downloads
Yamf.Core.Themed.Blazor

Package Description

Yamf.Authentication.Themed.Blazor

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.6 25,874 6/11/2022
6.0.5 483 6/4/2022
6.0.4 2,268 5/1/2022
6.0.3 2,379 3/5/2022
6.0.2 422 3/4/2022
6.0.1 410 3/3/2022
6.0.0 439 3/1/2022
2.0.0 15,504 2/22/2021
1.1.7 1,056 12/5/2020
1.1.6 874 9/20/2020
1.1.5 2,275 5/20/2020
1.1.4 660 10/12/2019
1.1.3 530 9/27/2019
1.1.2 411 9/25/2019
1.1.1 417 9/25/2019
1.1.0 416 9/23/2019
1.0.5 699 5/10/2019