Interface IViewModelRegistry
Registry for associating ViewModel types with their corresponding View types.
public interface IViewModelRegistry
Examples
ViewModels:
class MySpecificViewModel1 : MyAbstractViewModel { }
class MySpecificViewModel2 : MyAbstractViewModel { }
Views:
@inherits MvvmComponentBase<MySpecificViewModel1>
<h3>View for MySpecificViewModel1</h3>
DI Setup:
services.AddSingleton<ViewModelRegistry>(container => {
var registry = new ViewModelRegistry(
container.GetRequiredService<ILogger<ViewModelRegistry>>());
registry.Register<MySpecificViewModel1, MyComponent1>();
registry.Register<MySpecificViewModel2, MyComponent2>();
return registry;
});
In Component:
@inject IEnumerable<MyAbstractViewModel> ViewModels
@foreach (var vm in ViewModels)
{
<RegisteredViewFor Vm="vm"/>
}
Methods
GetViewForViewModelDynamic(Type)
Like GetViewForViewModel<TViewModel>(TViewModel), but accepts a Type parameter instead of an instance.
Type? GetViewForViewModelDynamic(Type viewModelType)
Parameters
viewModelTypeType
Returns
GetViewForViewModel<TViewModel>(TViewModel)
Retrieves the registered View type for a given ViewModel type, or null if no registration exists.
Type? GetViewForViewModel<TViewModel>(TViewModel viewModel) where TViewModel : INotifyPropertyChanged
Parameters
viewModelTViewModel
Returns
Type Parameters
TViewModel
RegisterDynamic(Type, Type, bool)
Like Register<TViewModel, TComponent>(bool), but accepts Type parameters instead of generics.
Note that there are no runtime checks for the constraints that apply to the generic version of this method to allow usage with e.g. AOT compilation.
void RegisterDynamic(Type viewModelType, Type componentType, bool noWarn = false)
Parameters
Register<TViewModel, TComponent>(bool)
Registers a View type for a given ViewModel type. TComponent must be an MvvmComponentBase<T>, not with an optional or owned viewmodel, because we pass a non-null ViewModel instance to the component when rendering. Without that constraint, it would be possible to register a component that doesn't accept the ViewModel as a parameter.
You may prefer using the source generator - see ViewModelForAttribute.
If you are not using trimming (enabled by default in Blazor WASM since .NET 6 with
dotnet publish) or AOT compilation, you may prefer using
AutoRegister() for automatic registration.
Otherwise, use this method to manually register all View/ViewModel pairs.
void Register<TViewModel, TComponent>(bool noWarn = false) where TViewModel : INotifyPropertyChanged where TComponent : MvvmComponentBase<TViewModel>
Parameters
noWarnbool
Type Parameters
TViewModelTComponent