BorrowedTorch.UI.Markup 1.1.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package BorrowedTorch.UI.Markup --version 1.1.0
                    
NuGet\Install-Package BorrowedTorch.UI.Markup -Version 1.1.0
                    
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="BorrowedTorch.UI.Markup" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BorrowedTorch.UI.Markup" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="BorrowedTorch.UI.Markup" />
                    
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 BorrowedTorch.UI.Markup --version 1.1.0
                    
#r "nuget: BorrowedTorch.UI.Markup, 1.1.0"
                    
#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 BorrowedTorch.UI.Markup@1.1.0
                    
#: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=BorrowedTorch.UI.Markup&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=BorrowedTorch.UI.Markup&version=1.1.0
                    
Install as a Cake Tool

BorrowedTorch.UI.Markup

Builds BorrowedTorch.UI element trees from XML markup, so a UI can be written as data: loaded by the game at run time, and edited live in a tool.

<Ui>
    <StackPanel Name="menu" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button Name="newGame" Text="New game" DesiredWidth="200" DesiredHeight="40" OnClick="StartNewGame" />
        <Button Name="options" Text="Options" DesiredWidth="200" DesiredHeight="40" Margin="0, 8" />
        <Label Text="{Binding PlayerName}" Margin="0, 8" />
    </StackPanel>
</Ui>
using BorrowedTorch.UI.Markup;

var context = new MarkupContext { Handlers = this, DataContext = settings };

foreach (var element in UiMarkup.LoadFile("UI/MainMenu.btui", context)) _ui.AddElementToRoot(element);

UiMarkup.Load reads a markup from a string and LoadFile from a file. Both give the elements under <Ui> in the order they are written, not added anywhere yet. Load on the thread that updates the UI: the elements are made as code would make them.

LoadDocument gives them in a MarkupDocument, which remembers every element the markup made. Disposing it disposes all of them, the items and contents of its panels included: disposing a panel only detaches those. That is the way to take a whole UI away, a screen left or a markup loaded again in its place:

_menu?.Dispose();
_menu = UiMarkup.LoadDocument(File.ReadAllText("UI/MainMenu.btui"), context);

foreach (var element in _menu.Elements) _ui.AddElementToRoot(element);

The markup

The root is <Ui>, and every element in it is one of the UI's elements by its type name: <Button>, <StackPanel>, <Grid>. Name is the element's name, given when it is made (GetElementByName finds it). An element needs a constructor taking its name, as all of the UI's own elements have; a few internal parts (ContextMenuItem, ListBoxItem) have none and can't be written.

Attributes set the element's properties, the same ones code sets:

Type Written as
text as it is
numbers 120, 0.5 (a point, whatever the machine's culture)
bool true, false
enums the name of the value, in any case: Center, collapsed
Color #RRGGBB, #RRGGBBAA, r, g, b or r, g, b, a (0 to 255), or a name: CornflowerBlue
Margin 5 (all sides), 10, 20 (left, top), 10, 20, 30, 10, 20, 30, 40, as in code
Point, Vector2 x, y
converted from text an image's Source: a content path, loaded through the UI's ContentManager as it is read

UiMarkup.TryReadColor reads a text as a Color property's value is read, for a tool that shows the colours of a markup.

Child elements are the element's items (AddItem): the buttons of a panel, the rows of a list. An element with a Content (a Border, a Button, a Window) holds one child element, its content. An element that takes neither (a Label) refuses them. The text inside an element is its Text: <Label>Score</Label>.

Property elements give a property a value that is no text: an element of its own, a style, or a text too long for an attribute. They are named after the element and the property:

<Button Text="Save">
    <Button.ToolTip>
        <ToolTip Text="Save the game (Ctrl+S)" />
    </Button.ToolTip>
    <Button.TextStyle>
        <LabelStyle FontSize="18" ForegroundColor="White" />
    </Button.TextStyle>
</Button>

A style (or any other object of the UI that is no element) sets its properties by attributes too, and its own objects by property elements.

Grid columns and rows are listed in Columns and Rows: a size in units, auto (as large as the content) or a share of what is left (*, 2*):

<Grid Columns="200, *" Rows="auto, *">
    <Label Text="Title" GridColumnSpan="2" />
    <ListBox GridRow="1" />
    <Image GridColumn="1" GridRow="1" Source="Textures/Preview" />
</Grid>

Events name a method of the context's Handlers: OnClick="StartNewGame" subscribes its StartNewGame, public or not, taking the event's parameters ((BaseElement sender, MouseClickEventArgs e)) or none. Without such a method the markup can't be read, unless the context has a MissingHandler: then the event calls that instead, with the element, the event's name and the handler's name. A tool shows a markup's events that way, without the game's code to handle them:

var context = new MarkupContext
{
    MissingHandler = (sender, eventName, handlerName) => Log($"{((BaseElement)sender).Name} {eventName}: {handlerName}")
};

Bindings bind a property to one of the context's DataContext, a Bindable view model: Text="{Binding PlayerName}" follows it, and Mode sets the direction ({Binding Volume, Mode=TwoWay}; OneWay without one). A text that starts with a brace and is no binding starts with {}: Text="{}{not bound}".

Types and converters of your own

var context = new MarkupContext()
    .AddType<HealthBar>()                                    // <HealthBar Name="health" />
    .AddConverter(text => Enum.Parse<Rarity>(text, true));   // a type the markup can't read by itself

A type of your own is made by its name like the UI's: an element needs a constructor taking its name, any other object (a style) a constructor without parameters. A converter reads a property type from its text, in place of the markup's own way for that type.

Errors

A markup that can't be read throws a MarkupException: its Message starts with the line and the column, Line, Column and Reason give them apart. An unknown element or property, a value that can't be read, a child element where none is taken, a binding without a data context, a handler that is missing: each one is reported where it is written. A read that fails keeps none of what it made: every element made so far is disposed.

Building and testing

dotnet test BorrowedTorch.UI.Markup.slnx

The tests in BorrowedTorch.UI.Markup.Tests read markup into elements of the BorrowedTorch.UI version the package references, without a GraphicsDevice. The markup finds the elements' properties by reflection, so after a BorrowedTorch.UI release, raise the reference and run them: a property renamed there breaks a markup at run time, not at compile time.

Product Compatible and additional computed target framework versions.
.NET net10.0-windows10.0.19041 is compatible. 
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.

Version Downloads Last Updated
1.9.2 52 11/18/2026
1.9.1 47 11/18/2026
1.9.0 51 11/18/2026
1.8.0 68 11/17/2026
1.7.0 137 11/1/2026
1.6.0 106 10/31/2026
1.5.0 114 10/31/2026
1.4.0 119 10/31/2026
1.3.0 121 10/31/2026
1.2.0 115 10/31/2026
1.1.0 110 10/30/2026
1.0.0 116 10/30/2026