
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
Mainfunction inProgramthrows 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 Mainallows the CLR to manage the main thread lifetime asynchronously, breaking the synchronization context during Avalonia initialization, leading to the COM exception when clipboard operations callCoInitialize.- 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
DataGridloading hundreds of thousands of rows (15 columns), real-time refresh is very smooth.
Investigation Process
- Initially suspected an issue with how the
DataGridwas used: optimized column bindings (only one-way binding), removed complex styles/animations — no significant improvement. - Suspected Dock control performance issues: updated Dock control version, tried older versions — problem persisted.
- Ruled out Dock control: replaced with native
TabControlfor testing — the lag still occurred. - Community help: after reporting in the Avalonia discussion group, members suggested trying
TreeDataGridinstead ofDataGrid. 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
- The Avalonia entry function
Mainmust be declared as synchronous;async Taskwill break the synchronization context, leading to exceptions in system-level operations such as clipboard. DataGridhas performance shortcomings in tab-switching scenarios; using the officially recommendedTreeDataGridcan resolve the lag issue.- 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.