Skip to content

Report trim warnings for [RequiresUnreferencedCode] types and members used in XAML #26661

Open

Description

@Xiaoy312

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 Bindings class
  • 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 Message and Url, like IL2026 does.
  • Only report when trim analysis is on (EnableTrimAnalyzer, PublishTrimmed or IsTrimmable), so apps that don't trim see no change.
  • Keep the blanket UnconditionalSuppressMessage for 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 UXAML diagnostic, or IL2026 itself through a generated, unsuppressed call site?
    • A UXAML diagnostic can point at the .xaml line, but NoWarn IL2026 won't cover it.
    • IL2026 matches the rest of the trimming story, but it would land in the generated .g.cs file.
  • Other Requires* attributes: do [RequiresDynamicCode] (IL3050) and [RequiresAssemblyFiles] (IL3002) need the same treatment for NativeAOT?
  • XamlReader: XamlReader.Load is 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>

For which platforms 📱💻🖥️

  • All platforms 🌍
  • Build tasks

Anything else we need to know? 💬

Activity

  1. added
    kind/enhancement
    triage/untriaged
    on Nov 23, 2026
  2. Sergio0694 commented on Nov 23, 2026

    @Sergio0694

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/enhancementtriage/untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions