question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Loaded/Unloaded Events from WinUI/UWP

See original GitHub issue

Is 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:closed
  • Created a year ago
  • Reactions:6
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
robloocommented, May 2, 2022

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.

1reaction
amwxcommented, Apr 11, 2022

@amwx This is near the top of my list for next issues to close. I imagine I’ll get to it next weekend. Would appreciate a review at that time or even some ideas on good places to raise these events. I’m not very familiar with the control lifecycle in Avalonia at the moment.

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.ExecuteLayoutPass as 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.

Unloaded is much less of an issue, and I think DetachedFromVisualTree does the same thing, and Unloaded may not even be needed. Though for parity, it makes sense to include it and can probably be raised at the same time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FrameworkElement.Loaded Event (Windows.UI.Xaml)
The Loaded event can be used as a point to hook up event handlers on elements that come from a template, or to...
Read more >
OnLoaded event calling again in UWP
The Loaded event gets raised every time the element gets added to the VisualTree, has rendered and is ready for interaction.
Read more >
ToolWindowUnloaded Event - Infragistics WPF™ Help
This sample demonstrates handling the ToolWindowLoaded and Unloaded events to add handlers to the ToolWindow created for floating panes within the ...
Read more >
Windows Community Toolkit | XAML Brewer, by Diederik Krols
In this article we showcase the latest and greatest version of the Radial Gauge control in a WinUI 3 Desktop Application.
Read more >
Events Overview - Telerik UI for WPF - Documentation
The DataLoading and DataLoaded events are triggered when the ItemsSource is changed ... AddingNewDataItem: Occurs when a new data item is about to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found