Skip to content

fix(control): Re-measure when Template changes - #26556

Merged
MartinZikmund merged 2 commits into
unoplatform:masterfrom
chmolto:fix/control-template-affects-measure
Nov 24, 2026
Merged

MartinZikmund merged 2 commits into
unoplatform:masterfrom
chmolto:fix/control-template-affects-measure

Conversation

@chmolto

@chmolto chmolto commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #25217

PR Type:

Bugfix

What changed?

Setting Template on a control that is already loaded left the control blank. OnTemplateChanged removes the old template root, but TemplateProperty was registered without AffectsMeasure. Nothing scheduled a measure pass, so ApplyTemplate never ran for the new template. A comment said the flag was applied conditionally in SetUpdateControlTemplate, but that method no longer exists.

Control.TemplateProperty now has AffectsMeasure. This matches WinUI, where Control_Template is registered with AffectMeasure, and matches how ContentControl.ContentTemplate is already declared here.

I added Given_Control.When_Template_Changed_After_Loaded, which is the repro from the issue. It fails on master because the button has no children after the template is restored, and passes with this change. I ran the runtime tests on Skia Desktop (Win32) only. Given_Control, Given_Button, Given_ContentControl and Given_Style all pass. A wider subset of 13 control/layout test classes gave the same results with and without the change, apart from the new test.

PR Checklist

  • Added Runtime tests, UI tests, or a manual test sample (for bug fixes / features, if applicable)
  • Docs have been added/updated following the documentation template (for bug fixes / features): N/A, bug fix with no API or documented behaviour change
  • Validated PR Screenshots Compare Test Run results. Will check once the pipeline has run on this PR.
  • Contains NO breaking changes
  • Reviewed 2 other open pull requests (optional but appreciated!)

Control.Template was missing the AffectsMeasure flag it has in WinUI,
so replacing the template of a loaded control removed the old template
root but never scheduled a measure pass, and the new template was not
applied until something else invalidated the control.

Fixes unoplatform#25217
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@mergify

mergify Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@MartinZikmund

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.

@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 247500 found UI Test snapshots differences: skia-linux-screenshots: 225, skia-windows-screenshots: 264

Details
  • skia-linux-screenshots: 225 changed over 2500

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • Accessibility_Showcase.png-dark
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • Border_CornerRadius_Binding.png-dark
    • Border_CornerRadius_Binding.png
    • Border_CornerRadius_BorderThickness.png-dark
    • Border_CornerRadius_BorderThickness.png
    • AnimatedVisualsTestbedPage.png-dark
    • AutomationProperties_AutomationId.png
    • AutomationProperties_AutomationId.png-dark
    • Basics.png-dark
    • Basics.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Accessibility_Showcase.png
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_StructureAndProps.png
    • BasicAcrylicBrushTest.png-dark
    • BasicAcrylicBrushTest.png
    • Buttons.png-dark
  • skia-windows-screenshots: 264 changed over 2500

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_StructureAndProps.png
    • Basics.png-dark
    • Basics.png
    • Border_CornerRadius_BorderThickness.png-dark
    • Border_CornerRadius_BorderThickness.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • AnimatedBackVisualSourceSamplePage.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • ColorPickerSample.png-dark
    • ColorPickerSample.png
    • AnimatedVisualsTestbedPage.png
    • AutomationProperties_AutomationId.png-dark
    • Buttons.png-dark
    • Buttons.png
    • AnimatedVisualsTestbedPage.png-dark
    • AppWindowTitleBar Properties.png
    • AutomationProperties_AutomationId.png

@MartinZikmund MartinZikmund 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.

@chmolto thanks for tracking this one down, and for cleaning up that stale SetUpdateControlTemplate comment while you were at it!

