maui icon indicating copy to clipboard operation
maui copied to clipboard

[Custom Control] HotReload doesn't work for newly added nested items

Open the8thC opened this issue 3 years ago • 7 comments

Description

Continuation of https://github.com/dotnet/maui/issues/10390, but I've implemented IVisualTreeElement. Only now the HotReload works for items defined in XAML at launch, but doesn't work for items added via HotReload. I've noticed the IReadOnlyList<IVisualTreeElement> IVisualTreeElement.GetVisualChildren() method is not called when adding new nested items, maybe I should handle collection changes somehow so it's called, @PureWeen?

https://user-images.githubusercontent.com/21204523/199957266-18de9468-d27d-4c95-beed-68c0b97377a5.mov

Steps to Reproduce

Watch the video

Link to public reproduction project repository

https://github.com/the8thC/ElementVisuaElementHotReload

Version with bug

7.0 Release Candidate 2

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, I was not able test on other platforms

Affected platform versions

iOS 16, Android API 32

Did you find any workaround?

Remove and add the whole parent control and HotReload works for all items

Relevant log output

No response

the8thC avatar Nov 04 '22 11:11 the8thC

@drasticactions any ideas here maybe?

jfversluis avatar Nov 04 '22 15:11 jfversluis

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Nov 04 '22 15:11 ghost

@the8thC If you try adding new child elements into the parent control via C# Code Behind (Ex. Create a button that will add a new element when clicked), does it do the same thing?

CC @etvorun, do you see anything in their underlying controls in their repro that would prohibit adding nested elements from working?

drasticactions avatar Nov 10 '22 00:11 drasticactions

@drasticactions If I add the item in code-behind I can't edit it's XAML to check for HotReload, so I can't really test that. But the IVisualTreeElement.GetVisualChildren() isn't called when I add items via button clicked event

https://user-images.githubusercontent.com/21204523/201051556-2766fda6-995b-4b2b-ba8b-be9079f0e7ce.mov

the8thC avatar Nov 10 '22 09:11 the8thC

That's fine, it was to see if GetVisualChildren would be called.

If that's not being called, then, most likely, VisualDiagnostics isn't seeing that new item being added to the children, so it's not firing the events internally to show that the children were added at all. That means Hot Reload can't work, since either the control or the base MAUI controls the control is based on (if it is) is not tracking the child items correctly.

For your control, try adding VisualDiagnostics.OnChildAdded(this, element); (as seen here)

This is normally done by the base controls within MAUI. If you make your own and VisualDiagnostics.OnChildAdded and OnChildRemoved are not being called, you need to call it for Hot Reload to work.

drasticactions avatar Nov 10 '22 13:11 drasticactions

@drasticactions, it works this way, thanks! The issue can be closed

the8thC avatar Nov 14 '22 11:11 the8thC

Before we close it, we should probably consider documenting this somewhere (Creating custom controls and writing IVisualTreeElement overrides), so others don't get bit by the same thing. @Redth Is this possible to have this done? Maybe for the backlog?

drasticactions avatar Nov 14 '22 12:11 drasticactions

Ideally users creating custom controls wouldn't always have to know about IVisualTreeElement and we'd have a way to wire into LogicalChildren https://github.com/dotnet/maui/issues/11528

PureWeen avatar Nov 21 '22 16:11 PureWeen

@davidbritch are you able to assist in adding some conceptual docs around LogicalChildren API's?

Redth avatar Jan 26 '23 14:01 Redth

@Redth https://github.com/dotnet/docs-maui/issues/1216

davidbritch avatar Jan 27 '23 10:01 davidbritch

Thanks!

Redth avatar Jan 27 '23 11:01 Redth