BetterWinUI.DependencyInjection.PageActivation 0.1.2

dotnet add package BetterWinUI.DependencyInjection.PageActivation --version 0.1.2
                    
NuGet\Install-Package BetterWinUI.DependencyInjection.PageActivation -Version 0.1.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BetterWinUI.DependencyInjection.PageActivation" Version="0.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BetterWinUI.DependencyInjection.PageActivation" Version="0.1.2" />
                    
Directory.Packages.props
<PackageReference Include="BetterWinUI.DependencyInjection.PageActivation" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BetterWinUI.DependencyInjection.PageActivation --version 0.1.2
                    
#r "nuget: BetterWinUI.DependencyInjection.PageActivation, 0.1.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BetterWinUI.DependencyInjection.PageActivation@0.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BetterWinUI.DependencyInjection.PageActivation&version=0.1.2
                    
Install as a Cake Addin
#tool nuget:?package=BetterWinUI.DependencyInjection.PageActivation&version=0.1.2
                    
Install as a Cake Tool

BetterWinUI.DependencyInjection.PageActivation

NativeAOT-compatible constructor injection for WinUI Page navigation.

You can register explicitly in the DI composition root:

public App()
{
    var services = new ServiceCollection();
    services.AddTransient<MainViewModel>();
    services.AddTransient<MainPage>();
    services.AddBetterPageActivation();

    Services = services.BuildServiceProvider();
    this.InitializeBetterPageActivation(Services);
}

public ServiceProvider Services { get; }
public sealed partial class MainPage : Page
{
    public MainPage(MainViewModel viewModel)
    {
        InitializeComponent();
        ViewModel = viewModel;
    }

    public MainViewModel ViewModel { get; }
}

public sealed class MainViewModel;

Or, you can use the optional registration attributes:

[View] // ServiceLifetime.Transient by default
public sealed partial class MainPage : Page
{
    public MainPage(MainViewModel viewModel)
    {
        InitializeComponent();
        ViewModel = viewModel;
    }

    public MainViewModel ViewModel { get; }
}

[ViewModel(ServiceLifetime.Transient)]
public sealed class MainViewModel;

[View] and [ViewModel] are independent registration syntax sugar; they do not declare a navigation or View-to-ViewModel relationship.

Generated registrations use standard Microsoft DI descriptors and TryAdd, so explicit registrations already present in the service collection are preserved. Constructor selection and keyed constructor dependencies remain container-owned. The package does not assign DataContext.

Unregistered Pages throw by default. Native XAML activation can be enabled explicitly:

services.AddBetterPageActivation(static options =>
{
    options.UnregisteredPageBehavior =
        UnregisteredPageBehavior.FallbackToXamlActivator;
});

Call InitializeBetterPageActivation exactly once after the service provider is built and before navigating to a Page.

ServiceLifetime.Scoped is rejected until navigation scope ownership and disposal are defined.

Diagnostics

BWPA0001

The application type must be declared partial so the generator can extend it.

BWPA0002

[PageActivation] must target a concrete, non-generic, top-level WinUI Application subclass.

BWPA0003

Only one application type per assembly can use [PageActivation].

BWPA0004

The XAML compiler output must expose one native IXamlMetadataProvider property on the application type.

BWPA0005

Rename a user-declared InitializeBetterPageActivation member so the generator can emit its initialization method.

BWPA0006

[View] must target an accessible, concrete, non-generic WinUI Page subclass.

BWPA0007

[ViewModel] must target an accessible, concrete, non-abstract, closed class.

BWPA0009

Generated registrations cannot use ServiceLifetime.Scoped until navigation scope ownership and disposal are implemented.

BWPA0010

A referenced generated view module uses an incompatible page activation contract version.

BWPA0011

The generated ViewModel registration has no public constructor; provide an explicit DI factory before calling AddBetterPageActivation.

BWPA0012

No InitializeBetterPageActivation call was found for the application type.

BWPA0013

The referenced Windows App SDK XAML interfaces do not expose the interception contract required by the generator.

BWPA0014

The generated Page registration has no public constructor; provide an explicit DI factory before calling AddBetterPageActivation.

BWPA0015

Generated registrations support only ServiceLifetime.Transient and ServiceLifetime.Singleton.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.