Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DesktopChildSiteBridge closes unexpectedly, throwing WindowParentChain invalid state #10231

Open
aquinn39 opened this issue Dec 7, 2024 · 11 comments
Labels
bug Something isn't working needs-triage Issue needs to be triaged by the area owners

Comments

@aquinn39
Copy link

aquinn39 commented Dec 7, 2024

Describe the bug

With WinUI 3, we can now use XamlIsland using the DesktopWindowXamlSource class. By using this class, we can create a custom HwndHost in WPF to host WinUI3 content. When doing this, however, the XamlHost's window will be destroyed in response to various unrelated actions such as toggling between high contrast mode and remoting onto a machine via RDP. If you enable all exceptions in Visual Studio, you can see that it throws an exception with the message 'WindowParentChain invalid state' when it does this.
Image

This is a problem for various reasons. First being that the content disappears, second if we try to reinitialise the DesktopWindowXamlSource, it will throw a System.Runtime.InteropServices.COMException exception. If we try to create a new DesktopWindowXamlSource and put the content from the previous one into the new one, this causes some strange side effects, such as if a ContentDialog was open previously it will now be closed, pressing tab will do nothing and various keyboard events won't be fired. Another side effect is a bunch of exceptions will be thrown when a flyout is opened. It also results in the content disappearing for a bit before it comes back.

I checked WindowsFormsHost and the old UWP/WinUI 2 XamlHost and they don't have this issue - I checked with Spy++ and the child window handle stays the same after doing something that would close the XamlHost whereas with WinUI 3, it is destroyed.

What I think is happening is that the HwndHost is doing something WinUI 3's XamlHost doesn't like which is causing it to throw that exception and close.

Steps to reproduce the bug

  1. Run the attached project in Visual Studio
  2. Toggle high contrast mode on or off in Windows Settings for RDP into the box
  3. Note that the app will crash due to BuildWindowCore running again since the window was destroyed

Expected behavior

The window should stay open. In fact, looking at the source code for the WindowsFormsHost and UWP/WinUI 2 XamlHost, they don't even seem to account for the fact that BuildWindowCore could be called again.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.6.3: 1.6.241114003

Windows version

Windows 11 (23H2): Build 22631

Additional context

No response

@aquinn39 aquinn39 added the bug Something isn't working label Dec 7, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Dec 7, 2024
@aquinn39
Copy link
Author

aquinn39 commented Dec 7, 2024

No description provided.

@aquinn39
Copy link
Author

aquinn39 commented Dec 7, 2024

Minimal reproduction project (GitHub keeps failing to upload the file) - https://1drv.ms/u/s!Apx1hDSN2e_8vPZPGi7U0g5OGuDTBQ?e=syDRx3

@aquinn39
Copy link
Author

aquinn39 commented Dec 7, 2024

It seems to log the following message to the log:
onecoreuap\windows\moderncore\inputv2\inputhost\outside\lifted\windowparentchain.cpp(326)\Microsoft.UI.Input.dll!00007FF8A4985FCC: (caller: 00007FF8A4986AB1) ReturnHr(2) tid(15c88) 8007139F The group or resource is not in the correct state to perform the requested operation. Msg:[HWND 000000000088348A changed parent from 00000000017F11E4 to 000000000042392C. Unsupported scenario.] Exception thrown at 0x00007FFA13D5FA4C (KernelBase.dll) in QPad.exe: WinRT originate error - 0x8007139F : 'WindowParentChain invalid state.'.

But the question is why is the parent changing and who is changing it?

00000000017F11E4 appears to be the handle of the WPF window, 000000000088348A is the XamlHost and 000000000042392C is some sort of "SystemResourceNotifyWindow" owned by the same process as the app.
Image

@aquinn39
Copy link
Author

aquinn39 commented Dec 7, 2024

Might be this code being mistakenly called, in which case it might be an issue with WPF itself: https://github.com/dotnet/wpf/blob/c6bff274b42ea186c4a53aab6e6447cf92aeb1e1/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Interop/HwndHost.cs#L984. Still, perhaps the XamlHost shouldn't be being so strict and destroying its window when the parent is changed (or at least have a way to restore it).

@castorix
Copy link

castorix commented Dec 7, 2024

I just tested by toggling high contrast mode on my PC (Windows 10 22H2) with the sample I posted last year (WPF_XAML_Islands_WinUI3) by recompiling it with Windows App SDK 1.6.241114003 and I don't have this problem...

@aquinn39
Copy link
Author

aquinn39 commented Dec 8, 2024

Interesting, thanks, I'll try with your sample and see if it happens on my PC.

@aquinn39
Copy link
Author

aquinn39 commented Dec 8, 2024

@castorix I tried your sample and it doesn't have the issue, but it doesn't seem to be using HwndHost, so I guess that confirms this only happens with HwndHost.

@aquinn39
Copy link
Author

aquinn39 commented Dec 8, 2024

Also submitted to WPF repo - dotnet/wpf#10153. I think this is an issue with both WinUI 3 (closing the child window when it is reparented) and WPF (HwndHost reparenting the window for no reason)

@karkarl karkarl transferred this issue from microsoft/microsoft-ui-xaml Dec 12, 2024
@karkarl
Copy link
Contributor

karkarl commented Dec 12, 2024

DesktopWindowXamlSource sits under Windows.UI.Xaml.Hosting. Transferring to WindowsAppSDK repo.

@RDMacLachlan RDMacLachlan transferred this issue from microsoft/WindowsAppSDK Dec 12, 2024
@RDMacLachlan
Copy link
Member

This appears to be lifted XAML Islands in WinUI3, Microsoft.UI.XAML.hosting.DesktopWindowXAMLSource.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-triage Issue needs to be triaged by the area owners
Projects
None yet
Development

No branches or pull requests

4 participants