Namespace Dmnk.Blazor.Mvvm
This library provides some base types to inherit from in a Blazor application, to implement the MVVM
pattern. It makes no assumptions about the implementation of the ViewModel, only that it must
implement INotifyPropertyChanged and that commands implement ICommand.
Example
// You don't have to use the community toolkit, but we will use it in this example here.
// If you are using the community toolkit, you probably also want the
// Dmnk.Blazor.Mvvm.CommunityToolkit package, which allows binding to async relay commands.
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
public class MyViewModel : ObservableObject
{
[ObservableProperty]
private string _text;
[RelayCommand]
private void DoSomething()
{
Text = "Hello World!";
}
}
@inherits MvvmComponentBase<MyViewModel>
<p>@Vm.Text</p>
<button @onclick="@Vm.DoSomethingCommand.Bind(this)">Click me</button>
Classes
- AbstractMvvmComponentBase<T>
Invokes StateHasChanged when ViewModel fires INotifyPropertyChanged and when any property of type ICommand fires CanExecuteChanged.
- MvvmComponentBase<T>
Invokes StateHasChanged when ViewModel fires INotifyPropertyChanged and when any property of type ICommand fires CanExecuteChanged.
The ViewModel is set as a required parameter(Vm).
- OptionalMvvmComponentBase<T>
Invokes StateHasChanged when ViewModel fires INotifyPropertyChanged and when any property of type ICommand fires CanExecuteChanged.
The ViewModel is set as an optional parameter(Vm).
- OwningMvvmComponentBase<T>
Invokes StateHasChanged when ViewModel fires INotifyPropertyChanged and when any property of type ICommand fires CanExecuteChanged.
The ViewModel is set as a property that isn't a parameter(Vm).
- ReflectionAutoViewModelRegistry
Like ViewModelRegistry, but with an additional method AutoRegister() that automatically registers all ViewModels and their corresponding Views based on reflection.
You may prefer using the source generator - see ViewModelForAttribute.
- RegisteredViewFor<TViewModel>
Blazor component that serves as a marker for associating a ViewModel type with a View type in the ViewModelRegistry.
- ViewModelForAttribute
Marks a ViewModel to be automatically registered with its corresponding view component. The source generator will create registration code for this pairing. See https://dominiksta.github.io/Dmnk.Toolkit/api/Dmnk.Blazor.Mvvm.SourceGen.html.
Interfaces
- IViewModelRegistry
Registry for associating ViewModel types with their corresponding View types.