Control Lifecycle - Event Sequence
A structured view of initialization, updates, and teardown events
Developers are curious creatures. Sometimes we look at a control in WinUI 3 and wonder: “What secrets are you hiding? When do you fire your events? And why do you keep lagging my app when I touch layout stuff?”
So, naturally, I did what any sane person would do:
I subscribed to every single event I
could find on a control.
Yes, every. Even the ones nobody talks about at conferences. Even the ones that sound like they
belong in a Marvel movie
(ManipulationInertiaStarting, anyone?).
The Experiment
I hooked up every event handler to a simple:
Debug.WriteLine("EventName");
And then… I launched the app, touched nothing, and just closed the window. What did I see?
The Output
DataContextChanged //Occurs when the value of the FrameworkElement.DataContext property changes.
Loading //Occurs when a FrameworkElement begins to load.
EffectiveViewportChanged //Occurs when the FrameworkElement's effective viewport changes.
SizeChanged //Occurs when either the ActualHeight or the ActualWidth property changes value on a FrameworkElement.
LayoutUpdated //Occurs when the layout of the visual tree changes, due to layout-relevant properties changing value or some other action that refreshes the layout.
Loaded //Occurs when a FrameworkElement has been constructed and added to the object tree, and is ready for interaction.
LayoutUpdated
Window Closing
LayoutUpdated
Unloaded //Occurs when this object is no longer connected to the main object tree.
LayoutUpdated
The Story These Events Tell
A Word of Caution About LayoutUpdated
This event is like that one friend who never stops texting (kind of like me with my crush who never texted back). It fires a lot.
You look at it the wrong way? Boom, LayoutUpdated. Resize the window by 1px?
LayoutUpdated party.
Do not put expensive logic
here. Unless you enjoy frame drops, stutters, and fans
spinning like a jet engine.
If you must react to layout changes, use lighter events like SizeChanged or
EffectiveViewportChanged.
Reserve LayoutUpdated for small, quick reactions only.
Final Thoughts
Subscribing to everything showed me one thing: most events won’t fire unless you poke the control, but the lifecycle events tell a neat story from birth to death.
- Use
Loadedfor setup. - Use
Unloadedfor cleanup. - Avoid
LayoutUpdatedfor expensive logic — or, if you have no other option, handle it carefully as in this PR.