Table of Contents

Class CrystalApplication

Namespace
Crystal.Avalonia
Assembly
Crystal.Avalonia.dll

The base application class, extending Avalonia's Avalonia.Application. Provides the foundation for modular development and MVVM binding.

public class CrystalApplication : Application, INotifyPropertyChanged, IDataContextProvider, IGlobalDataTemplates, IDataTemplateHost, IGlobalStyles, IStyleHost, IThemeVariantHost, IResourceHost, IResourceNode, IOptionalFeatureProvider
Inheritance
AvaloniaObject
Application
CrystalApplication
Implements
IDataContextProvider
IGlobalDataTemplates
IDataTemplateHost
IGlobalStyles
IStyleHost
IThemeVariantHost
IResourceHost
IResourceNode
IOptionalFeatureProvider
Inherited Members
Application.DataContextProperty
Application.ActualThemeVariantProperty
Application.RequestedThemeVariantProperty
Application.NameProperty
Application.Initialize()
Application.RegisterServices()
Application.OnPropertyChanged(AvaloniaPropertyChangedEventArgs)
Application.DataContext
Application.RequestedThemeVariant
Application.ActualThemeVariant
Application.Current
Application.DataTemplates
Application.Resources
Application.Styles
Application.ApplicationLifetime
Application.PlatformSettings
Application.Name
Application.ResourcesChanged
Application.ActualThemeVariantChanged
AvaloniaObject.CheckAccess()
AvaloniaObject.VerifyAccess()
AvaloniaObject.ClearValue(AvaloniaProperty)
AvaloniaObject.ClearValue<T>(AvaloniaProperty<T>)
AvaloniaObject.ClearValue<T>(StyledProperty<T>)
AvaloniaObject.ClearValue<T>(DirectPropertyBase<T>)
AvaloniaObject.GetHashCode()
AvaloniaObject.GetValue(AvaloniaProperty)
AvaloniaObject.GetValue<T>(StyledProperty<T>)
AvaloniaObject.GetValue<T>(DirectPropertyBase<T>)
AvaloniaObject.GetBaseValue<T>(StyledProperty<T>)
AvaloniaObject.IsAnimating(AvaloniaProperty)
AvaloniaObject.IsSet(AvaloniaProperty)
AvaloniaObject.SetValue<T>(StyledProperty<T>, T, BindingPriority)
AvaloniaObject.SetValue<T>(DirectPropertyBase<T>, T)
AvaloniaObject.SetCurrentValue<T>(StyledProperty<T>, T)
AvaloniaObject.Bind(AvaloniaProperty, BindingBase)
AvaloniaObject.CoerceValue(AvaloniaProperty)
AvaloniaObject.OnPropertyChangedCore(AvaloniaPropertyChangedEventArgs)
AvaloniaObject.RaisePropertyChanged<T>(DirectPropertyBase<T>, T, T)
AvaloniaObject.SetAndRaise<T>(DirectPropertyBase<T>, ref T, T)
AvaloniaObject.InheritanceParent
AvaloniaObject.this[AvaloniaProperty]
AvaloniaObject.this[IndexerDescriptor]
AvaloniaObject.Dispatcher
AvaloniaObject.PropertyChanged

Examples

public class MyApp : CrystalApplication
{
    public override void RegisterModules(IModuleRegistrar moduleRegistrar)
    {
        moduleRegistrar.RegisterModule<MyModule>();
    }

    public override void RegisterServices(IServiceCollection services)
    {
        services.AddMvvmTransient<MainView, MainViewModel>();
    }

    public override void CreateShell(IServiceProvider serviceProvider)
    {
        CreateShell<MainWindow, MainView>();
    }
}

Remarks

Usage steps:

  1. Define modules: Create classes implementing IModule.
  2. Override RegisterModules(IModuleRegistrar): Register all functional modules.
  3. Override RegisterServices(IServiceCollection): Register application-level services (optional).
  4. Override CreateShell(IServiceProvider): Create the main window or main view.

Properties

Mvvm

Gets the MVVM mapping and service-provider holder for this application instance.

public MvvmManager Mvvm { get; }

Property Value

MvvmManager

Options

Gets the framework options for this application instance. Also registered as a singleton in the DI container. Prefer configuring via ConfigureOptions(CrystalOptions).

public CrystalOptions Options { get; }

Property Value

CrystalOptions

Methods

ConfigureOptions(CrystalOptions)

Override this method to configure CrystalOptions before the DI container is built.

public virtual void ConfigureOptions(CrystalOptions options)

Parameters

options CrystalOptions

The options instance for this application.

Examples

public override void ConfigureOptions(CrystalOptions options)
{
    options.EnableViewLocator = false;
}

CreateShell(IServiceProvider)

Override this method to create the main shell (window or view) of the application.

public virtual void CreateShell(IServiceProvider serviceProvider)

Parameters

serviceProvider IServiceProvider

The service provider, can be used to resolve services registered in RegisterServices(IServiceCollection) or modules.

CreateShell<TWindow, TView>()

Creates the shell based on ApplicationLifetime. Shell views are not resolved from DI; use new (no constructor injection expected). Child ViewModels are wired via ViewModelLocator when AutoWireViewModel="True".

protected void CreateShell<TWindow, TView>() where TWindow : Window, new() where TView : Control, new()

Type Parameters

TWindow

Desktop main window type.

TView

Single-view main view type.

OnFrameworkInitializationCompleted()

public override void OnFrameworkInitializationCompleted()

RegisterModules(IModuleRegistrar)

Override this method to register all functional modules for the application.

public virtual void RegisterModules(IModuleRegistrar moduleRegistrar)

Parameters

moduleRegistrar IModuleRegistrar

The module registrar, used to register IModule implementations.

Examples

public override void RegisterModules(IModuleRegistrar moduleRegistrar)
{
    moduleRegistrar.RegisterModule<HomeModule>();
    moduleRegistrar.RegisterModule<SettingsModule>();
}

RegisterServices(IServiceCollection)

Override this method to register application-level services. It is recommended to use AddMvvmTransient<TView, TViewModel>(IServiceCollection) or AddMvvmSingleton<TView, TViewModel>(IServiceCollection) to register View/ViewModel pairs. CrystalOptions is already registered as a singleton before this method runs.

public virtual void RegisterServices(IServiceCollection services)

Parameters

services IServiceCollection

The service collection to add application-level services to.