none
Why are there more Unloaded than Loaded events? RRS feed

  • Question

  • I have made a sample app is on github:

    https://github.com/victorpadureanu/LoadedUnloadedIssue

    Here you can easily see that if you change the parent of an UIElement ( in my situation a simple button ) multiple times very fast, you will get considerably more Unloaded events that Loaded events.

    Is this the intended behavior?

    To use the sample:

    Open App2.sln, build on the x86/x64 platform, read the on-screen instructions and monitor the details.

    Tuesday, May 14, 2019 3:43 PM

All replies

  • Hi,

    Yes, the loaded event only fires when the button is loaded completed. But sometimes you are changing too fast, the button will be removed even when it is not loaded completely. that's why it won't trigger the loaded event. To see how much times the button is loading, you could subscribe the FrameworkElement.Loading Event instead of the loaded event. This time, it shows the behavior that you want. 

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 15, 2019 3:29 AM
    Moderator
  • Hi,

    Based on my experiments and reading some forum questions on this topic, looks like Loaded, Loading and Unloaded events are async events and the order is not guaranteed. For example, if I add the view to the tree (op 1), remove the view (op 2), and add it back (op 3), I receive the events in this order:

    Loading (from op 1), Loaded (from op 3), Loading(from op 3), Unloaded(from op 2) - how do I know if `Loaded` corresponds to "op 1" (the view was fully loaded the first time it was attached to the tree), or it corresponds to "op 3" (the view was not fully loaded on "op 1", but it was fully loaded after being re-attached)? If "Loaded" corresponds to "op 1", I should wait for one more Loaded event before starting working with the control (e.g. drawing). What is the recommended way to determine if a view is fully loaded given this complexity of async & out of order events? Is there a way to correlate Loading/Loaded/Unloaded events to each other?

    For example, Win2D is a library relying on Loaded/Unloaded events to stop/resume drawing: https://github.com/microsoft/Win2D/blob/master/winrt/lib/xaml/BaseControl.h#L828 . Is there a way to ensure drawing is resumed properly?

    Wednesday, May 15, 2019 8:41 AM
  • Hello,

    I have tested with the Loading event and it seems that the issue happens less often, but it sometimes appears when the application is more complex.

    That means using Loading event is still not a reliable option.

    Best regards,

    Victor P.

    Monday, May 20, 2019 9:22 AM
  • Hi,

    Using loading event is to answer your previous question about why the count of loaded event is less than the unloaded event. As I said in my last reply, the loaded event shows when the control is loaded completely. If the control is removed before it is loaded, it won't trigger the loaded event but trigger the unloaded event. Generally, we run code in loaded event because when the event happens, it means the control is ready to interaction.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 21, 2019 6:02 AM
    Moderator
  • Hello,

    I have made a simple sample:
    https://github.com/victorpadureanu/LoadedUnloadedIssue/tree/LoadingLoadedUnloaded

    Here you can see that if you press the button fast enough you will miss both Loading and Loaded.

    Is this the normal behavior to miss the Loading event?

    Best regards,
    Victor P.


    Tuesday, May 28, 2019 4:42 PM
  • Hi,

    Well, in your sample I did noticed the behavior your said. But when I did change the sleep time from 10 to 1000. All the events works well. So it seems like that when you trigger the event 10 times at one time, the button is prepared in the background but it is removed before it starts to be rendered.

    I'm a little confused about why you are doing this. Because the sample you give describes a very special scenario which means generally people won't add or remove the control so quickly. Why you want to add and remove the control for so many times so quickly? What's the real scenario of your project?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 29, 2019 4:43 AM
    Moderator
  • Hello,

    We are not doing this exact behavior. We are doing something like a browser that converts html to xaml. On click events ( can be any events ) we might restructure the dom ( add nodes, move nodes ... ). One of the node is the win2d CanvasControl that is relying on Loaded and Unloaded for the draw callback. The scenario appears when expanding and collapsing some ui elements based on a click ( other scenarios are possible as well ). And if the user presses the button very fast that triggers the collapse and expand it will do some fast reparenting on the same frame. This is the actual scenario.

    My question right now is:

    Can xaml be affected, or can there be side effects besides missing events?
    I am only referring to internal xaml workings, not 3rd party libraries at this point.

    We also need to target RS2 ( 15063 ). That means we are not able to use IsLoaded which is present in 17763.

    Best regards,
    Victor P.


    Wednesday, May 29, 2019 9:17 AM
  • Hi,

    Well, it depends on what you want to do in your code. If you are drawing the nodes in both loaded and unloaded events, it might has effects. But if you are just removing nodes in unloaded event, based on the results we observed, unloaded event fires more than loaded event, so you just need to check if the node exist before you remove it. This won't have side effects. 

    My suggestion is using a different way, for example,after a button click, the node will be adding and the loading event will be triggered, before the loaded event is finished, disabled the button, make it unable to click. The control requires some time to render completely and it doesn't cost many time. When the node is fully loaded, then make the button enabled again.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 30, 2019 8:54 AM
    Moderator