Native AOT
Native AOT (Ahead-of-Time) compilation is supported in XPF. Unlike WPF, XPF does not use COM marshalling, which allows it to be compatible with AOT compilation. Large applications using third-party control libraries can be successfully compiled with Native AOT.
Project configuration
Add PublishAot to your .csproj.
<PropertyGroup>
<PublishAot>true</PublishAot>
</PropertyGroup>
Publishing
To publish your app, run dotnet publish in the command line:
dotnet publish -r <runtime> -c Release
As an example, dotnet publish -r osx-arm64 -c Release would publish the app for Apple Silicon devices.
For more information, please see Native AOT deployment and dotnet publish on the .NET documentation site.
Trimming by the Native AOT linker
To use AOT with XPF, trimming and linking must be conservative.
By default, the XPF SDK ships with an rd.xml root descriptor that force-includes XPF runtime libraries, including all built-in WPF assemblies and any user-executable assemblies on which Xpf.Sdk is set.
However, if your application references third-party WPF libraries, they must have equivalent trimming configurations. Otherwise, the Native AOT linker may remove types that are only referenced from XAML, which it cannot detect as being in use.
If you are experiencing runtime errors due to missing types, check if you are using any third-party libraries that do not ship with a root descriptor. Add a root descriptor for these to your .csproj file.
<ItemGroup>
<ProjectReference Include="..\YourAssembly\YourAssembly.csproj" />
<TrimmerRootAssembly Include="YourAssembly" />
</ItemGroup>
For information, please see Trimming on the .NET documentation site.
See also
- Native AOT (Avalonia): AOT setup for standard Avalonia applications
- Windows Deployment
- macOS Deployment
- Linux Deployment
- Performance Optimization