Table of Contents

Class MvvmServiceCollectionExtensions

Namespace
Crystal.Avalonia
Assembly
Crystal.Avalonia.dll

Extension methods for registering View/ViewModel pairs on IServiceCollection.

public static class MvvmServiceCollectionExtensions
Inheritance
MvvmServiceCollectionExtensions
Inherited Members

Methods

AddMvvmSingleton<TView, TViewModel>(IServiceCollection)

Registers a ViewModel as Singleton in DI and records the View ↔ ViewModel mapping. Only TViewModel is added to DI via services.AddSingleton<TViewModel>(). TView is used for mapping only; views are created by XAML or ViewLocator.

public static void AddMvvmSingleton<TView, TViewModel>(this IServiceCollection services) where TView : Control where TViewModel : class

Parameters

services IServiceCollection

The service collection.

Type Parameters

TView

The View type, must inherit from Avalonia.Controls.Control.

TViewModel

The ViewModel type, can be any class.

Examples

services.AddMvvmSingleton<SettingsView, SettingsViewModel>();

AddMvvmTransient<TView, TViewModel>(IServiceCollection)

Registers a ViewModel as Transient in DI and records the View ↔ ViewModel mapping. Only TViewModel is added to DI via services.AddTransient<TViewModel>(). TView is used for mapping only; views are created by XAML or ViewLocator.

public static void AddMvvmTransient<TView, TViewModel>(this IServiceCollection services) where TView : Control where TViewModel : class

Parameters

services IServiceCollection

The service collection.

Type Parameters

TView

The View type, must inherit from Avalonia.Controls.Control.

TViewModel

The ViewModel type, can be any class.

Examples

services.AddMvvmTransient<MainView, MainViewModel>();