MarkWin2D 0.0.6-alpha

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

MarkWin2D

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

功能

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

安装

dotnet add package MarkWin2D

快速开始

XAML

<UserControl
    xmlns:md="using:MarkWin2D.Controls">
    <md:MarkWin2DControl 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
};
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.6-alpha 0 6/6/2026
0.0.5-alpha 0 6/6/2026
0.0.4-alpha 0 6/6/2026