Add Loaded/Unloaded Events from WinUI/UWP
See original GitHub issueIs your feature request related to a problem? Please describe.
UWP has the Loaded/Unloaded events which are useful for control authors to connect/disconnect things.
- Loaded : “Occurs when a FrameworkElement has been constructed and added to the object tree, and is ready for interaction.”
- Unloaded : “Occurs when this object is no longer connected to the main object tree.”
OnAttachedToVisualTree and OnDetachedFromVisualTree are similar but the sequence these methods/events are called is not the same. OnAttachedToVisualTree is called BEFORE OnApplyTemplate which is a problem if you are connecting events and the like. Therefore, Loaded would be invoked just before (or during) the control is first shown to the user – after both OnApplyTemplate and OnAttachedToVisualTree.
Describe the solution you’d like
Add closely equivalent Loaded/Unloaded events and new OnLoaded/OnUnloaded methods to Control. This will help control authors and apps coming over from WinUI and UWP. It might also simplify some Avalonia controls.
Describe alternatives you’ve considered
Additional context Add any other context or screenshots about the feature request here.
Issue Analytics
- State:
- Created a year ago
- Reactions:6
- Comments:6 (5 by maintainers)

 Top Related Medium Post
Top Related Medium Post Top Related StackOverflow Question
Top Related StackOverflow Question
Yes, forgot to add it, but that’s what the _IsLoaded field is for. May be a DirectProperty or a CLR property, haven’t decided yet and will check WPF.
I had a peek at the WPF source and checked a call stack for how this is implemented. Doesn’t make a whole lot of sense as its scattered in 10 different places with weirdly named helper classes in typical MS fashion, but it looks like they fire it after the entire layout pass is completed and just before the render pass. The exact details of this are a bit more obscure. In Avalonia, it probably could be done in
LayoutManager.ExecuteLayoutPassas the last step, by keeping track of which elements haven’t loaded yet. But, I’m not that familiar with the layout => rendering pipeline other than just generally how it works so one of the core team will probably have better advice.Unloadedis much less of an issue, and I thinkDetachedFromVisualTreedoes the same thing, andUnloadedmay not even be needed. Though for parity, it makes sense to include it and can probably be raised at the same time.