Problems with Avalonia clipboard and DataGrid

Problems with Avalonia clipboard and DataGrid

Record two problems recently solved by Avalonia desktop software development: clipboard copying crash and Tab switching DataGrid stuck, analyze the root causes and give solutions

最后更新 1/11/2026 12:23 PM
沙漠尽头的狼
预计阅读 4 分钟
分类
.NET Avalonia UI
标签
.NET C# Avalonia UI desktop development Avalonia

Record two typical problems encountered and solved recently when developing desktop applications based on Avalonia, hoping to provide reference for developers who encounter similar problems.

1. clipboard copy exception (AOT crash/debugging throw COM exception)

problem phenomenon

项目中对TextBoxSelectedTextBlock控件的文本执行Ctrl+C复制操作时,出现两种异常表现:

  • The AOT release and running program crashes directly. Check the application events:

  • Debug调试模式下,未捕获全局异常,ProgramMain函数会抛出明确的COM异常:
System.Runtime.InteropServices.COMException:“尚未调用 CoInitialize。 (0x800401F0 (CO_E_NOTINITIALIZED))”

root location

排查代码后发现核心问题出在Program.csMain函数定义上——将入口函数声明为async Task类型,而非Avalonia要求的同步:

async Task改为void修复,Avalonia 文档:Application Lifetimes 其实有说明:

principle analysis

Avalonia作为桌面UI框架,对主线程的SynchronizationContext(同步上下文)有强依赖,剪贴板、窗口消息循环等系统级操作均需基于稳定的主线程上下文执行。

  • async Task Main会让CLR以异步方式管理主线程生命周期,破坏Avalonia初始化的同步上下文,导致剪贴板操作时触发CoInitialize未调用的COM异常;
  • Avalonia官方文档明确说明:应用入口阶段(Main函数)不应依赖未就绪的SynchronizationContext,因此入口函数必须为同步。

2. Tab Switch DataGrid stuck

problem scenario

项目使用Dock控件(VS风格布局,Document等价于TabControl的TabItem),在Document中嵌入DataGrid展示数据:

  • The amount of data is extremely small (only dozens of items, 8-9 fields);
  • The running environment is Win7/Windows Server 2019, and the feedback from colleagues in Win10 is also poor;
  • Phenomenon: The Tab stuck for about 2 seconds after the Debug mode switch, and the stuck stuck intensified to 3-4 seconds after the release of AOT;
  • 对比:单窗口直接展示DataGrid加载几十万条数据(15个字段)时,实时刷新很流畅。

process of investigation

  1. 初期怀疑DataGrid使用方式问题:优化列绑定(仅单向绑定)、移除复杂样式/动画,卡顿无明显改善;
  2. Suspected performance problems with the Dock control: Update the Dock control version and switch to the old version, but the problem remains;
  3. 排除Dock控件影响:改用原生TabControl测试,卡顿问题复现;
  4. 社区求助:在Avalonia交流群反馈后,群友提示尝试TreeDataGrid替代DataGrid,替换后卡顿问题完全解决。

DataGrid是早期移植的WPF(链接指Silverlight)风格组件,“Cell级控件+被动虚拟化”的设计在小数据量+Tab切换重建时,控件实例化/布局开销被Win7/AOT环境放大;而TreeDataGrid是Avalonia团队重构的新组件,“行级渲染+强制虚拟化”的设计从根源上降低了切换开销,且官方已明确推荐使用TreeDataGrid替代DataGrid,讨论请点击链接

这里要明确,TreeDataGrid已被Avalonia放入商用套件

使用免费版,必然会有新发现Bug得不到修复的情况(官方把旧版维护推给了开源社区分支),Avalonia.Controls.TreeDataGrid免费版可使用11.1.1及以前版本:

Semi提供的配套主题库对应版本为11.1.1.1

summary

  1. Avalonia入口函数Main必须声明为同步,async Task会破坏同步上下文,导致剪贴板等系统级操作异常;
  2. DataGrid在Tab切换场景下存在性能短板,优先使用官方推荐的TreeDataGrid可解决卡顿问题;
  3. Old components of Avalonia (such as DataGrid) are prone to exposure performance problems in the old system (Win7/Server)+AOT release environment. Giving priority to official new components can reduce the number of setbacks. Remember: update the latest control library in a timely manner.

Finally, thanks to the solutions provided by the friends of the Avalonia Communication Group, exchanges in the open source community can always quickly locate such "counter-intuitive" problems ~

Keep Exploring

延伸阅读

更多文章