Skip to content

Fix SwipeViews with invoked properties crash the app in Release mode - #35208

Merged
kubaflo merged 3 commits into
dotnet:inflight/currentfrom
BagavathiPerumal:fix-18055
May 6, 2026
Merged

Fix SwipeViews with invoked properties crash the app in Release mode#35208
kubaflo merged 3 commits into
dotnet:inflight/currentfrom
BagavathiPerumal:fix-18055

Conversation

@BagavathiPerumal

@BagavathiPerumal BagavathiPerumal commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Issue details

When a virtual XAML event handler is wired inside a DataTemplate (for example, SwipeItem.Invoked) and the handler is defined in a base class and overridden in a derived class, the app crashes in Release builds on iOS/macOS and may also behave incorrectly on Android. This occurs due to a bug in XamlC's IL code generator (SetPropertiesVisitor.cs), where the ldvirtftn instruction receives the wrong vtable object (the anonymous DataTemplate class instead of the root XAML element), causing runtime failure with Full AOT compilation.

Root Cause

The issue occurs because of incorrect IL generation in SetPropertiesVisitor.ConnectEvent() when wiring a virtual event handler inside a DataTemplate.

When XamlC compiles a DataTemplate, it generates an anonymous nested class (e.g., _anonXamlCDataTemplate_1) with a LoadDataTemplate() method where event wiring happens. For virtual handlers, XamlC must emit ldvirtftn, which requires the actual page object as the vtable source to resolve the correct overridden method.

The bug was that the code always pushed Ldarg_0 as the vtable object. Inside LoadDataTemplate(), Ldarg_0 is the anonymous class, not the root page. This causes ldvirtftn to look up the virtual method on the wrong vtable, so the override on the generic subclass is never found. On iOS Full AOT, this causes a hard crash; on Android and Windows, the JIT silently calls the base class method instead of the override.

Description of Change

The fix involves replacing the extra Ldarg_0 push before ldvirtftn with a Dup instruction, which reuses the delegate target object that is already correctly loaded on the stack just above this code.

The delegate target (the root page) is already on the stack, correctly resolved via context.Root for both the top-level and DataTemplate contexts. Dup copies that same object for ldvirtftn, ensuring virtual dispatch always targets the actual page's vtable, and the overridden method on the generic subclass is found correctly on all platforms.

Windows platform behavior:

The issue does not occur on the Windows platform. Based on analysis, even though Windows does not crash, the behavior before the fix was technically undefined, as it relied on the JIT being lenient. If the method resolution happened to pick the base class version instead of the override, the logic would silently be incorrect without any error. The fix makes the IL correct and explicit for all platforms.

Tested the behavior in the following platforms.

  • iOS
  • Mac
  • Android
  • Windows

Issues Fixed

Fixes #18055

Output

Before Issue Fix After Issue Fix
18055-BeforeFix.1.mov
18055-AfterFix.mov
@github-actions

github-actions Bot commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 35208

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 35208"
@dotnet-policy-service dotnet-policy-service Bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Apr 29, 2026

@MauiBot MauiBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expert Review — 3 findings

See inline comments for details.

Comment thread src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs Outdated
Comment thread src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs Outdated
Comment thread src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs
@MauiBot MauiBot added s/agent-changes-requested AI agent recommends changes - found a better alternative or issues s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review) labels May 3, 2026
…ftn in ConnectEvent() to ensure correct virtual event handler dispatch inside DataTemplates, preventing iOS/macOS Release mode crashes.
Comment thread src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs Outdated
Comment thread src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs Outdated
Comment thread src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs
@sheiksyedm
sheiksyedm marked this pull request as ready for review May 5, 2026 08:24
Copilot AI review requested due to automatic review settings May 5, 2026 08:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a XamlC IL generation bug affecting event hookups for virtual handlers inside DataTemplates. In Release/AOT scenarios (notably iOS/MacCatalyst Full AOT), ldvirtftn was being emitted with the wrong vtable source, which can crash or dispatch to the base implementation instead of the override.

Changes:

  • Update XamlC event hookup generation to use dup (reuse the already-loaded delegate target) before ldvirtftn, ensuring correct virtual dispatch.
  • Add an XAML unit test (Maui18055) validating that a virtual handler wired inside a DataTemplate dispatches to an override on a derived page type.
  • Add a minimal XAML repro template resource to exercise the event wiring path.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs Adjusts XamlC IL emission for virtual event handlers to use dup instead of pushing ldarg.0 as the vtable source.
