microsoft / microsoft/microsoft-ui-xaml
DesktopChildSiteBridge closes unexpectedly, throwing WindowParentChain invalid state
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 942
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 105
Description
### 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.

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_
Contributor guide
Research direction
Start by locating DesktopChildSiteBridge, DesktopWindowXamlSource, and the HwndHost entry point BuildWindowCore, then reproduce the high-contrast or RDP transition described in the issue. Done means the hosted window remains open, BuildWindowCore does not cause a crash when called again, and the WinUI 3 content and input continue to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100