DeclarativeComposition 0.2.1-alpha

This is a prerelease version of DeclarativeComposition.
dotnet add package DeclarativeComposition --version 0.2.1-alpha
                    
NuGet\Install-Package DeclarativeComposition -Version 0.2.1-alpha
                    
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="DeclarativeComposition" Version="0.2.1-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DeclarativeComposition" Version="0.2.1-alpha" />
                    
Directory.Packages.props
<PackageReference Include="DeclarativeComposition" />
                    
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 DeclarativeComposition --version 0.2.1-alpha
                    
#r "nuget: DeclarativeComposition, 0.2.1-alpha"
                    
#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 DeclarativeComposition@0.2.1-alpha
                    
#: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=DeclarativeComposition&version=0.2.1-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=DeclarativeComposition&version=0.2.1-alpha&prerelease
                    
Install as a Cake Tool

Declarative Composition

Tired of wiring up all the components in your visual tree? Declarative Composition is here to help you simplify the construction of the WinUI visual layer using a clean, declarative syntax.

Features

  • Declarative DSL: Write clear and readable declarations for defining visual hierarchies.
  • C# Source Generator: Directly call the automatically generated C# code for your visual tree.
  • Type-Aware Syntax: Built-in support for nested visuals and inline C# expressions. (And more on the way!)
  • Anonymous Objects: Stop having to name every component in your visual tree. Only name the ones you need, and let the rest be anonymous.

Usage

Add the following package reference to the DeclarativeComposition NuGet package in your WinUI project.

<PackageReference Include="MyCompany.MyCodeGenerator" Version="0.2.1-alpha"
                  OutputItemType="Analyzer"
                  ReferenceOutputAssembly="false" />

The package includes a C# source generator that will automatically generate the necessary code for your visual tree based on the declarative syntax you define.

Documentation

WIP

Example

With Composition APIs, you can create a visual tree like this:

// ...

_rootVisual = _compositor.CreateSpriteVisual();
_rootVisual.Size = new(800f, 600f);
_rootVisual.Offset = new(400f, 300f, 0f);

var backgroundVisual = _compositor.CreateSpriteVisual();
backgroundVisual.RelativeSizeAdjustment = new(1f, 1f);
_backgroundBrush = _compositor.CreateColorBrush();
_backgroundBrush.Color = Windows.UI.Color.FromArgb(0x3F, 0xBF, 0xBF, 0xBF);
backgroundVisual.Brush = _backgroundBrush;
_rootVisual.Children.InsertAtTop(backgroundVisual);

_foregroundVisual = _compositor.CreateSpriteVisual();
_foregroundVisual.RelativeSizeAdjustment = new(0.9f, 1f);
var foregroundBrush = _compositor.CreateColorBrush();
foregroundBrush.Color = Windows.UI.Color.FromArgb(0xBF, 0x7F, 0x7F, 0x7F);
_foregroundVisual.Brush = foregroundBrush;
_rootVisual.Children.InsertAtTop(_foregroundVisual);

var clip = _compositor.CreateGeometricClip();
_clipGeometry = _compositor.CreateRoundedRectangleGeometry();
_clipGeometry.CornerRadius = new(4f);
_clipGeometry.Size = new(800f, 600f);
clip.Geometry = _clipGeometry;
_rootVisual.Clip = clip;

// ...

It is sometimes exhausting to wire up every component, and naming always gives us headaches... With Declarative Composition, the equivalent visual tree can be defined as:

_rootVisual : SpriteVisual {
    .size = "800, 600"
    .offset = "400 300 0"
    SpriteVisual {
        .relativeSizeAdjustment = "1"
        .brush = _backgroundBrush : ColorBrush {
            .color = "#3fbfbfbf"
        }
    }
    _foregroundVisual: SpriteVisual {
        .relativeSizeAdjustment = "0.9 1"
        .brush = ColorBrush {
            .color = "#BF7F7F7F"
        }
    }   
    .clip = GeometricClip {
        .geometry = _clipGeometry : RoundedRectangleGeometry {
            .cornerRadius = "4"
            .size = "800 600"
        }
    }
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.1-alpha 124 8/4/2025
0.1.3-alpha 485 7/23/2025