The Control.cs change looks right to me:

  • WinUI parity: Control_Template carries MetaDataPropertyInfoFlags::AffectMeasure in dxaml/xcp/components/metadata/StaticMetadata.g.cpp, and CControl::SetValue (dxaml/xcp/core/core/elements/Control.cpp) only removes the old template child. The next measure then re-applies the template through CFrameworkElement::ApplyTemplate, which is guarded by HasTemplateChild(). With this change Uno does the same.
  • Startup cost: nothing extra happens there. Elements start out with MeasureDirty set (DEFAULT_STARTING_LAYOUTFLAGS), so a Template set through a Style before the first measure makes InvalidateMeasure() return straight away. The template is still applied once, because ApplyTemplate is guarded by HasTemplateChild(). Re-setting the same template instance doesn't raise a change at all.

One thing needs fixing before merge: the new test fails on the native WinUI lane (Tests - WinAppSDK Runtime Tests, build 247500). It is the only failure in that run:

Assert.AreEqual failed. Expected:<1>. Actual:<0>. ... The new template was not applied
   at Given_Control.When_Template_Changed_After_Loaded()

See the inline comment. The ~225/264 screenshot diffs are the usual baseline noise (other PRs from today show the same counts), so you can ignore those.

With a null Template, WinUI's ContentControl creates default visuals for
string content, so the Button round trip did not behave the same on the
native WinUI lane. Use a non-content control and switch to a different
template instead.
@chmolto

chmolto commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Pushed the updated test in b871055. Could you kick off the pipeline again when you have a moment, so the WinAppSDK lane can confirm it?

@MartinZikmund MartinZikmund 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.

@chmolto thanks! Swapping to a plain Control with two distinct templates sidesteps the ContentControl default-visuals difference on WinUI, and the explicit 40 -> 60 width check now proves the second template was really applied and measured. Looks good to me, pending the WinAppSDK lane confirming it on the next run.

@MartinZikmund

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.

@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 247851 found UI Test snapshots differences: skia-linux-screenshots: 254, skia-windows-screenshots: 270

Details
  • skia-linux-screenshots: 254 changed over 2500

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedVisualsTestbedPage.png
    • AppWindowTitleBar Properties.png-dark
    • Basics.png-dark
    • Basics.png
    • BasicAcrylicBrushTest.png-dark
    • BasicAcrylicBrushTest.png
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_StructureAndProps.png
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • Accessibility_Showcase.png
    • AnimatedVisualsTestbedPage.png-dark
    • AppWindowTitleBar Properties.png
    • AutomationProperties_AutomationId.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • Border_CornerRadius_BorderThickness.png-dark
    • Border_CornerRadius_BorderThickness.png
  • skia-windows-screenshots: 270 changed over 2500

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedVisualsTestbedPage.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • Border_CornerRadius_BorderThickness.png-dark
    • Border_CornerRadius_BorderThickness.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Buttons.png-dark
    • AutomationProperties_AutomationId.png-dark
    • AutomationProperties_AutomationId.png
    • ClipboardTests.png-dark
    • ClipboardTests.png
    • Accessibility_Showcase.png-dark
    • AnimatedBackVisualSourceSamplePage.png
    • AnimatedVisualsTestbedPage.png-dark
    • Basics.png-dark
    • Basics.png
    • AnimatedBackVisualSourceSamplePage.png-dark
    • ColorPickerSample.png-dark
    • ColorPickerSample.png

@MartinZikmund
MartinZikmund merged commit 00477f6 into unoplatform:master Nov 24, 2026
48 checks passed
@welcome

welcome Bot commented Nov 24, 2026

Copy link
Copy Markdown

🎉 Congratulations on getting your very first pull request merged into Uno Platform! This is a huge milestone — you are now officially part of the community of contributors helping shape the future of cross-platform .NET development, and your code will reach developers and apps all over the world. 🌍

We are genuinely grateful for the time and care you put into this contribution. Every PR — big or small — makes Uno Platform better for everyone, and we hope this is the first of many. We cannot wait to see what you build next! 🚀

As a small thank-you, we would love to send you some swag so you can wear your contributor status with pride. And if you have any feedback or ideas on how we can make contributing to Uno Platform even better, please open a feedback issue — we truly value your perspective.

giphy
Welcome to the family, and merci beaucoup! 👋❤️

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.

Setting Control.Template on a loaded control never applies the new template (Control.Template is missing AffectsMeasure)

4 participants