XamlToolkit.WinUI.Markdown
0.1.0-alpha
dotnet add package XamlToolkit.WinUI.Markdown --version 0.1.0-alpha
NuGet\Install-Package XamlToolkit.WinUI.Markdown -Version 0.1.0-alpha
<PackageReference Include="XamlToolkit.WinUI.Markdown" Version="0.1.0-alpha" />
<PackageVersion Include="XamlToolkit.WinUI.Markdown" Version="0.1.0-alpha" />
<PackageReference Include="XamlToolkit.WinUI.Markdown" />
paket add XamlToolkit.WinUI.Markdown --version 0.1.0-alpha
#r "nuget: XamlToolkit.WinUI.Markdown, 0.1.0-alpha"
#:package XamlToolkit.WinUI.Markdown@0.1.0-alpha
#addin nuget:?package=XamlToolkit.WinUI.Markdown&version=0.1.0-alpha&prerelease
#tool nuget:?package=XamlToolkit.WinUI.Markdown&version=0.1.0-alpha&prerelease
XamlToolkit.WinUI.Markdown
Two WinUI 3 Markdown controls implemented in Rust:
MarkdownTextBlockbuilds a XAML element tree and supports CommonMark/GFM with syntax-highlighted code, tables, footnotes, math and images. Mermaid diagrams are available when the native component is built with themermaidCargo feature.==mark==text stays plain; inline highlight backgrounds are aMarkdownViewfeature. Footnote references are linked inMarkdownTextBlock;MarkdownViewkeeps[^1]references literal because it has no document-level footnote UI.MarkdownViewis the Direct2D renderer: a composition drawing surface painted by D2D, IDWriteTextLayout for text, and remend-based streaming completion. It supports scrolling, auto-scroll-to-end, links, headings, lists, WinUI-style task checkboxes, quotes (including> [!NOTE]admonitions rendered as InfoBar-style surfaces), inline highlights (==mark==and<mark>), tables, highlighted code blocks, HTTP/SVG images (including animated GIF, WebP and APNG; PNG/JPEG/GIF/WebP/BMP/ICO raster decoding), TeX math, text selection/copy, and (with the Full native variant) Mermaid diagrams.
Both are regular Microsoft.UI.Xaml.Controls.Controls, so they can be used from XAML and
styled like any other control, and both raise LinkClicked. MarkdownView paints its own
text, so it exposes a read-only UI Automation TextPattern instead of a full XAML text
tree, and uses the light/dark palette rather than system high-contrast colors.
UI Automation
MarkdownView creates its own automation peer and exposes TextPattern:
DocumentRangeandGetTextreturn the rendered plain text.- Text ranges support character/word/line/paragraph movement and
FindText. GetSelectionreports the active range, including a drag selection or Select All.GetVisibleRangesreturns the text visible in the viewport; lazy layouts fall back to the whole document.- Ranges can
SelectandScrollIntoView.
Selection/scroll mapping and visible ranges use the eager layout; lazy-layout documents
fall back to the whole range. RangeFromPoint currently returns the document range.
Bounding rectangles for text ranges, formatting attributes, link elements and automatic text/selection-change
events are not part of this first phase. Use MarkdownTextBlock when a full XAML
accessibility tree, text scaling or XAML-level styling is required.
Requirements
- Windows 10/11
- .NET 10 (or later) targeting Windows
- Windows App SDK 1.8
- A concrete platform (
x64,x86orarm64);AnyCPUcannot select the native DLL
Install
dotnet add package XamlToolkit.WinUI.Markdown
Native variants
The package contains two native DLLs for every architecture:
Slim(default): the smaller build; Mermaid fences render as code blocks.Full: includes the Mermaid/merman renderer.
Select the full build in the consuming project:
<PropertyGroup>
<XamlToolkitMarkdownVariant>Full</XamlToolkitMarkdownVariant>
</PropertyGroup>
XamlToolkitMarkdownVariant defaults to Slim and is case-insensitive.
The managed projection is compatible with Native AOT. Publish the consuming app with
-p:PublishAot=true -p:SelfContained=true; the selected native DLL is copied next to the
executable as usual.
For a C++/WinRT project the package's build/native target adds the component winmd and
copies the selected DLL (and its theme PRI) to the output directory; set the same
XamlToolkitMarkdownVariant property in the .vcxproj. The C++ project also needs its own
Windows App SDK reference/tooling, as usual for WinUI 3 C++.
Use
<Page
xmlns:md="using:XamlToolkit.WinUI.Markdown">
<md:MarkdownTextBlock x:Name="Markdown" />
</Page>
Markdown.Text = "# Hello\n\nRendered by **MarkdownTextBlock**.";
Markdown.LinkClicked += (_, args) =>
{
// Handle the link instead of letting the control navigate.
args.Handled = true;
};
Direct2D view and streaming
MarkdownView renders into a composition drawing surface, so it does not create a XAML
element per run. Use it when a document is long or is being streamed:
<md:MarkdownView
x:Name="StreamingPreview"
AutoScrollToEnd="True" />
Append each response chunk to the view:
StreamingPreview.Reset();
await foreach (var chunk in response)
{
StreamingPreview.Append(chunk);
}
Append starts an implicit stream on its first call and keeps the current Text as its
prefix. Call it on the UI thread; updates are coalesced there. The streaming parser
completes unfinished emphasis, code spans and links before rendering each snapshot.
While streaming, Text is published from the accumulated buffer no more than once per
100 ms, with a trailing update after the last chunk. Text can therefore lag the newest
chunk by up to about 100 ms; after the trailing update it contains the complete
accumulated Markdown. Reset cancels the stream and clears the document, while setting
Text replaces the document and cancels the active stream. LinkClicked has the same
shape as on MarkdownTextBlock.
MarkdownView also works without its own scrollbar. When the parent measures it with an
unbounded height (for example, a StackPanel inside a ScrollViewer), it lays out the
whole document and reports its natural height; this is the mode for chat transcripts and
gallery pages. A finite height selects the internal ScrollViewer and lazy layout.
The view follows the control's FontFamily, FontSize and Foreground. Changing
FontSize re-resolves the heading ramp and re-lays out the document.
Large documents use lazy layout. Images, formulas and diagrams keep their measured size
but release decoded pixels and GPU bitmaps once they are more than three viewports away;
they are loaded again when scrolled back into range. Reset releases the parsed document,
layouts and assets immediately, including while the control is unloaded.
Raster images are decoded at their natural resolution; bitmaps larger than 4096 pixels on the longest side are downsampled before upload. Animated GIF, WebP and APNG frames are decoded at their requested display size multiplied by the current display scale, so they stay sharp on high-DPI screens without exceeding the same side limit.
LinkClicked is an ordinary WinRT event, so the control keeps each subscribed delegate
alive. A handler that captures the view or its page (a lambda or an instance method)
therefore forms a cycle that the garbage collector cannot break: remove it when the page
or window goes away. The sample does this in Window.Closed; elsewhere use
view.LinkClicked -= handler, or set ImageProvider back to null if the provider
captures the view.
Custom images (both controls)
Set ImageProvider on either control to resolve images yourself. The provider receives
the absolute image Uri and returns a readable stream. MarkdownTextBlock feeds it to
XAML's BitmapImage; MarkdownView decodes it with the component's Rust image decoders.
using System;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Streams;
using XamlToolkit.WinUI.Markdown;
public sealed class MyImageProvider : IImageProvider
{
public IAsyncOperation<IRandomAccessStream> GetImageAsync(Uri uri) =>
GetStreamAsync(uri).AsAsyncOperation();
private static async Task<IRandomAccessStream> GetStreamAsync(Uri uri)
{
var file = await StorageFile.GetFileFromPathAsync(uri.LocalPath);
return await file.OpenReadAsync();
}
}
Markdown.ImageProvider = new MyImageProvider();
PreviewD2D.ImageProvider = new MyImageProvider();
WinRT metadata cannot expose Task<T>, so the interface returns
IAsyncOperation<IRandomAccessStream>; AsAsyncOperation() adapts a Task<T>. The
provider is consulted for every image. If it cannot start a load, the control falls back
to its built-in HTTP/SVG loader. MarkdownView reads provider streams and decodes raster
formats itself with Rust decoders (PNG/JPEG/GIF/WebP/BMP/ICO, including animated GIF,
WebP and APNG); SVG streams are not decoded on this path.
Unpackaged applications register the component's activatable classes in app.manifest:
<file name="XamlToolkit.WinUI.Markdown.dll">
<winrtv1:activatableClass name="XamlToolkit.WinUI.Markdown.MarkdownTextBlock" threadingModel="both" />
<winrtv1:activatableClass name="XamlToolkit.WinUI.Markdown.MarkdownView" threadingModel="both" />
<winrtv1:activatableClass name="XamlToolkit.WinUI.Markdown.LinkClickedEventArgs" threadingModel="both" />
<winrtv1:activatableClass name="XamlToolkit.WinUI.Markdown.XamlMetaDataProvider" threadingModel="both" />
</file>
See the sample application for a complete project.
Package contents
The package contains the component's winmd and theme resources, the native DLLs for x64/x86/arm64, and the C#/WinRT projection.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-windows10.0.26100 is compatible. |
| native | native is compatible. |
-
- Microsoft.WindowsAppSDK.WinUI (>= 1.8.260803003)
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.1.0-alpha | 72 | 11/29/2026 |
Adds InfoBar-style alert surfaces, WinUI-style task checkboxes, translucent inline highlights, content-sized auto-height layout, a read-only UI Automation TextPattern for MarkdownView, and preview font-size/theme polish.