src/Controls/tests/Xaml.UnitTests/Issues/Maui18055.xaml Adds a DataTemplate that wires an event to a virtual handler name.
src/Controls/tests/Xaml.UnitTests/Issues/Maui18055.xaml.cs Adds a regression test verifying override dispatch when the handler is virtual and defined on a base type.
Comment thread src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs Outdated
Comment thread src/Controls/tests/Xaml.UnitTests/Issues/Maui18055.xaml.cs Outdated
Comment thread src/Controls/tests/Xaml.UnitTests/Issues/Maui18055.xaml.cs Outdated
@dotnet dotnet deleted a comment from MauiBot May 5, 2026
@dotnet dotnet deleted a comment from MauiBot May 5, 2026
@dotnet dotnet deleted a comment from MauiBot May 5, 2026
@dotnet dotnet deleted a comment from MauiBot May 5, 2026

@MauiBot MauiBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expert Review — 2 findings

See inline comments for details.

@kubaflo
kubaflo changed the base branch from main to inflight/current May 6, 2026 09:30
@dotnet dotnet deleted a comment from MauiBot May 6, 2026
@dotnet dotnet deleted a comment from MauiBot May 6, 2026
@dotnet dotnet deleted a comment from MauiBot May 6, 2026

@MauiBot MauiBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expert Review — 2 findings

See inline comments for details.

@MauiBot MauiBot added the s/agent-fix-pr-picked AI could not beat the PR fix - PR is the best among all candidates label May 6, 2026
@kubaflo
kubaflo merged commit 49de947 into dotnet:inflight/current May 6, 2026
38 of 41 checks passed
@github-actions github-actions Bot added this to the .NET 10 SR7 milestone May 6, 2026
@dotnet dotnet deleted a comment from MauiBot May 7, 2026
@dotnet dotnet deleted a comment from MauiBot May 7, 2026
@dotnet dotnet deleted a comment from MauiBot May 7, 2026
@MauiBot

MauiBot commented May 7, 2026

Copy link
Copy Markdown
Collaborator

🤖 AI Summary

👋 @BagavathiPerumal — new AI review results are available. Please review the latest session below.

📊 Review Session000f1e9 · fix-18055-Changes updated. · 2026-05-07 12:14 UTC
🚦 Gate — Test Before & After Fix

Gate Result: ✅ PASSED

Platform: IOS · Base: main · Merge base: 1463c4c5

Test Without Fix (expect FAIL) With Fix (expect PASS)
📄 Maui18055 Maui18055 ✅ FAIL — 48s ✅ PASS — 43s
🔴 Without fix — 📄 Maui18055: FAIL ✅ · 48s

(truncated to last 15,000 chars)

