Skip to content

[Android, iOS] Control templates are applied too early #190

Closed as not planned

Description

@dr1rrb

I'm submitting a...

  • Bug report (I searched for similar issues and did not find one)

Current behavior

If we instantiate a control and set the Template property, then this template is applied immediately (which leads to invoke the OnApplyTemplate in the control).

Expected behavior

On Windows the template of a control is being applied only when this control is in the visual tree and either we explicitly invoke the ApplyTemplate method, or we wait for the first measure pass.

Note: To get the full behavior of windows, we should be able to do:

var myControl = new MyControl { Template = _myControlTemplate };
_panelInTheVisualTree.Children.Add(myControl); // Template was not applied yet
myControl.ApplyTemplate(); // Will invoke `OnApplyTemplate` synchronously and returns `true`
_panelInTheVisualTree.Children.Remove(myControl); // `myControl` was not measured

But if the panel is not in the visual tree, myControl.ApplyTemplate() should not do anything and returns false

Minimal reproduction of the problem with instructions

var myControl = new MyControl { Template = _myControlTemplate };

Environment

Nuget Package: Uno.UI
Package Version(s): 1.25.0-dev.42

Affected platform(s):
- [x] iOS
- [x] Android
- [?] WebAssembly
- [ ] Windows
- [ ] Build tasks

Visual Studio : _Irrelevant_
Relevant plugins: _Irrelevant_

Activity

  1. self-assigned this
    on Sep 12, 2018
  2. added
    kind/bug
    on Sep 12, 2018
  3. dr1rrb commented on Sep 12, 2018

    @dr1rrb
    MemberAuthor

    This got mitigated by #192, but it's still not properly aligned with UWP.

  4. davidjohnoliver commented on Sep 27, 2018

    @davidjohnoliver
    Contributor

    ApplyTemplate() doesn't require the view to be in the visual tree, it just requires Template to be non-null. If the Control has an implicit style then its Template will be null on UWP until it's in the visual tree because the Style can't be resolved until that point. The root cause then is #119.

    For now the best workaround is to set FeatureConfiguration.Control.UseLegacyLazyApplyTemplate = true.

  5. added
    platform/android 🤖
    platform/ios 🍎
    on May 22, 2019
  6. peternary commented on Aug 6, 2019

    @peternary

    The specific issue we're encountering involves a custom control that wraps a ListView and accepts a DataTemplateSelector as a wrapper but includes a default DataTemplateSelector. By the time we have received the DataTemplateSelector the ListView has already been rendered using the default DataTemplateSelector.

  7. dr1rrb commented on Jun 19, 2020

    @dr1rrb
    MemberAuthor

    This is causing some troubles again with this:

    <Setter Property="DisplayTemplate" Value="{StaticResource templTextDisplay}" />
    <Setter Property="InputTemplate" Value="{StaticResource templTextInput}" />
    
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Fill="Red" Stretch="Fill" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    If in the callback of the DisplayTemplateProperty you do something like Template = DisplayTemplate, then the OnApplyTemplate will be invoked twice (once with the DisplayTemplate and a second time withe the template defined in the style for the Template property).

    On Windows the template is applied only on first measure, so the OnApplyTemplate is invoked only once (with the DisplayTemplate, but this is certainly due to #3398)

  8. modified the milestone: 3.1 on Aug 29, 2020
  9. added
    project/layout 🧱
    and removed on Sep 21, 2020
  10. added
    difficulty/tbd
    on Feb 15, 2021
  11. 1 remaining item

  12. added
    difficulty/medium 🤔
    on May 31, 2021
  13. added
    project/navigation-lifecycle 🧬
    on Oct 24, 2022
  14. added
    difficulty/challenging 🤯
    and removed
    difficulty/medium 🤔
    on May 1, 2023
  15. Youssef1313 commented on Nov 9, 2023

    @Youssef1313
    Member

    This was causing an issue in one of the tests that I worked around in #14368.

    For my case though, the issue is that control templates are not applied on the first measure pass, not that they are applied early.

    I added some code comments in #14368 that mentions some of the relevant parts of WinUI source code.

  16. self-assigned this
    on May 22, 2024
  17. removed their assignment
    on Jun 12, 2024
  18. changed the title [-]Control templates are applied too early[/-] [+][Non-Skia] Control templates are applied too early[/+] on Sep 16, 2024
  19. changed the title [-][Non-Skia] Control templates are applied too early[/-] [+][Android, iOS] Control templates are applied too early[/+] on Sep 16, 2024
  20. Youssef1313 commented on Oct 6, 2024

    @Youssef1313
    Member

    This will be easily fixable after #18261 is merged

  21. MartinZikmund commented on Jul 15, 2025

    @MartinZikmund
    Member

    This is only relevant to native renderer, closing. CC @Jen-Uno

  22. Youssef1313 commented on Jul 15, 2025

    @Youssef1313
    Member

    @MartinZikmund I'm curious. Are native renderers removed completely? Or they are there but are no longer a supported scenario and exists only for backcompat?

  23. github-actions commented on May 5, 2026

    @github-actions
    Contributor

    This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or feature requests.

  24. locked as resolved and limited conversation to collaborators on May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty/challenging 🤯kind/bugplatform/android 🤖platform/ios 🍎project/layout 🧱project/navigation-lifecycle 🧬

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions