MarkdownRender 0.0.3-alpha

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

MarkdownRender

基于 Win2DGitHub Flavored Markdown 渲染控件,用于 WinUI 3 应用。

功能

  • 标题 (H1–H6)
  • 粗体、斜体、删除线
  • 行内代码 & 围栏代码块
  • 有序 / 无序 / 任务列表
  • 嵌套块引用
  • 表格(自动列宽)
  • 分隔线
  • 超链接(可点击,支持开关)
  • 图片(通过 IImageProvider 接入)
  • 图片对齐 — 左对齐 / 居中 / 右对齐

安装

dotnet add package MarkdownRender

快速开始

XAML

<UserControl
    xmlns:md="using:MarkdownRender.Controls">
    <md:MarkdownRenderControl x:Name="MarkdownView" />
</UserControl>

代码

MarkdownView.Text = "# Hello\n**粗体** `代码`";
MarkdownView.AreLinksEnabled = true;   // 开启链接点击

图片

控件不自带下载。实现 IImageProvider 后挂载:

public class MyImageProvider : IImageProvider
{
    private readonly HttpClient _http = new();
    private readonly Dictionary<string, CanvasBitmap> _cache = new();

    public event EventHandler? ImagesInvalidated;

    public CanvasBitmap? GetImage(string url, CanvasDevice device)
    {
        if (_cache.TryGetValue(url, out var bmp)) return bmp;
        _ = LoadAsync(url, device);
        return null;
    }

    private async Task LoadAsync(string url, CanvasDevice device)
    {
        var bytes = await _http.GetByteArrayAsync(url);
        using var ms = new MemoryStream(bytes);
        var bmp = await CanvasBitmap.LoadAsync(device, ms.AsRandomAccessStream());
        _cache[url] = bmp;
        ImagesInvalidated?.Invoke(this, EventArgs.Empty);
    }
}

MarkdownView.ImageProvider = new MyImageProvider();

图片对齐

MarkdownView.DocumentStyle.ImageAlignment = ImageAlignment.Center;
// Left(默认)、Center、Right

样式

所有样式属性通过 MarkdownStyle 暴露:

MarkdownView.DocumentStyle = new MarkdownStyle
{
    FontFamily = "Consolas",
    BodyFontSize = 14,
    TextColor = Colors.White,
    ImageAlignment = ImageAlignment.Center
};

环境要求

  • .NET 10 + Windows 10.0.26100
  • WinUI 3 / Windows App SDK 1.8
  • Win2D 1.4
Product Compatible and additional computed target framework versions.
.NET net10.0-windows10.0.26100 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.3-alpha 0 6/5/2026