Skip to content

fix(itemsrepeater): Accept a custom IElementFactory as ItemTemplate - #26324

Merged
Xiaoy312 merged 9 commits into
masterfrom
dev/xygu/20261111/repeater-xaml-element-factory
Nov 17, 2026
Merged

Xiaoy312 merged 9 commits into
masterfrom
dev/xygu/20261111/repeater-xaml-element-factory

Conversation

@Xiaoy312

@Xiaoy312 Xiaoy312 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue: closes unoplatform/uno-private#2561

PR Type:

🐞 Bugfix

What changed? 🚀

Before: ItemsRepeater.ItemTemplate (and ItemsView.ItemTemplate, which forwards to its inner repeater) only accepted Uno's internal IElementFactoryShim, a DataTemplate or a DataTemplateSelector. Any other Microsoft.UI.Xaml.IElementFactory threw ArgumentException: ItemTemplate, while WinUI accepts any implementation. On Skia, the Microsoft.UI.Xaml ElementFactoryGetArgs/ElementFactoryRecycleArgs, and DataTemplate/DataTemplateSelector's own GetElement/RecycleElement, were generated stubs that threw. BreadcrumbBar threw InvalidCastException on a custom factory, and NavigationView/RadioButtons ignored one.

After: Microsoft.UI.Xaml.IElementFactory is what everything consumes, as in WinUI 3. IElementFactoryShim (a WinUI 2 leftover) stays public and is adapted into it.

  • Args: ElementFactoryGetArgs (Data, Parent, plus the internal Index WinUI also has) and ElementFactoryRecycleArgs (Element, Parent) are implemented. IElementFactory is declared outside Generated/.
  • DataTemplate / DataTemplateSelector implement IElementFactory, ported from WinUI's DataTemplate_Partial.cpp / DataTemplateSelector_Partial.cpp: a template keeps the elements given back through RecycleElement, prefers one recycled from the same parent (or none), and takes a reused element out of its old Panel. The selector picks the template for the item (falling back to the null-container overload) and recycles an element into the template that produced it. Two deliberate divergences, since the C# signature is non-nullable: an empty template returns a 0×0 Rectangle (ItemTemplateWrapper's existing fallback; WinUI dereferences null), and a selector that picks no template throws InvalidOperationException (WinUI returns null).
  • The repeater and its factories hold IElementFactory: ItemsRepeater's template wrapper, ViewManager (now passing the Microsoft.UI.Xaml args), ItemTemplateWrapper, and the BreadcrumbBar, NavigationView and RadioButtons item factories. The template is resolved in WinUI 3's order everywhere: DataTemplate/DataTemplateSelector through ItemTemplateWrapper, any other IElementFactory as-is, then (Uno only) an IElementFactoryShim through ElementFactoryShimAdapter. RadioButtons also accepts a DataTemplateSelector now, as WinUI 3 does.
  • ElementFactory implements IElementFactory. Its public GetElementCore/RecycleElementCore overrides keep the shim args, so no public signature changes. Uno's own factories override WinUI-shaped internal overloads that take the Microsoft.UI.Xaml args, and the shim members translate between the two (re-entrant safe, no per-call allocation).
  • Removing the shim is left for a breaking release: a // TODO Uno: on IElementFactoryShim states the end state (drop it and the Microsoft.UI.Xaml.Controls args, move the overrides to the Microsoft.UI.Xaml args as in WinUI), and shorter TODOs mark the adapter, the translation and each shim branch.

Tests

  • New: custom IElementFactory on ItemsRepeater (realize, Data/Parent, recycling exactly once, template swap back to a DataTemplate, args cleared after the call, recycling with a null Parent when a non-virtualizing layout resets), on ItemsView, on BreadcrumbBar and RadioButtons; RadioButtons with a DataTemplateSelector; a factory that hands its calls to a DataTemplate; DataTemplate/DataTemplateSelector GetElement/RecycleElement; Uno only: a plain IElementFactoryShim, ItemsView with a RecyclingElementFactory, and a custom factory set as ItemTemplate during a layout pass (the dynamic template update path in ItemsRepeater.Templates.cs; WinUI throws there).
  • Red on the code before each fix: ArgumentException: ItemTemplate, NotImplementedException inside layout, BreadcrumbBar InvalidCastException, RadioButtons factory/selector ignored.
  • Skia desktop: 184 passed / 0 failed across Given_ItemsRepeater, Given_DataTemplate, Given_DataTemplateSelector, Given_BreadcrumbBar, Given_RadioButtons, Given_NavigationView and the MUX RepeaterTests, ItemsViewTests, NavigationViewTests, RadioButtonsTests; then Given_ItemsRepeater with the tests added afterwards: 31 run, 0 failed, 2 existing skips.
  • Native WinUI (/winui-runtime-tests): every new test that compiles there passes, including Parent being the repeater on recycle, and null on a non-virtualizing reset.

Not covered / not in this PR

  • Pre-existing, Uno only: when ItemTemplate changes during a layout pass, an element the old template already realized is later recycled into the new one (seen with a DataTemplate → custom factory swap). Not asserted, not changed here.
  • A custom factory returning null still fails inside layout (NullReferenceException), where WinUI dereferences null natively; no guard added, for parity.
  • The Generated/ edits (#if false on [NotImplemented], "Skipping already declared" comments) were applied by hand, following f95b674, not by a run of the sync generator.

PR Checklist ✅

🤖 Generated with Claude Code

Xiaoy312 and others added 3 commits November 11, 2026 17:31
Microsoft.UI.Xaml.ElementFactoryGetArgs (Data, Parent) and
ElementFactoryRecycleArgs (Element, Parent) were generated stubs whose
getters threw, so a custom Microsoft.UI.Xaml.IElementFactory could not
read its arguments. Implement them, and declare IElementFactory's
members outside the generated folder.

Part of unoplatform/uno-private#2561

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ItemsRepeater only accepted an IElementFactoryShim, a DataTemplate or a
DataTemplateSelector, and threw ArgumentException for any other
Microsoft.UI.Xaml.IElementFactory, which WinUI accepts. ItemsView, which
hands its ItemTemplate to its repeater, rendered nothing.

Wrap such a factory in an IElementFactoryShim adapter, in both
OnItemTemplateChanged and the dynamic template update path. The adapter
reuses one args instance per call type and clears it after each call,
so it doesn't keep the last item alive. DataTemplate and
DataTemplateSelector keep their existing path.

Part of unoplatform/uno-private#2561

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ItemsRepeater: items are realized, GetElement gets each item and the
repeater as Parent, and resetting the source recycles every element with
the repeater as Parent. ItemsView: the containers from the factory are
realized. Both fail on master (ArgumentException, ItemsView never loads).

Part of unoplatform/uno-private#2561

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 245552 found UI Test snapshots differences: skia-linux-screenshots: 226, skia-windows-screenshots: 345

Details
  • skia-linux-screenshots: 226 changed over 2496

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png-dark
    • AnimatedVisualsTestbedPage.png-dark
    • Basics.png-dark
    • Basics.png
    • Border_CornerRadius_Binding.png-dark
    • Border_CornerRadius_Binding.png
    • CompositionPath.png-dark
    • CompositionPath.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • BitmapIcon_Foreground.png-dark
    • BitmapIcon_Foreground.png
    • AppWindowTitleBar Properties.png-dark
    • AnimatedBackVisualSourceSamplePage.png
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_StructureAndProps.png
    • BreadcrumbBarPage.png-dark
    • BreadcrumbBarPage.png
  • skia-windows-screenshots: 345 changed over 2496

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Basics.png-dark
    • Basics.png
    • AutomationProperties_AutomationId.png-dark
    • AutomationProperties_AutomationId.png
    • Border_CornerRadius_Binding.png-dark
    • Border_CornerRadius_Binding.png
    • Border_CornerRadius_BorderThickness.png-dark
    • Border_CornerRadius_BorderThickness.png
    • ClipboardTests.png-dark
    • ClipboardTests.png
    • ComboBox_MaxDropdownHeight.png-dark
    • ComboBox_MaxDropdownHeight.png
    • AnimatedBackVisualSourceSamplePage.png
    • BreadcrumbBarPage.png-dark
    • BreadcrumbBarPage.png
    • AnimatedBackVisualSourceSamplePage.png-dark
    • AnimatedVisualsTestbedPage.png-dark
    • BasicAcrylicBrushTest.png-dark
    • BasicAcrylicBrushTest.png
    • CornerRadiusControls.png-dark

@Xiaoy312 Xiaoy312 added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 12, 2026
@Xiaoy312

This comment was marked as outdated.

Xiaoy312 and others added 5 commits November 11, 2026 23:24
DataTemplate.GetElement/RecycleElement and the DataTemplateSelector pair
were generated stubs on Skia, so a custom IElementFactory that hands its
calls to a DataTemplate threw NotImplementedException inside layout.

Port WinUI's DataTemplate_Partial.cpp: a template keeps the elements
given back through RecycleElement, prefers one recycled from the same
parent (or none), and takes a reused element out of its old Panel. The
selector picks the template for the item (falling back to the
null-container overload) and recycles an element into the template that
produced it.

Part of unoplatform/uno-private#2561

Co-Authored-By: Claude Opus 5.5 <[email protected]>
DataTemplate: loads new content, reuses a recycled element once, prefers
one from the same parent, and removes a reused element from its old
panel. DataTemplateSelector: uses the selected template, recycles into
it, and falls back to the null-container overload. ItemsRepeater: a
custom factory that hands its calls to a DataTemplate realizes items
(failed before with NotImplementedException during layout).

Part of unoplatform/uno-private#2561

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ElementFactoryShim

WinUI 3's ItemsRepeater, ViewManager, ItemTemplateWrapper and the
BreadcrumbBar, NavigationView and RadioButtons item factories hold and
call Microsoft.UI.Xaml.IElementFactory. Uno still went through its
WinUI 2-era IElementFactoryShim and the Microsoft.UI.Xaml.Controls
args, and wrapped a custom IElementFactory into that shim.

Make IElementFactory the one they consume, as in WinUI:
- ElementFactory implements IElementFactory. Its public overrides keep
  the shim args, so the built-in factories override WinUI-shaped
  internal overloads, and the shim members translate.
- The template is resolved in WinUI 3's order everywhere: DataTemplate
  and DataTemplateSelector through ItemTemplateWrapper, any other
  IElementFactory as-is, then (Uno only) an IElementFactoryShim through
  ElementFactoryShimAdapter, now adapting the shim into IElementFactory.
- ViewManager passes the Microsoft.UI.Xaml args, which carry the
  internal Index the test hooks read.

As a result, BreadcrumbBar no longer throws InvalidCastException for a
custom IElementFactory, NavigationView and RadioButtons no longer ignore
one, and RadioButtons now accepts a DataTemplateSelector as WinUI does.

IElementFactoryShim and its args stay public; TODO Uno comments mark
them, the adapter and the translation for removal.

Part of unoplatform/uno-private#2561

Co-Authored-By: Claude Opus 5.5 <[email protected]>
BreadcrumbBar and RadioButtons use the items a custom IElementFactory
creates, and RadioButtons the template a DataTemplateSelector picks.
ItemsView takes a RecyclingElementFactory as its IElementFactory
ItemTemplate. A plain IElementFactoryShim as an ItemsRepeater
ItemTemplate is still realized and recycled through the adapter.

Part of unoplatform/uno-private#2561

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…recycling

Replacing a custom IElementFactory ItemTemplate with a DataTemplate
recycles every element back into the factory. The args a factory gets
are cleared once the call returns. Each realized element is recycled
exactly once and the new source is shown, retried rather than read
after a single idle wait.

Part of unoplatform/uno-private#2561

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 245628 found UI Test snapshots differences: skia-linux-screenshots: 234, skia-windows-screenshots: 315

Details
  • skia-linux-screenshots: 234 changed over 2496

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedVisualsTestbedPage.png
    • AutomationProperties_AutomationId.png-dark
    • AutomationProperties_AutomationId.png
    • BasicAcrylicBrushTest2.png-dark
    • BasicAcrylicBrushTest2.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • Border_CornerRadius_Binding.png-dark
    • Border_CornerRadius_Binding.png
    • BreadcrumbBarPage.png-dark
    • BreadcrumbBarPage.png
    • Border_CornerRadius_BorderThickness.png-dark
    • Border_CornerRadius_BorderThickness.png
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • ClipboardTests.png-dark
    • CompositionPath.png-dark
    • ComboBox_MaxDropdownHeight.png-dark
    • CompositionPath.png
    • ComboBox_MaxDropdownHeight.png
  • skia-windows-screenshots: 315 changed over 2496

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png-dark
    • AnimatedVisualsTestbedPage.png
    • AppWindowTitleBar Properties.png
    • BasicAcrylicBrushTest2.png-dark
    • BasicAcrylicBrushTest2.png
    • CompositionPath.png-dark
    • CompositionPath.png
    • AnimatedVisualsTestbedPage.png-dark
    • AutomationProperties_AutomationId.png-dark
    • AutomationProperties_AutomationId.png
    • AutomationProperties_StructureAndProps.png-dark
    • BasicAcrylicBrushTest.png-dark
    • BasicAcrylicBrushTest.png
    • AppWindowTitleBar Properties.png-dark
    • BreadcrumbBarPage.png-dark
    • BreadcrumbBarPage.png
    • AnimatedBackVisualSourceSamplePage.png
    • ClipboardTests.png
    • ComboBox_MaxDropdownHeight.png-dark
    • ComboBox_MaxDropdownHeight.png

@Xiaoy312 Xiaoy312 removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 12, 2026
@Xiaoy312
Xiaoy312 marked this pull request as ready for review November 12, 2026 07:14
@Xiaoy312
Xiaoy312 enabled auto-merge November 12, 2026 07:14
@mergify

mergify Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

…ycled without owner

- Uno only: ItemTemplate swapped to a custom IElementFactory from ElementPrepared goes through
  ItemsRepeater.Templates.cs, which realizes the next items from it (WinUI throws there).
- A NonVirtualizingLayout reset recycles each element once with a null Parent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Xiaoy312
Xiaoy312 force-pushed the dev/xygu/20261111/repeater-xaml-element-factory branch from 511d41e to c077b3a Compare November 12, 2026 07:28
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 245671 found UI Test snapshots differences: skia-linux-screenshots: 234, skia-windows-screenshots: 302

Details
  • skia-linux-screenshots: 234 changed over 2496

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • AnimatedBackVisualSourceSamplePage.png-dark
    • BreadcrumbBarPage.png-dark
    • BreadcrumbBarPage.png
    • AnimatedVisualsTestbedPage.png
    • CompositionPath.png-dark
    • CompositionPath.png
    • DamageRegion_ShadowChildShape.png-dark
    • DamageRegion_ShadowChildShape.png
    • AnimatedBackVisualSourceSamplePage.png
    • AppWindowTitleBar Properties.png
    • AutomationProperties_StructureAndProps.png-dark
    • Basics.png-dark
    • Basics.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Buttons.png-dark
    • Buttons.png
    • CompositionNineGridBrush_Source_Changes.png-dark
  • skia-windows-screenshots: 302 changed over 2496

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png
    • AutomationProperties_AutomationId.png-dark
    • Border_CornerRadius_Binding.png-dark
    • Border_CornerRadius_Binding.png
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • AnimatedVisualsTestbedPage.png-dark
    • BasicAcrylicBrushTest.png-dark
    • BasicAcrylicBrushTest.png
    • CompositionPath.png-dark
    • CompositionPath.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • BreadcrumbBarPage.png-dark
    • BreadcrumbBarPage.png
    • ClipboardTests.png-dark
    • ClipboardTests.png
    • ComboBox_MaxDropdownHeight.png-dark
    • ComboBox_MaxDropdownHeight.png
    • AutomationProperties_AutomationId.png

@morning4coffe-dev morning4coffe-dev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Compared against DataTemplate_Partial.cpp, DataTemplateSelector_Partial.cpp, ItemTemplateWrapper.cpp and ItemsRepeater.cpp (winui3/release/1.8.4, dc46907e). DataTemplate.GetElement/RecycleElement follow the C++ pooling (same-parent-or-orphan preference, reparenting out of the old panel, last element as fallback), and OnItemTemplateChanged has the same branch order with the comment kept verbatim. Differences:

  1. DataTemplateSelector.GetElement throws InvalidOperationException when nothing is selected, and the comment says this matches ItemTemplateWrapper. It matches neither source: DataTemplateSelector::GetElementImpl returns a null result (S_OK), while ItemTemplateWrapper::GetElement throws hresult_invalid_argument("Null encountered as data template…"), i.e. an ArgumentException. For a 1:1 port it should return null (with a nullable return) or throw the ArgumentException.
  2. IsRealized isn't maintained. DataTemplate::GetElementImpl calls InitVirtualizationInformation(), SetIsRealized(true) and SetSelectedTemplate(this), and RecycleElementImpl sets IsRealized(false). Here only the origin template is recorded (RecyclePool.SetOriginTemplate). Probably harmless through ItemsRepeater, but worth confirming nothing reads IsRealized after a direct DataTemplate.GetElement.
  3. The new DataTemplate.ElementFactory.cs / DataTemplateSelector headers cite the C++ file without tag and commit. .claude/rules/code-style.md asks for // MUX Reference <file>, tag winui3/release/1.8.4, commit dc46907e.
  4. ElementFactory/ElementFactoryShimAdapter keep IElementFactoryShim next to Microsoft.UI.Xaml.IElementFactory. WinUI 3 has no shim, so this isn't 1:1; the TODO and breaking-change note are clear, so I'd just confirm the removal is planned for 7.0.

@Xiaoy312
Xiaoy312 merged commit 2a44387 into master Nov 17, 2026
52 checks passed
@Xiaoy312
Xiaoy312 deleted the dev/xygu/20261111/repeater-xaml-element-factory branch November 17, 2026 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants