dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

Switching to an existing page with SetTitleView used in Flyout menu causes "Element is already the child of another element" crash on Windows in MAUI 8.0.6 #21037

Open SmartmanApps opened 8 months ago

SmartmanApps commented 8 months ago

Description

In MAUI 8.0.3, the TitleBar was broken for NavigationPages, but otherwise switching between existing (i.e. not "new" each time) was working. In 8.0.6 the former was fixed but the latter is now broken on Windows. Bug also present in 8.0.7. It still works if...

Note: the repro repo is the same one as I used for 15767, which is still not fixed (I added the new code in a new branch).

Steps to Reproduce

  1. create a new MAUI app with a Flyout menu, which has at least 1 page in it which is only created once.
  2. use SetTitleView on at least one of those such pages.
  3. that page(s) will work the first time you switch to it, but when you switch to another page and then switch back to it the crash occurs on Windows.
  4. the code for all this is in the 806issue branch of the FlyoutRepro repo. You can change the commnted-out lines on both TemplateFlyoutPage.cs and SecondPage.cs to change the various configs (currently set to it's crashing state of using SetTitleView and only creating the page once). I have some //// comments just before those lines you can change (so just search for ////).

Link to public reproduction project repository

https://github.com/SmartmanApps/FlyoutRepro

Version with bug

8.0.6 SR1

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.3 GA

Affected platforms

Windows

Affected platform versions

1000.19041.1000.0

Did you find any workaround?

See description - changing any one of the 4 factors (existing page, Windows, SetTitleView, 8.0.6) works, just not those 4 things together.

Relevant log output

Exception thrown: 'System.Runtime.InteropServices.COMException' in WinRT.Runtime.dll
WinRT information: Element is already the child of another element.
'FlyoutRepro.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.2\System.Diagnostics.StackTrace.dll'. 
'FlyoutRepro.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.2\System.Reflection.Metadata.dll'. 
'FlyoutRepro.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.2\System.Collections.Immutable.dll'. 
********************************** UNHANDLED EXCEPTION! Details: System.Runtime.InteropServices.COMException (0x800F1000): No installed components were detected.

Element is already the child of another element.
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|39_0(Int32 hr)
The program '[23644] FlyoutRepro.exe' has exited with code 4294967295 (0xffffffff).
SmartmanApps commented 8 months ago

P.S. this happens regardless of what type of view is used in the TitleView. e.g. I was initially using a HorizontalStackLayout, but I tried it also with a Label and the same thing happened.

RoiChen001 commented 2 months ago

This issue can be reproduced on Windows platform on the latest 17.12.0 Preview 1.0(8.0.80 & 8.0.6), but this used to work in 8.0.3.