Repository navigation
fix(itemsrepeater): Accept a custom IElementFactory as ItemTemplate - #26324
Conversation
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]>
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
|
The build 245552 found UI Test snapshots differences: Details
|
This comment was marked as outdated.
This comment was marked as outdated.
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]>
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
|
The build 245628 found UI Test snapshots differences: Details
|
|
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]>
511d41e to
c077b3a
Compare
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
|
The build 245671 found UI Test snapshots differences: Details
|
morning4coffe-dev
left a comment
There was a problem hiding this comment.
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:
DataTemplateSelector.GetElementthrowsInvalidOperationExceptionwhen nothing is selected, and the comment says this matchesItemTemplateWrapper. It matches neither source:DataTemplateSelector::GetElementImplreturns a null result (S_OK), whileItemTemplateWrapper::GetElementthrowshresult_invalid_argument("Null encountered as data template…"), i.e. anArgumentException. For a 1:1 port it should return null (with a nullable return) or throw theArgumentException.IsRealizedisn't maintained.DataTemplate::GetElementImplcallsInitVirtualizationInformation(),SetIsRealized(true)andSetSelectedTemplate(this), andRecycleElementImplsetsIsRealized(false). Here only the origin template is recorded (RecyclePool.SetOriginTemplate). Probably harmless throughItemsRepeater, but worth confirming nothing readsIsRealizedafter a directDataTemplate.GetElement.- The new
DataTemplate.ElementFactory.cs/DataTemplateSelectorheaders cite the C++ file withouttagandcommit..claude/rules/code-style.mdasks for// MUX Reference <file>, tag winui3/release/1.8.4, commit dc46907e. ElementFactory/ElementFactoryShimAdapterkeepIElementFactoryShimnext toMicrosoft.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.
GitHub Issue: closes unoplatform/uno-private#2561
PR Type:
🐞 Bugfix
What changed? 🚀
Before:
ItemsRepeater.ItemTemplate(andItemsView.ItemTemplate, which forwards to its inner repeater) only accepted Uno's internalIElementFactoryShim, aDataTemplateor aDataTemplateSelector. Any otherMicrosoft.UI.Xaml.IElementFactorythrewArgumentException: ItemTemplate, while WinUI accepts any implementation. On Skia, theMicrosoft.UI.XamlElementFactoryGetArgs/ElementFactoryRecycleArgs, andDataTemplate/DataTemplateSelector's ownGetElement/RecycleElement, were generated stubs that threw.BreadcrumbBarthrewInvalidCastExceptionon a custom factory, andNavigationView/RadioButtonsignored one.After:
Microsoft.UI.Xaml.IElementFactoryis what everything consumes, as in WinUI 3.IElementFactoryShim(a WinUI 2 leftover) stays public and is adapted into it.ElementFactoryGetArgs(Data,Parent, plus the internalIndexWinUI also has) andElementFactoryRecycleArgs(Element,Parent) are implemented.IElementFactoryis declared outsideGenerated/.DataTemplate/DataTemplateSelectorimplementIElementFactory, ported from WinUI'sDataTemplate_Partial.cpp/DataTemplateSelector_Partial.cpp: a template keeps the elements given back throughRecycleElement, prefers one recycled from the same parent (or none), and takes a reused element out of its oldPanel. 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×0Rectangle(ItemTemplateWrapper's existing fallback; WinUI dereferences null), and a selector that picks no template throwsInvalidOperationException(WinUI returns null).IElementFactory:ItemsRepeater's template wrapper,ViewManager(now passing theMicrosoft.UI.Xamlargs),ItemTemplateWrapper, and theBreadcrumbBar,NavigationViewandRadioButtonsitem factories. The template is resolved in WinUI 3's order everywhere:DataTemplate/DataTemplateSelectorthroughItemTemplateWrapper, any otherIElementFactoryas-is, then (Uno only) anIElementFactoryShimthroughElementFactoryShimAdapter.RadioButtonsalso accepts aDataTemplateSelectornow, as WinUI 3 does.ElementFactoryimplementsIElementFactory. Its publicGetElementCore/RecycleElementCoreoverrides keep the shim args, so no public signature changes. Uno's own factories override WinUI-shaped internal overloads that take theMicrosoft.UI.Xamlargs, and the shim members translate between the two (re-entrant safe, no per-call allocation).// TODO Uno:onIElementFactoryShimstates the end state (drop it and theMicrosoft.UI.Xaml.Controlsargs, move the overrides to theMicrosoft.UI.Xamlargs as in WinUI), and shorter TODOs mark the adapter, the translation and each shim branch.Tests
IElementFactoryonItemsRepeater(realize,Data/Parent, recycling exactly once, template swap back to aDataTemplate, args cleared after the call, recycling with a nullParentwhen a non-virtualizing layout resets), onItemsView, onBreadcrumbBarandRadioButtons;RadioButtonswith aDataTemplateSelector; a factory that hands its calls to aDataTemplate;DataTemplate/DataTemplateSelectorGetElement/RecycleElement; Uno only: a plainIElementFactoryShim,ItemsViewwith aRecyclingElementFactory, and a custom factory set asItemTemplateduring a layout pass (the dynamic template update path inItemsRepeater.Templates.cs; WinUI throws there).ArgumentException: ItemTemplate,NotImplementedExceptioninside layout,BreadcrumbBarInvalidCastException,RadioButtonsfactory/selector ignored.Given_ItemsRepeater,Given_DataTemplate,Given_DataTemplateSelector,Given_BreadcrumbBar,Given_RadioButtons,Given_NavigationViewand the MUXRepeaterTests,ItemsViewTests,NavigationViewTests,RadioButtonsTests; thenGiven_ItemsRepeaterwith the tests added afterwards: 31 run, 0 failed, 2 existing skips./winui-runtime-tests): every new test that compiles there passes, includingParentbeing the repeater on recycle, and null on a non-virtualizing reset.Not covered / not in this PR
ItemTemplatechanges during a layout pass, an element the old template already realized is later recycled into the new one (seen with aDataTemplate→ custom factory swap). Not asserted, not changed here.NullReferenceException), where WinUI dereferences null natively; no guard added, for parity.Generated/edits (#if falseon[NotImplemented], "Skipping already declared" comments) were applied by hand, following f95b674, not by a run of the sync generator.PR Checklist ✅
Screenshots Compare Test Runresults.🤖 Generated with Claude Code