AvaloniaConsole 1.1.0
dotnet add package AvaloniaConsole --version 1.1.0
NuGet\Install-Package AvaloniaConsole -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="AvaloniaConsole" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AvaloniaConsole" Version="1.1.0" />
<PackageReference Include="AvaloniaConsole" />
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 AvaloniaConsole --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AvaloniaConsole, 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 AvaloniaConsole@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=AvaloniaConsole&version=1.1.0
#tool nuget:?package=AvaloniaConsole&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
AvaloniaConsole
一个高性能、易用的 Avalonia 控制台组件,支持大量数据展示、文本选择、自动滚动等功能。
✨ 特性
- 🚀 高性能虚拟化渲染 - 只绘制可见区域,可轻松处理数百万行日志
- 💾 内存高效 - 优化的数据存储,低内存占用
- 🖱️ 完整的鼠标交互
- 文本选择和复制 (支持多行连续选择)
- 自定义滚动条 (拖动、点击跳转)
- 鼠标滚轮滚动
- 📜 自动滚动到底部 - 新内容自动滚动,手动滚动时暂停
- 🎨 完全可定制 - 支持自定义字体、颜色、背景
- 🧵 线程安全 - 支持从任意线程添加日志
- 📦 易于集成 - 简单的 API,MVVM 友好
- 🔄 多实例支持 - 可在同一应用中使用多个控制台组件
🎯 使用场景
- IDE 输出窗口
- 应用程序日志查看器
- 实时监控面板
- 调试信息展示
- 命令行模拟器
- 构建输出显示
📦 安装
NuGet Package Manager
Install-Package AvaloniaConsole
.NET CLI
dotnet add package AvaloniaConsole
PackageReference
<PackageReference Include="AvaloniaConsole" Version="1.0.0" />
🚀 快速开始
1. 在 XAML 中添加控件
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ac="clr-namespace:AvaloniaConsole;assembly=AvaloniaConsole">
<ac:ConsoleControl Buffer="{Binding ConsoleBuffer}"
FontFamily="Cascadia Code, Consolas, Monospace"
FontSize="14"
Background="#1E1E1E"
Foreground="#D4D4D4"/>
</Window>
2. 在 ViewModel 中使用
using AvaloniaConsole;
public class MainViewModel : ViewModelBase
{
public ConsoleBuffer ConsoleBuffer { get; } = new ConsoleBuffer();
public void AddLog()
{
ConsoleBuffer.AddLine($"日志信息 {DateTime.Now}");
}
public async Task RunProcess()
{
await Task.Run(() =>
{
for (int i = 0; i < 10000; i++)
{
ConsoleBuffer.AddLine($"处理进度: {i}/10000");
}
});
}
}
3. 基本操作
// 添加单行
ConsoleBuffer.AddLine("Hello World");
// 添加多行
ConsoleBuffer.AddLines(new[] { "Line 1", "Line 2", "Line 3" });
// 清空内容
ConsoleBuffer.Clear();
// 获取行数
int count = ConsoleBuffer.Count;
// 访问特定行
string line = ConsoleBuffer[0];
📖 API 文档
ConsoleControl 属性
| 属性 | 类型 | 默认值 | 描述 |
|---|---|---|---|
Buffer |
ConsoleBuffer? |
null |
绑定的数据缓冲区 |
Background |
IBrush? |
null |
背景色 |
Foreground |
IBrush? |
null |
前景色(文字颜色) |
FontFamily |
FontFamily |
系统默认 | 字体 |
FontSize |
double |
12 | 字体大小 |
FontStyle |
FontStyle |
Normal |
字体样式 |
FontWeight |
FontWeight |
Normal |
字体粗细 |
ConsoleBuffer 方法
| 方法 | 描述 |
|---|---|
AddLine(string line) |
添加一行文本(线程安全) |
AddLines(IEnumerable<string> lines) |
批量添加多行(线程安全) |
Clear() |
清空所有内容 |
Count |
获取总行数 |
this[int index] |
通过索引访问特定行 |
ConsoleBuffer 事件
| 事件 | 描述 |
|---|---|
LinesAdded |
当添加新行或清空时触发 |
🎨 样式示例
暗色主题 (VS Code 风格)
<ac:ConsoleControl Background="#1E1E1E"
Foreground="#D4D4D4"
FontFamily="Cascadia Code, Consolas, Monospace"
FontSize="14"/>
亮色主题
<ac:ConsoleControl Background="White"
Foreground="Black"
FontFamily="Consolas, Courier New, monospace"
FontSize="12"/>
自定义颜色
<ac:ConsoleControl Background="#282C34"
Foreground="#ABB2BF"
FontFamily="JetBrains Mono, monospace"
FontSize="13"/>
⌨️ 键盘快捷键
- Ctrl+C - 复制选中的文本到剪贴板
🖱️ 鼠标操作
- 滚轮 - 向上/向下滚动内容
- 拖动选择 - 按住左键拖动选择文本
- 滚动条拖动 - 拖动滚动条快速定位
- 点击滚动条轨道 - 快速跳转到对应位置
🔧 高级用法
在后台线程添加日志
// 从任意线程安全地添加日志
await Task.Run(() =>
{
for (int i = 0; i < 100000; i++)
{
ConsoleBuffer.AddLine($"后台任务 {i}");
if (i % 1000 == 0)
Thread.Sleep(10); // 模拟耗时操作
}
});
多个控制台实例
<Grid RowDefinitions="*, *">
<ac:ConsoleControl Grid.Row="0"
Buffer="{Binding MainConsole}"
Background="#1E1E1E"
Foreground="LightGray"/>
<ac:ConsoleControl Grid.Row="1"
Buffer="{Binding ErrorConsole}"
Background="#2D1E1E"
Foreground="#FF6B6B"/>
</Grid>
监听新内容事件
ConsoleBuffer.LinesAdded += (sender, e) =>
{
Console.WriteLine("新内容已添加!");
};
🎯 完整示例
查看 Demo 项目 获取完整的示例代码。
运行示例:
cd src/AvaloniaConsole.Demo
dotnet run
🔍 性能
- ✅ 轻松处理 100 万行+ 日志
- ✅ 虚拟化渲染,只绘制可见行
- ✅ 内存占用随实际内容线性增长
- ✅ 流畅的 60 FPS 滚动体验
- ✅ 无 UI 线程阻塞
🛠️ 技术细节
架构设计
- 数据层:
ConsoleBuffer- 线程安全的数据存储 - UI 层:
ConsoleControl- 自定义控件,虚拟化渲染 - 滚动: 自定义实现,避免 Avalonia 滚动系统的性能问题
- 选择: 基于坐标的文本选择,支持跨行
性能优化
- 只渲染可见区域内的文本行
- 使用
ReaderWriterLockSlim保证线程安全 - 字符尺寸预计算,避免重复测量
- 自定义滚动条,避免递归更新
🤝 贡献
欢迎提交 Issue 和 Pull Request!
📄 许可证
本项目采用 MIT 许可证。
🙏 致谢
灵感来源于 AvalonStudio 的控制台实现。
📮 联系方式
如有问题或建议,请提交 GitHub Issue。
Made with ❤️ for Avalonia Community
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- Avalonia (>= 11.3.8)
- Avalonia.Themes.Fluent (>= 11.3.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.