ata-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui25309.xaml(21,9): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui25871.xaml(14,24): XamlC warning XC0045: Binding: Property "UpdateProgress" not found on "Microsoft.Maui.Controls.Xaml.UnitTests.Maui25871ViewModel". [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui25935.xaml(8,13): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31939.xaml(7,29): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31939.xaml(14,29): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31939.xaml(15,29): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(7,26): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(12,56): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(18,84): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(23,84): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(30,56): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(36,84): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(44,28): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui32056.xaml(9,9): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui32837.xaml(13,18): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui32924.xaml(14,20): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui32924.xaml(18,20): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui32924.xaml(22,20): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui32924.xaml(27,20): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui33291.xaml(20,29): XamlC warning XC0045: Binding: Property "BindingContext" not found on "Microsoft.Maui.Controls.Xaml.UnitTests.Maui33291Item". [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui33293.xaml(14,29): XamlC warning XC0045: Binding: Property "BindingContext" not found on "Microsoft.Maui.Controls.Xaml.UnitTests.Maui33293Product". [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui33876.xaml(8,20): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui34490.xaml(12,25): XamlC warning XC0045: Binding: Property "BindingContext" not found on "Microsoft.Maui.Controls.Xaml.UnitTests.Maui34490ItemModel". [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui6367.xaml(10,50): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui8149.xaml(12,43): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/RefToXamlControl.xaml(7,33): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/SetValue.xaml(20,26): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/SetValue.xaml(52,27): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/SetValue.xaml(53,27): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/WarnOnObsolete.xaml(6,25): XamlC warning XC0618: Property, Property setter or BindableProperty "ObsoleteBP" is deprecated. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/WarnOnObsolete.xaml(6,42): XamlC warning XC0618: Property, Property setter or BindableProperty "ObsoleteProp" is deprecated. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/WarnOnObsolete.xaml(6,61): XamlC warning XC0618: Property, Property setter or BindableProperty "ObsoletePropSetter" is deprecated. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/XReference.xaml(16,4): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/XReference.xaml(21,26): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/XReference.xaml(21,77): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Gh2007.rtxc.xaml(3,9): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
  Controls.Xaml.UnitTests -> /Users/cloudtest/vss/_work/1/s/artifacts/bin/Controls.Xaml.UnitTests/Debug/net10.0/Microsoft.Maui.Controls.Xaml.UnitTests.dll
Test run for /Users/cloudtest/vss/_work/1/s/artifacts/bin/Controls.Xaml.UnitTests/Debug/net10.0/Microsoft.Maui.Controls.Xaml.UnitTests.dll (.NETCoreApp,Version=v10.0)
VSTest version 18.0.1 (arm64)

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.8.2+699d445a1a (64-bit .NET 10.0.0)
[xUnit.net 00:00:00.15]   Discovering: Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:03.12]   Discovered:  Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:03.12]   Starting:    Microsoft.Maui.Controls.Xaml.UnitTests
The active test run was aborted. Reason: Test host process crashed

Test Run Aborted.

🟢 With fix — 📄 Maui18055: PASS ✅ · 43s

(truncated to last 15,000 chars)

me performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui25871.xaml(14,24): XamlC warning XC0045: Binding: Property "UpdateProgress" not found on "Microsoft.Maui.Controls.Xaml.UnitTests.Maui25871ViewModel". [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui25935.xaml(8,13): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31939.xaml(7,29): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31939.xaml(14,29): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31939.xaml(15,29): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(7,26): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(12,56): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(18,84): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(23,84): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(30,56): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(36,84): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui31995.xaml(44,28): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui32056.xaml(9,9): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui32837.xaml(13,18): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui32924.xaml(14,20): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui32924.xaml(18,20): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui32924.xaml(22,20): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui32924.xaml(27,20): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui33291.xaml(20,29): XamlC warning XC0045: Binding: Property "BindingContext" not found on "Microsoft.Maui.Controls.Xaml.UnitTests.Maui33291Item". [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui33293.xaml(14,29): XamlC warning XC0045: Binding: Property "BindingContext" not found on "Microsoft.Maui.Controls.Xaml.UnitTests.Maui33293Product". [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui33876.xaml(8,20): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui34490.xaml(12,25): XamlC warning XC0045: Binding: Property "BindingContext" not found on "Microsoft.Maui.Controls.Xaml.UnitTests.Maui34490ItemModel". [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui6367.xaml(10,50): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Maui8149.xaml(12,43): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/RefToXamlControl.xaml(7,33): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/SetValue.xaml(20,26): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/SetValue.xaml(52,27): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/SetValue.xaml(53,27): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/WarnOnObsolete.xaml(6,25): XamlC warning XC0618: Property, Property setter or BindableProperty "ObsoleteBP" is deprecated. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/WarnOnObsolete.xaml(6,42): XamlC warning XC0618: Property, Property setter or BindableProperty "ObsoleteProp" is deprecated. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/WarnOnObsolete.xaml(6,61): XamlC warning XC0618: Property, Property setter or BindableProperty "ObsoletePropSetter" is deprecated. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/XReference.xaml(16,4): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/XReference.xaml(21,26): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/XReference.xaml(21,77): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Issues/Gh2007.rtxc.xaml(3,9): XamlC warning XC0022: Binding could be compiled to improve runtime performance if x:DataType is specified. See https://learn.microsoft.com/dotnet/maui/fundamentals/data-binding/compiled-bindings for more information. [/Users/cloudtest/vss/_work/1/s/src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj]
  Controls.Xaml.UnitTests -> /Users/cloudtest/vss/_work/1/s/artifacts/bin/Controls.Xaml.UnitTests/Debug/net10.0/Microsoft.Maui.Controls.Xaml.UnitTests.dll
Test run for /Users/cloudtest/vss/_work/1/s/artifacts/bin/Controls.Xaml.UnitTests/Debug/net10.0/Microsoft.Maui.Controls.Xaml.UnitTests.dll (.NETCoreApp,Version=v10.0)
VSTest version 18.0.1 (arm64)

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.8.2+699d445a1a (64-bit .NET 10.0.0)
[xUnit.net 00:00:00.10]   Discovering: Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:02.49]   Discovered:  Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:02.49]   Starting:    Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:02.58]   Finished:    Microsoft.Maui.Controls.Xaml.UnitTests
  Passed VirtualHandlerInDataTemplateCallsOverride(inflator: Runtime) [54 ms]
  Passed VirtualHandlerInDataTemplateCallsOverride(inflator: XamlC) [< 1 ms]
  Passed VirtualHandlerInDataTemplateCallsOverride(inflator: SourceGen) [< 1 ms]

Test Run Successful.
Total tests: 3
     Passed: 3
 Total time: 2.8302 Seconds

📁 Fix files reverted (5 files)
  • .config/dotnet-tools.json
  • eng/Version.Details.xml
  • eng/Versions.props
  • eng/pipelines/ci-copilot.yml
  • src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs

🧪 UI Tests — Category Detection

Full UI test matrix will run (no specific categories detected from PR changes).


🔍 Pre-Flight — Context & Validation

Issue: #18055 - [iOS/Catalyst] Swipeviews with invoked properties crash the app in Release
PR: #35208 - Fix SwipeViews with invoked properties crash the app in Release mode
Platforms Affected: iOS, macOS (Full AOT); Android/Windows experience wrong dispatch silently
Files Changed: 1 implementation, 2 test

Key Findings

  • Root cause is in SetPropertiesVisitor.ConnectEvent() (XamlC IL code generator): when wiring a virtual event handler inside a DataTemplate, XamlC incorrectly pushed Ldarg_0 as the vtable object for ldvirtftn. Inside the anonymous DataTemplate method, Ldarg_0 is the anonymous nested class — not the root XAML element — so virtual dispatch resolves on the wrong vtable.
  • On iOS/macOS Full AOT this causes a hard crash; on Android/Windows the JIT silently calls the base class instead of the override.
  • Fix: Replace yield return Create(Ldarg_0) with yield return Create(Dup) before ldvirtftn. The delegate target (root page/element) is already correctly on the stack, so Dup reuses it.
  • Additional safety improvement: condition is now methodDef.IsVirtual && !methodDef.IsStatic to correctly handle C# 11+ interface static-abstract members (which are virtual but should use ldftn).
  • Test added: Maui18055.xaml + Maui18055.xaml.cs — XAML unit test using SubMaui18055 subclass to verify override is dispatched (not base).
  • CI: One check failing — maui-pr (Run Helix Unit Tests Windows Helix Unit Tests (Debug)). Release variant passes. This may be a flaky or pre-existing failure.

Code Review Summary

Verdict: LGTM
Confidence: high
Errors: 0 | Warnings: 1 | Suggestions: 1

Key code review findings:

  • ⚠️ src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs:1343 — The !methodDef.IsStatic guard (secondary fix for static-abstract interface members) has no regression test. Guard is correct but untested.
  • 💡 src/Controls/tests/Xaml.UnitTests/Issues/Maui18055.xaml.cs:25 — Only XamlInflator.XamlC actually exercises the fixed IL; Runtime and SourceGen inflators pass even without the fix. A comment noting this would help future maintainers.

Fix Candidates

# Source Approach Test Result Files Changed Notes
PR PR #35208 Replace Ldarg_0 with Dup before ldvirtftn; add !methodDef.IsStatic guard ✅ PASSED (Gate) SetPropertiesVisitor.cs, Maui18055.xaml, Maui18055.xaml.cs Original PR

🔬 Code Review — Deep Analysis

Code Review — PR #35208

Independent Assessment

What this changes: SetPropertiesVisitor.ConnectEvent() in the XamlC build task emits Dup instead of Ldarg_0 before ldvirtftn when wiring a virtual (non-static) event handler. It also adds a !methodDef.IsStatic guard to the IsVirtual branch so C# 11+ static abstract interface members fall through to ldftn.

Inferred motivation: In a DataTemplate context, XamlC generates an anonymous nested class (_anonXamlCDataTemplate_N) whose LoadDataTemplate() method handles event wiring. Inside that method, Ldarg_0 is the anonymous class itself — not the root page/XAML element. When ldvirtftn receives the anonymous class as its vtable source, virtual dispatch is broken: on JIT runtimes it silently calls the base class method rather than the override; on iOS/macOS Full AOT it crashes outright.

The fix reuses the delegate target that was already correctly pushed onto the stack (via context.Root resolution at lines 1330–1341) by duplicating it with Dup. This ensures ldvirtftn gets the correct vtable object in both DataTemplate and non-DataTemplate contexts.

IL stack analysis (confirming correctness):

// Before fix (DataTemplate, IsVirtual):
Ldloc parent       ; stack: [parent]
Ldarg_0            ; stack: [parent, anon_class]   ← WRONG vtable
Ldfld root_field   ; stack: [parent, anon_class, page] (hypothetical)
Ldarg_0            ; stack: [parent, page, anon_class]  ← pushed wrong object
Ldvirtftn method   ; pops anon_class — wrong vtable!

// After fix (DataTemplate, IsVirtual):
Ldloc parent       ; stack: [parent]
Ldarg_0 + Ldfld    ; stack: [parent, page]   ← correct root object
Dup                ; stack: [parent, page, page]
Ldvirtftn method   ; pops page for vtable lookup → stack: [parent, page, ftn]
Newobj delegate    ; takes (page, ftn) → stack: [parent, delegate]
Callvirt add_Event ; stack: []

The fix is IL-correct for both VariableDefinition root (non-DataTemplate, where LoadAs emits Ldloc) and FieldDefinition root (DataTemplate, where Ldarg_0 + Ldfld loads the page from a captured field).

Is the approach sound? Yes. Dup is the standard, minimal fix for this pattern. An alternative would be to store/reload via a local variable, but Dup is cleaner and doesn't introduce temporaries. The !methodDef.IsStatic guard is necessary because the static branch (lines 1325–1328) pushes LdnullDup + ldvirtftn on null would throw NullReferenceException.


Reconciliation with PR Narrative

Author claims: XamlC emits Ldarg_0 before ldvirtftn, which is wrong in DataTemplate context because Ldarg_0 is the anonymous DataTemplate class. Fix is to use Dup to reuse the delegate target already on the stack.

Agreement: My independent analysis fully agrees. The root cause, fix mechanism, and IL semantics described in the PR description are accurate. The author also correctly identifies that this is a crash on iOS/macOS Full AOT and silent wrong-method dispatch on JIT runtimes.

Minor discrepancy: The PR title says "Fix SwipeViews with invoked properties crash" — the fix is actually generic to any virtual event handler inside a DataTemplate, not just SwipeViews. The fix correctly applies to the general case.


Findings

⚠️ Warning — Static-abstract guard has no regression test

File: src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs:1343

The !methodDef.IsStatic guard prevents the Dup + ldvirtftn path for C# 11+ static abstract interface members (which are marked IsVirtual && IsStatic in metadata). Without this guard, those methods would go through Dup + ldvirtftn with a null target on the stack (because the static branch at lines 1325–1328 pushes Ldnull), causing a NullReferenceException at delegate creation.

The guard is correct, but there is no test covering this edge case. A future refactor could inadvertently remove the guard and reintroduce the crash silently. Consider adding a test with a static event handler (the existing EventsConnection test may already cover the static handler path — worth confirming).

💡 Suggestion — Clarify which inflator is the critical path in test comment

File: src/Controls/tests/Xaml.UnitTests/Issues/Maui18055.xaml.cs:25

[XamlInflatorData] runs with Runtime, XamlC, and SourceGen inflators. Only XamlC exercises the fixed IL path:

  • Runtime uses reflection-based XAML loading — CLR handles virtual dispatch correctly regardless.
  • SourceGen generates elementWithEvent.Clicked += HandleVirtualClicked as plain C# — the CLR resolves the virtual method normally.

The test is still valid (it guards against regressions in all three paths), but a brief comment noting XamlC is the critical inflator for this specific IL fix would help future maintainers understand why the test exists.


CI Status

maui-pr overall failed, with the specific failure being Run Helix Unit Tests Windows Helix.... The PR was merged despite this. Given that 36 of 39 checks passed (including all integration tests, all builds, and all other Helix jobs), and the PR is already merged into inflight/current, this failure appears to be an unrelated Helix infrastructure flake or pre-existing test issue. The Build Analysis failure is a downstream consequence of the Helix failure, not an independent issue.


Devil's Advocate

"Is the Dup approach safe in all non-DataTemplate contexts?"
In a non-DataTemplate scenario, context.Root is typically a VariableDefinition, and LoadAs emits Ldloc rootVar (for reference types). The value on the stack is the same object as before — Dup copies it correctly. Old code would push Ldarg_0 which IS the page in non-DataTemplate IL, so behavior is equivalent. ✓

"Could Dup corrupt the stack if context.Root is a FieldDefinition in a non-DataTemplate context?"
The FieldDefinition branch only occurs in DataTemplate context where the root page is captured as a field of the anonymous class. In that context, Ldarg_0 + Ldfld correctly loads the page, and Dup copies it. ✓

"Is IsVirtual && IsStatic actually possible in MAUI-targeted C#?"
C# 11+ interface static abstract members are marked IsVirtual in CLI metadata. While uncommon in XAML event handlers, the guard is correct and harmless. ✓

"Does the fix regress the non-DataTemplate virtual handler case?"
The existing EventsConnection tests cover virtual handlers (elementWithVirtualHandler) in non-DataTemplate context with XamlInflatorData. If the fix broke non-DataTemplate virtual handlers, those tests would fail. They pass. ✓


Verdict: LGTM

Confidence: high

Summary: The fix is IL-correct and surgically targeted. The Dup instruction correctly reuses the already-computed delegate target for ldvirtftn's vtable argument, fixing wrong-method dispatch on JIT and hard crashes on iOS/macOS Full AOT. The !methodDef.IsStatic guard is necessary and correct. The regression test is well-structured and exercises all three XamlC inflators, though only the XamlC path validates the fix. The one gap (no test for static abstract interface members as event handlers) is a minor omission that does not block merge.


🔧 Fix — Analysis & Comparison

Fix Candidates

# Source Approach Test Result Files Changed Notes
1 try-fix-1 Explicit context.Root reload before PASS 1 file Re-emits root-loading sequence for vtable
2 try-fix-2 Stloc/Ldloc temp variable to hold target twice PASS 1 file Saves delegate target to temp local, loads it twice
3 try-fix-3 DataTemplate-specific ldftn FAIL 1 file Breaks override ldftn always calls base dispatch demotion
4 try-fix-4 Upstream DataTemplate-context fix (context.Root as VariableDefinition in LoadDataTemplate PASS 1 file Fixes ILContext setup, not emission )
PR PR #35208 before + guard PASSED (Gate) 3 files Original PR

Cross-Pollination

Model Round New Ideas? Details
claude-opus-4.6 2 No new ideas All viable approaches covered: Dup (PR), explicit reload, stloc/ldloc, upstream context fix
claude-sonnet-4.6 2 No new ideas ldftn normalization already shown to break dispatch; no new angle
gpt-5.3-codex 2 No new ideas Attempts 1,2,4 collectively cover all viable vtable-fix strategies
gpt-5.4 2 No new ideas Upstream fix (attempt-4) is the most architectural; no improvements remain

Exhausted: Yes
Selected Fix: PR's fix (Dup + !IsStatic Simplest, most idiomatic IL, gate passed, LGTM from code review.guard)


📋 Report — Final Recommendation

✅ Final Recommendation: APPROVE

Phase Status

Phase Status Notes
Pre-Flight ✅ COMPLETE 3 files classified (1 implementation, 2 test)
Code Review LGTM (high) 0 errors, 1 warning, 1 suggestion
Gate ✅ PASSED ios (XamlC inflator)
Try-Fix ✅ COMPLETE 4 attempts: 3 passing, 1 failing
Report ✅ COMPLETE

Code Review Impact on Try-Fix

Code review found one moderate warning: the !methodDef.IsStatic guard has no regression test. This was addressed in the pr-plus-reviewer candidate by adding StaticHandlerInDataTemplateUsesLdftn. The code review's note that only XamlInflator.XamlC exercises the fixed IL path was also addressed with a clarifying comment. The try-fix exploration confirmed that Dup (PR's approach) is the simplest viable solution — alternatives (explicit reload, stloc/ldloc, upstream context fix) all pass tests but add more complexity. The ldftn demotion (attempt-3) was correctly ruled out as it breaks override dispatch.

Summary

PR #35208 fixes a long-standing iOS/macOS Full AOT crash caused by incorrect IL generation in XamlC's ConnectEvent() for virtual event handlers wired inside DataTemplates. The fix (Ldarg_0Dup before ldvirtftn) is minimal, correct, and well-explained. The pr-plus-reviewer candidate strengthens the PR by adding test coverage for the !methodDef.IsStatic guard and a clarifying comment for future maintainers. No try-fix candidate produced a simpler or better alternative — the PR's own approach is the most idiomatic.

Root Cause

In SetPropertiesVisitor.ConnectEvent(), when emitting IL for a virtual event handler, the code pushed Ldarg_0 as the vtable object for ldvirtftn. Inside a DataTemplate's LoadDataTemplate() method, Ldarg_0 is the anonymous nested class (not the root XAML element), causing virtual dispatch to resolve on the wrong vtable. On iOS/macOS Full AOT this is a hard crash; on JIT runtimes the base class method is silently called instead of the override.

Fix Quality

The fix is IL-correct: Dup copies the delegate target (root XAML element) already correctly pushed by the context.Root resolution above it, avoiding the need to reload it. The !methodDef.IsStatic secondary guard correctly handles C# 11+ static abstract interface members. The pr-plus-reviewer candidate (selected winner) adds a regression test for the static-abstract guard path and a comment noting the XamlC-critical inflator path — strictly improving on the PR without changing the core fix logic.


@MauiBot MauiBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expert Review — 2 findings

See inline comments for details.

}

if (methodDef.IsVirtual)
if (methodDef.IsVirtual && !methodDef.IsStatic)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[moderate] XAML and Bindings - Secondary fix (static-abstract guard) has no regression test.

The !methodDef.IsStatic guard prevents Dup + ldvirtftn for C# 11+ static abstract interface members. Without it, the old code would emit Ldarg_0 + ldvirtftn on a null delegate target (the static branch pushes Ldnull above), causing a NullReferenceException at delegate-creation time. The guard correctly routes these to ldftn, but there is no test covering it. Consider adding a test where a static abstract interface member is used as an event handler, to prevent a silent regression if this condition is ever relaxed.

// not the base class. Without the fix, XamlC emitted Ldarg_0 (the anonymous DataTemplate
// class) as the ldvirtftn vtable source — causing wrong dispatch on JIT and a hard crash
// on iOS/macOS Full AOT.
internal void VirtualHandlerInDataTemplateCallsOverride(XamlInflator inflator)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[minor] Regression Prevention - Only XamlInflator.XamlC exercises the IL fix.

[XamlInflatorData] drives this theory with all three inflators, but:

  • Runtime uses reflection-based LoadFromXaml; virtual dispatch is always correct via the CLR.
  • SourceGen emits elementWithEvent.Clicked += HandleVirtualClicked in generated C#; the CLR handles vtable lookup normally.

Neither path would fail without the Dup fix. Only XamlInflator.XamlC runs the rewritten InitializeComponentXamlC that contains the fixed IL. The test remains valuable for guarding SourceGen/Runtime regressions, but a comment noting that XamlC is the critical path for this specific fix would help future maintainers.

@MauiBot MauiBot added s/agent-approved AI agent recommends approval - PR fix is correct and optimal and removed s/agent-changes-requested AI agent recommends changes - found a better alternative or issues labels May 7, 2026
@kubaflo kubaflo added the s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix) label May 20, 2026
PureWeen pushed a commit that referenced this pull request Jun 2, 2026
…35208)

> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Issue details
When a virtual XAML event handler is wired inside a DataTemplate (for
example, SwipeItem.Invoked) and the handler is defined in a base class
and overridden in a derived class, the app crashes in Release builds on
iOS/macOS and may also behave incorrectly on Android. This occurs due to
a bug in XamlC's IL code generator (SetPropertiesVisitor.cs), where the
ldvirtftn instruction receives the wrong vtable object (the anonymous
DataTemplate class instead of the root XAML element), causing runtime
failure with Full AOT compilation.

### Root Cause

The issue occurs because of incorrect IL generation in
SetPropertiesVisitor.ConnectEvent() when wiring a virtual event handler
inside a DataTemplate.

When XamlC compiles a DataTemplate, it generates an anonymous nested
class (e.g., <InitializeComponent>_anonXamlCDataTemplate_1) with a
LoadDataTemplate() method where event wiring happens. For virtual
handlers, XamlC must emit ldvirtftn, which requires the actual page
object as the vtable source to resolve the correct overridden method.

The bug was that the code always pushed Ldarg_0 as the vtable object.
Inside LoadDataTemplate(), Ldarg_0 is the anonymous class, not the root
page. This causes ldvirtftn to look up the virtual method on the wrong
vtable, so the override on the generic subclass is never found. On iOS
Full AOT, this causes a hard crash; on Android and Windows, the JIT
silently calls the base class method instead of the override.
 
### Description of Change

The fix involves replacing the extra Ldarg_0 push before ldvirtftn with
a Dup instruction, which reuses the delegate target object that is
already correctly loaded on the stack just above this code.

The delegate target (the root page) is already on the stack, correctly
resolved via context.Root for both the top-level and DataTemplate
contexts. Dup copies that same object for ldvirtftn, ensuring virtual
dispatch always targets the actual page's vtable, and the overridden
method on the generic subclass is found correctly on all platforms.
 
**Windows platform behavior:**
 
The issue does not occur on the Windows platform. Based on analysis,
even though Windows does not crash, the behavior before the fix was
technically undefined, as it relied on the JIT being lenient. If the
method resolution happened to pick the base class version instead of the
override, the logic would silently be incorrect without any error. The
fix makes the IL correct and explicit for all platforms.

Tested the behavior in the following platforms.
 
- [x] iOS
- [x] Mac
- [ ] Android
- [ ] Windows

### Issues Fixed

Fixes #18055

### Output

| Before Issue Fix | After Issue Fix |
|----------|----------|
| <video width="270" height="600"
src="https://github.com/user-attachments/assets/6bc99f45-7114-4ce5-9a75-d98d3c014882">
| <video width="270" height="600"
src="https://github.com/user-attachments/assets/00cd3e91-cb1b-4a9a-9df9-1a2538d2ec56">
|
PureWeen pushed a commit that referenced this pull request Jun 11, 2026
…35208)

> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Issue details
When a virtual XAML event handler is wired inside a DataTemplate (for
example, SwipeItem.Invoked) and the handler is defined in a base class
and overridden in a derived class, the app crashes in Release builds on
iOS/macOS and may also behave incorrectly on Android. This occurs due to
a bug in XamlC's IL code generator (SetPropertiesVisitor.cs), where the
ldvirtftn instruction receives the wrong vtable object (the anonymous
DataTemplate class instead of the root XAML element), causing runtime
failure with Full AOT compilation.

### Root Cause

The issue occurs because of incorrect IL generation in
SetPropertiesVisitor.ConnectEvent() when wiring a virtual event handler
inside a DataTemplate.

When XamlC compiles a DataTemplate, it generates an anonymous nested
class (e.g., <InitializeComponent>_anonXamlCDataTemplate_1) with a
LoadDataTemplate() method where event wiring happens. For virtual
handlers, XamlC must emit ldvirtftn, which requires the actual page
object as the vtable source to resolve the correct overridden method.

The bug was that the code always pushed Ldarg_0 as the vtable object.
Inside LoadDataTemplate(), Ldarg_0 is the anonymous class, not the root
page. This causes ldvirtftn to look up the virtual method on the wrong
vtable, so the override on the generic subclass is never found. On iOS
Full AOT, this causes a hard crash; on Android and Windows, the JIT
silently calls the base class method instead of the override.
 
