Loading and Loaded in UWP

All controls in UWP has the Loading  and Loaded  event. They are called when the view is about to show on screen. One would believe that Loading  always comes before Loaded  but this is not always the case.

During my last project we had a bug which was strange and when debugging I found that the Loading  event was actually not called at all. Loaded  is called as expected.

Visibility makes a difference

I was using UserControls  which was visible or not depending on certain criteria. And after doing some more debugging I found out that if the control has Visibility Hidden  during startup the Loading  event will not be called. And even more strange is that if the control later becomes visible the Loading  event gets called, by then Loaded  already has been called making the code which was depending on Loading  should be called before Loaded  not to work.

Conclusion

Needless to say that I changed how the code ran during startup and all Loading  events was removed. But if you must/want to have code in Loading  beware that it will not be called if the control is hidden, but called later when it becomes visible.

I tried to find some info on this in the help but nothing is mention about this there so maybe this blogpost will help someone. If anyone has any info or comment on this please let my know in the comments below.

Leave a Reply