Skip to content

Loading, Loaded & Unloaded events are called too early #2895

Closed as not planned

Description

@MartinZikmund

Current behavior

The Loading and Loaded events are called immediately after the control is added in the visual tree.

Expected behavior

The controls start Loading and get Loaded only once the UI thread is freed up.

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

I have two examples that demonstrate the differences.

Frame navigation

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        Debug.WriteLine("Page constructor");
        Loading += MainPage_Loading;
        Loaded += MainPage_Loaded;
    }

    private void MainPage_Loading(FrameworkElement sender, object args)
    {            
        Debug.WriteLine("Page loading");
    }

    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        base.OnNavigatingFrom(e);
        Debug.WriteLine("Page navigating from");
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        Debug.WriteLine("Page navigated to");
    }

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("Page loaded");
    }
}


// navigating to the page
frame.Navigating += RootFrame_Navigating;
frame.Navigated += RootFrame_Navigated;
frame.NavigationStopped += RootFrame_NavigationStopped;
frame.Navigate(typeof(MainPage));
Debug.WriteLine("After Navigated");

The output is:

Frame navigating
Page constructor
Frame navigated
Page navigated to
After navigated
Page loading
Page loaded

Whereas in case of Uno it is:

Frame navigating
Page constructor
Page loading
Page loaded
Frame navigated
Page navigated to
After navigated

Creating & throwing away many controls

public partial class CustomControl : Control
{
    public CustomControl()
    {
        this.Loading += CustomControl_Loading;
        this.Loaded += CustomControl_Loaded;
    }

    private void CustomControl_Loading(
        Windows.UI.Xaml.FrameworkElement sender,
        object args)
    {
        Debug.WriteLine("Loading");
    }

    private void CustomControl_Loaded(
        object sender, 
        Windows.UI.Xaml.RoutedEventArgs e)
    {
        Debug.WriteLine("Loaded");
    }

    private void CustomControl_Unloaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        Debug.WriteLine("UnLoaded");
    }

    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
    }
}

// in button click handler
private void Button_Click(object sender, RoutedEventArgs e)
{
    for (int i = 0; i < 1000; i++)
    {
        var btn = new CustomControl();
        Content = btn;
    }
    Debug.WriteLine("After for loop");
}

Output on UWP:

After for loop
Loading
Loaded
999x Unloaded

Output on Uno:

Loading
Loaded
Unloaded
Loading
Loaded
Unloaded
... 997x the same
Unloaded
Loading
Loaded
After for loop

Environment

Nuget Package: 2.0

Package Version(s): 2.0

Affected platform(s):

  • iOS
  • Android
  • WebAssembly
  • WebAssembly renderers for Xamarin.Forms
  • macOS
  • Windows
  • Build tasks
  • Solution Templates

