Skip to content

ItemsRepeater OnElementPrepared is called after NavigationViewItem.OnApplyTemplate #4689

Open

Description

@MartinZikmund

Current behavior

In UWP, the NavigationVIewItem.SetNavigationViewParent method is called before OnApplyTemplate, which is needed to properly initialize the items and NavigationVIewItemPresenters. The SetNavigationViewParent is called from OnRepeaterElementPrepared handler, which unfortunately runs after item's OnApplyTemplate executes in Uno (reproduced on Android, WASM, Skia, probably happens on others too). In addition, I noticed that when I run the HierarchicalNavigationViewMarkup test page (in NavigationView section of samples app), an assert fails in VirtualizationInfo.MoveOwnershipToLayoutFromElementFactory, maybe it is related somehow. Currently I have worked around the SetNavigationViewParent problem by walking up the Visual tree (see the NavigationViewItemBase.FindAncestorNavigationView method), but that does help on Skia and WASM only, whereas on Android and iOS, the parent is not yet set when OnApplyTemplate is called.

The problem is likely related to the lifetime events order.

Expected behavior

OnElementPrepared should be called before OnApplyTemplate.

How to reproduce it (as minimally and precisely as possible)

See above.

Workaround

Currently worked around in ItemsRepeater and NavigationView source.

Environment

Nuget Package:

  • Uno.UI / Uno.UI.WebAssembly / Uno.UI.Skia
  • Uno.WinUI / Uno.WinUI.WebAssembly / Uno.WinUI.Skia
  • Uno.SourceGenerationTasks
  • Uno.UI.RemoteControl / Uno.WinUI.RemoteControl
  • Other:

Nuget Package Version(s):

Affected platform(s):

  • iOS
  • Android
  • WebAssembly
  • WebAssembly renderers for Xamarin.Forms
  • macOS
  • Skia
    • WPF
    • GTK (Linux)
    • Tizen
  • Windows
  • Build tasks
  • Solution Templates

IDE:

  • Visual Studio 2017 (version: )
  • Visual Studio 2019 (version: )
  • Visual Studio for Mac (version: )
  • Rider Windows (version: )
  • Rider macOS (version: )
  • Visual Studio Code (version: )

Relevant plugins:

  • Resharper (version: )

Anything else we need to know?

Activity

  1. added
    kind/bug
    triage/untriaged
    project/third-party 3️⃣
    and removed
    triage/untriaged
    on Dec 8, 2020
  2. MartinZikmund commented on Dec 30, 2020

    @MartinZikmund
    MemberAuthor

    The OnApplyTemplate order has effects on NavigationViewPresenter as well (which is nested in the NavigationViewItem)

  3. 9 remaining items

  4. MartinZikmund commented on Jul 28, 2025

    @MartinZikmund
    MemberAuthor

    Likely fixed for Skia, but needs to be verified

  5. MartinZikmund commented on Apr 8, 2026

    @MartinZikmund
    MemberAuthor

    Repro generator

    Runtime test(s) added in branch dev/mazi/repro/4689:

    • src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Repeater/Given_ItemsRepeater.cs → When_ElementPrepared_Then_FiredBeforeOnApplyTemplate

    ⚠️ The test(s) pass on current master — the bug may have been fixed since the original report in 2020. The test verifies that ElementPrepared fires before OnApplyTemplate for elements in an ItemsRepeater, using a custom ContentControl subclass that tracks event ordering. On the current Skia target, the ordering is correct (ElementPrepared first, then OnApplyTemplate).

    Note: The original issue was specifically about NavigationViewItem inside NavigationView's internal repeater. The test uses a simpler standalone ItemsRepeater scenario — the NavigationView-specific interaction may still have edge cases not covered by this test.

  6. self-assigned this
    on Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

difficulty/challenging 🤯has-repro-branchkind/bugproject/layout 🧱project/navigation-lifecycle 🧬project/third-party 3️⃣

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions