Issues with Avalonia Clipboard and DataGrid

Issues with Avalonia Clipboard and DataGrid

Records two issues resolved in recent Avalonia desktop software development: clipboard copy crash, DataGrid lag on tab switching, analyzing root causes and providing solutions

Last updated 1/11/2026 12:23 PM
沙漠尽头的狼
5 min read
Category
.NET Avalonia UI
Tags
.NET C# Avalonia UI Desktop Development Avalonia

Here are two typical issues encountered and resolved while developing a desktop application based on Avalonia, hoping to provide a reference for developers facing similar problems.

1. Clipboard Copy Exception (AOT Crash / COM Exception in Debug)

Symptom

When performing a Ctrl+C copy operation on text in TextBox or SelectedTextBlock controls, two types of exceptions occur:

  • The AOT published program crashes directly; check the application event log:

  • In Debug mode, the global exception is not caught, and the Main function in Program throws a clear COM exception:
System.Runtime.InteropServices.COMException: "CoInitialize has not been called. (0x800401F0 (CO_E_NOTINITIALIZED))"

Root Cause

After examining the code, the core issue was found in the definition of the Main function in Program.cs — the entry point was declared as async Task instead of the synchronous type required by Avalonia:

Changing async Task to void fixed the issue. The Avalonia documentation: Application Lifetimes actually explains this:

Principle Analysis

As a desktop UI framework, Avalonia has a strong dependency on the SynchronizationContext of the main thread. System-level operations such as clipboard and window message loop must be executed based on a stable main thread context.

  • async Task Main allows the CLR to manage the main thread lifetime asynchronously, breaking the synchronization context during Avalonia initialization, leading to the COM exception when clipboard operations call CoInitialize.
  • The Avalonia official documentation clearly states that the application entry stage (Main function) should not rely on an unready SynchronizationContext; therefore, the entry function must be synchronous.

2. DataGrid Lag When Switching Tabs

Scenario

The project uses the Dock control (VS-style layout, where Document is equivalent to a TabControl TabItem). A DataGrid is embedded in the Document to display data:

  • Very small data volume (only tens of rows, 8-9 columns);
  • Running environment: Win7 / Windows Server 2019; colleagues on Win10 also report lag;
  • Symptom: In Debug mode, switching tabs lags for about 2 seconds; after AOT publishing, the lag worsens to 3-4 seconds;
  • Comparison: When a single window directly displays a DataGrid loading hundreds of thousands of rows (15 columns), real-time refresh is very smooth.

Investigation Process

  1. Initially suspected an issue with how the DataGrid was used: optimized column bindings (only one-way binding), removed complex styles/animations — no significant improvement.
  2. Suspected Dock control performance issues: updated Dock control version, tried older versions — problem persisted.
  3. Ruled out Dock control: replaced with native TabControl for testing — the lag still occurred.
  4. Community help: after reporting in the Avalonia discussion group, members suggested trying TreeDataGrid instead of DataGrid. After replacement, the lag issue was completely resolved.

DataGrid is an early port of the WPF (referencing Silverlight) style component. Its "cell-level control + passive virtualization" design, when switching tabs with small data volumes, amplifies the overhead of control instantiation/layout in Win7/AOT environments. In contrast, TreeDataGrid is a new component refactored by the Avalonia team, with a "row-level rendering + forced virtualization" design that fundamentally reduces switching overhead. The official team has clearly recommended using TreeDataGrid to replace DataGrid. For discussion, click here.

It should be noted that TreeDataGrid has been placed by Avalonia into the commercial suite.

When using the free version, it is inevitable that new bugs may not be fixed (the official team has pushed maintenance of the old version to the open-source community branch). For the free version of Avalonia.Controls.TreeDataGrid, versions 11.1.1 and earlier are available:

The corresponding version of the theme library provided by Semi is 11.1.1.1.

Summary

  1. The Avalonia entry function Main must be declared as synchronous; async Task will break the synchronization context, leading to exceptions in system-level operations such as clipboard.
  2. DataGrid has performance shortcomings in tab-switching scenarios; using the officially recommended TreeDataGrid can resolve the lag issue.
  3. Old Avalonia components (like DataGrid) are prone to exposing performance issues on legacy systems (Win7/Server) with AOT publishing. Prioritizing the official new components can reduce pitfalls. Remember to update component libraries in a timely manner.

Finally, thanks to the members of the Avalonia discussion group for providing solutions. Communication in the open-source community always helps quickly locate such "counter-intuitive" problems.

Keep Exploring

Related Reading

More Articles