Activity

  1. added
    kind/bug
    triage/untriaged
    platform/android 🤖
    platform/ios 🍎
    platform/macos 🍏
    platform/wasm 🌐
    and removed
    triage/untriaged
    on Mar 27, 2020
  2. changed the title [-]Loading & Loaded events are called too early[/-] [+]Loading, Loaded & Unloaded events are called too early[/+] on Mar 27, 2020
  3. MartinZikmund commented on Mar 27, 2020

    @MartinZikmund
    MemberAuthor

    I have updated the issue with Unloaded event as well, as it is affected too.

  4. MartinZikmund commented on Mar 27, 2020

    @MartinZikmund
    MemberAuthor

    Out of curiosity I tried to implement this "delayed" Loading and Loaded reporting for iOS only in this branch. This implementation is surely not ideal solution, handles FrameworkElement only and does not fix Unloaded, but the Samples app runs fine even after this change.

    Essentially instead of raising OnLoading and OnLoaded immediately, they are enqueued and then batch executed when UI thread is available.

  5. nor0x commented on Apr 21, 2020

    @nor0x
    Contributor

    not sure if this is related, but with the current version the splash screen is not disappearing once the app is already loaded, i have tested this on WASM

    The following block doesn't get removed or hidden for me.

    <div id="uno-loading">
        <img src="Assets/SplashScreen.scale-200.png" id="uno-loading-splash" class="uno-splash">
    </div>
    
  6. MartinZikmund commented on May 2, 2020

    @MartinZikmund
    MemberAuthor

    @jeromelaban Is it realistic to resolve this without significant breaking changes? It is probably the main blocker for MvvmCross support. If it cannot be solved easily, I will check if MvvmCross.Uno code can be modified to circumvent this somehow.

  7. jeromelaban commented on May 4, 2020

    @jeromelaban
    Member

    It is realistic (and required) to fix this without breaking changes, but it would most likely be fixed properly by porting the original Frame code. I would suggest that, for now, modifying the MvvmCross code to adjust for the behavior is the safest course of action.

    Note that FrameworkElement.IsLoaded is available in WinUI and can be used to compensate for this behavior in a backward compatible way, once this is fixed in Uno.

  8. MartinZikmund commented on May 4, 2020

    @MartinZikmund
    MemberAuthor

    It is realistic (and required) to fix this without breaking changes, but it would most likely be fixed properly by porting the original Frame code. I would suggest that, for now, modifying the MvvmCross code to adjust for the behavior is the safest course of action.

    Note that FrameworkElement.IsLoaded is available in WinUI and can be used to compensate for this behavior in a backward compatible way, once this is fixed in Uno.

    I think the problem is not only in Frame, but in other controls too - the Loaded event is in general called immediately after the control is attached to tree, whereas in UWP it is executed after the UI thread is free - see the CustomControl example code in the issue description.

    We were looking into it with @nickrandolph , the problem for MvvmCross can be worked around for now by using INotifyPropertyChanged on the page and notifying after the VM was set to refresh all x:Binds (but it also means all x:Bind must be at least OneWay), or use classic Binding. This workaround could be mentioned as a temporary limitation for the first release.

  9. 19 remaining items

  10. changed the title [-]Loading, Loaded & Unloaded events are called too early[/-] [+]`Loading`, `Loaded` & `Unloaded` events are called too early[/+] on Jun 12, 2023
  11. self-assigned this
    on Nov 28, 2023
  12. Youssef1313 commented on Dec 2, 2023

    @Youssef1313
    Member

    We'll need to revisit this workaround:

    // Uno workaround: Since Loaded is called before measure, the increase/decrease button visibility is not initially set
    // properly unless we subscribe to ScrollableWidth changing.

  13. Youssef1313 commented on Dec 4, 2023

    @Youssef1313
    Member

    ContentControl specifically seems to be a bit special.

    image

    It looks like when Content is set, it's not immediately added as a child (the comment in WinUI code says this is done "later".

    Then, for some reason, fIsLive is set to false which may have an effect on Loaded event.

    In the case of Uno, setting ContentControl.Content will immediately add the child.

    image

  14. Youssef1313 commented on Dec 4, 2023

    @Youssef1313
    Member

    Also note that UserControl in WinUI is NOT ContentControl, while in Uno it's a ContentControl.

    If we aligned ContentControl behavior with WinUI, we will have to verify UserControls behavior as well on WinUI and exclude them from that special behavior until the inheritance is fixed.

  15. Youssef1313 commented on Dec 4, 2023

    @Youssef1313
    Member
        public partial class MyStackPanel : StackPanel
        {
            public MyStackPanel()
            {
                var btn = new Button();
                btn.Loading += Btn_Loading;
                btn.Loaded += Btn_Loaded;
                this.Children.Add(btn);
            }
    
            private void Btn_Loaded(object sender, RoutedEventArgs e)
            {
                Debug.WriteLine("Loaded");
            }
    
            private void Btn_Loading(FrameworkElement sender, object args)
            {
                Debug.WriteLine("Loading");
            }
    
            protected override Size MeasureOverride(Size availableSize)
            {
                return new Size(500, 500);
            }
    
            protected override Size ArrangeOverride(Size finalSize)
            {
                return new Size(500, 500);
            }
        }

    Using the above code, the button will get "Loaded" but not "Loading".

    Note that if ArrangeOverride is removed, it will get both. This is because Arrange will attempt to measure if it detects the element wasn't measured.

  16. Youssef1313 commented on Dec 4, 2023

    @Youssef1313
    Member

    Another interesting case:

    // sp is an already loaded stack panel.
    var btn = new Button();
    btn.Loading += Btn_Loading;
    btn.Loaded += Btn_Loaded;
    btn.Unloaded += Btn_Unloaded;
    sp.Children.Add(btn);
    sp.Children.Clear();
    sp.Children.Add(btn);

    The order of events is:

    1. Loaded
    2. Loading
    3. Unloaded

    But the end state of btn is IsLoaded = true.

  17. KWodarczyk commented on Sep 16, 2024

    @KWodarczyk

    I have also observed that all If I have multiple pages in my application their "Loaded" event if fired without event going to the page. Not sure if this is related but I need to figure out how to make "Loaded" event fire only when page is accessed any ideas ?

  18. MartinZikmund commented on Sep 17, 2024

    @MartinZikmund
    MemberAuthor

    I have also observed that all If I have multiple pages in my application their "Loaded" event if fired without event going to the page. Not sure if this is related but I need to figure out how to make "Loaded" event fire only when page is accessed any ideas ?

    That is certainly odd, which platform are you observing this on? Can you create a repro and post as a separate issue please?

  19. MartinZikmund commented on Jul 17, 2025

    @MartinZikmund
    MemberAuthor

    Works correctly in Skia targets, closing

  20. jeanplevesque commented on Jul 17, 2025

    @jeanplevesque
    Member

    Works correctly in Skia targets, closing

    Does that mean native renderers are out of support? @MartinZikmund

  21. github-actions commented on May 4, 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.

  22. locked as resolved and limited conversation to collaborators on May 4, 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 🤯epickind/bugplatform/android 🤖platform/ios 🍎platform/macos 🍏platform/wasm 🌐project/navigation-lifecycle 🧬

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions