Repository navigation
Description
What would you like to be added? 💡
When a XAML file uses a type or member marked [RequiresUnreferencedCode], the app should get a trim warning that points at that XAML. Today it gets none.
The XAML generator puts a blanket suppression on the code it generates for a XAML file:
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Generated code")]
[UnconditionalSuppressMessage("Trimming", "IL2111", Justification = "Generated code")](AnalyzerSuppressionsGenerator.GenerateTrimExclusions, in src/SourceGenerators/SourceGeneratorHelpers/Helpers/AnalyzerSuppressions.cs.) It is emitted on:
InitializeComponent- the generated
__<File>_<n>builder classes - the x:Bind
Bindingsclass GlobalStaticResources- the single-time resource initializers
- the lazy/template apply methods
So creating [RequiresUnreferencedCode] types, and setting or binding to [RequiresUnreferencedCode] members, is silent in XAML. This holds for both the Roslyn trim analyzer and ILLink.
Proposal:
- In the XAML generator, detect objects created in XAML whose type or parameterless constructor has
[RequiresUnreferencedCode]. - Also detect members set from XAML that carry it: property setters, attached property
Set*accessors, and event handlers. - Report each one as a generator warning located at the XAML element or attribute. It should carry the attribute's
MessageandUrl, like IL2026 does. - Only report when trim analysis is on (
EnableTrimAnalyzer,PublishTrimmedorIsTrimmable), so apps that don't trim see no change. - Keep the blanket
UnconditionalSuppressMessagefor code the generator owns. The new warning replaces the signal that the suppression hides, at a location the user can act on.
Open questions:
- Warning ID: a new
UXAMLdiagnostic, or IL2026 itself through a generated, unsuppressed call site?- A
UXAMLdiagnostic can point at the.xamlline, butNoWarn IL2026won't cover it. - IL2026 matches the rest of the trimming story, but it would land in the generated
.g.csfile.
- A
- Other Requires* attributes: do
[RequiresDynamicCode](IL3050) and[RequiresAssemblyFiles](IL3002) need the same treatment for NativeAOT? - XamlReader:
XamlReader.Loadis out of scope. It is a runtime, reflection-based path.
Why is this needed? 🤔
Libraries mark APIs [RequiresUnreferencedCode] so that apps using them get warned before trimming breaks them. For example, CallMethodAction, ChangePropertyAction, DataTriggerBehavior and EventTriggerBehavior in Uno.Microsoft.Xaml.Behaviors.Interactivity.WinUI are marked that way.
These types are almost always used from XAML, and the XAML use is exactly what the generator suppresses. Using them from C# (new EventTriggerBehavior()) does warn as expected.
Before #26630, an app referencing the Behaviors package did get IL2026 for these types. But the warnings came from BindableMetadata.g.cs, which emits an activator for every bindable type in referenced assemblies, whether the app uses it or not. They fired even when the app used none of the behaviors. #26630 suppresses that noise. Neither before nor after that PR does an app that does use the behaviors in XAML get a warning it can act on. This issue tracks closing that gap.
Repro: an app on Uno.Sdk 7.0.22 targeting net10.0-desktop, with EnableTrimAnalyzer=true, referencing Uno.Microsoft.Xaml.Behaviors.Interactivity.WinUI 4.0.1. Use EventTriggerBehavior with CallMethodAction in MainPage.xaml:
<Button Content="Click">
<i:Interaction.Behaviors>
<i:EventTriggerBehavior EventName="Click">
<i:CallMethodAction TargetObject="{x:Bind}" MethodName="OnClicked" />
</i:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Button>- Expected: a trim warning pointing at the two elements.
- Actual: no warning for the XAML use. (Before fix(trimming): Suppress IL2026 on bindable activators #26630, only the 4 unrelated
BindableMetadata.g.cswarnings.)
For which platforms 📱💻🖥️
- All platforms 🌍
- Build tasks
Anything else we need to know? 💬
- Code generated by Uno itself, such as the bindable metadata activators,
Build()and the generator's own glue, should stay suppressed. Those warnings aren't actionable by the app. - Related: fix(trimming): Suppress IL2026 on bindable activators #26630 (suppresses the bindable activator IL2026).
Activity
Sergio0694 commentedon Nov 23, 2026
Can you open this issue in WinUI? I can address this there first and then we can backport it to Uno. It'd be much better if these warnings were kept in sync between the two, rather than being Uno-only.