Table of Contents

Namespace Dmnk.Blazor.Mvvm.SourceGen

(This is not a separate NuGet package, but built into in the main Dmnk.Blazor.Mvvm package)

Use source-generation to register ViewModels with their Views, such that they can be used with .

Usage

MyViewModel.cs:

// NOTE: MyView must either be in the same namespace as MyViewModel, or you must specify the full
// namespace in the attribute. This is a limitation of using source-generation for razor components.
[ViewModelFor(typeof(MyView))]
public class MyViewModel : INotifyPropertyChanged { ... }

MyView.razor:

@inherits MvvmComponentBase<MyViewModel>
@* ... *@

Program.cs:

// ...
ServiceProvider provider = services.BuildServiceProvider();

var registry = provider.GetRequiredService<IViewModelRegistry>();
My.Namespace.Containing.MyViewModel.SourceGeneratedViewModelRegistrations.Register(registry);

MyDynamicView.razor:

<RegisteredViewFor Vm="myViewModel" />

Classes

ViewModelRegistryGenerator

Generates automatic ViewModel-to-View registrations for types marked with [ViewModelFor] attribute.