GenerateAvaloniaDesignTimeConstructorAttribute
NexusLabs.Needlr.Avalonia¶
GenerateAvaloniaDesignTimeConstructorAttribute Class¶
Generates a parameterless constructor for Avalonia design-time XAML preview support.
Inheritance System.Object 🡒 System.Attribute 🡒 GenerateAvaloniaDesignTimeConstructorAttribute
Example¶
[GenerateAvaloniaDesignTimeConstructor]
public sealed partial class MainWindow : Window
{
private readonly ShellViewModel _viewModel;
public MainWindow(ShellViewModel viewModel)
{
_viewModel = viewModel;
InitializeComponent();
DataContext = viewModel;
}
}
Remarks¶
Apply this to a partial class that has a constructor with injectable
parameters (the DI constructor). The source generator emits a parameterless
constructor that:
- Suppresses CS8618 (non-nullable fields not initialized) via #pragma
- Throws System.InvalidOperationException if called at runtime
outside Avalonia's design mode
- Calls InitializeComponent() for the XAML previewer
At runtime, Needlr's richest-constructor selection ensures the DI constructor is used. The generated parameterless constructor exists solely for the Avalonia XAML designer/previewer.