### Description of Change

The fix involves replacing the extra Ldarg_0 push before ldvirtftn with
a Dup instruction, which reuses the delegate target object that is
already correctly loaded on the stack just above this code.

The delegate target (the root page) is already on the stack, correctly
resolved via context.Root for both the top-level and DataTemplate
contexts. Dup copies that same object for ldvirtftn, ensuring virtual
dispatch always targets the actual page's vtable, and the overridden
method on the generic subclass is found correctly on all platforms.
 
**Windows platform behavior:**
 
The issue does not occur on the Windows platform. Based on analysis,
even though Windows does not crash, the behavior before the fix was
technically undefined, as it relied on the JIT being lenient. If the
method resolution happened to pick the base class version instead of the
override, the logic would silently be incorrect without any error. The
fix makes the IL correct and explicit for all platforms.

Tested the behavior in the following platforms.
 
- [x] iOS
- [x] Mac
- [ ] Android
- [ ] Windows

### Issues Fixed

Fixes #18055

### Output

| Before Issue Fix | After Issue Fix |
|----------|----------|
| <video width="270" height="600"
src="https://github.com/user-attachments/assets/6bc99f45-7114-4ce5-9a75-d98d3c014882">
| <video width="270" height="600"
src="https://github.com/user-attachments/assets/00cd3e91-cb1b-4a9a-9df9-1a2538d2ec56">
|
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 20, 2026
@github-actions github-actions Bot modified the milestones: .NET 10 SR7, .NET 10 SR9 Jul 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-swipeview SwipeView community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/ios platform/macos macOS / Mac Catalyst s/agent-approved AI agent recommends approval - PR fix is correct and optimal s/agent-fix-pr-picked AI could not beat the PR fix - PR is the best among all candidates s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix) s/agent-reviewed PR was reviewed by AI agent workflow (full 4-phase review)

5 participants