LibVlcSharp.WPF VideoView content (ForegroundWindow) hook not working and hidden when TabControl selection changed
A VideoView object is located inside a tab control. The tab selection is changed as follows: TabVideoView->SecondTab->TabVideoView. Now the VideoView content (ForegroundWindow) is no longer visible and also the window hook to the WinHost does not work anymore.
Minimal project and steps to reproduce
- Switch from "FirstTab" to "SecondTab" and back. Now the VideoView Content should be disappeared.
What is the current bug behavior?
The VideoView Content disappeares after switching the Tab Control item
What is the expected correct behavior?
The VideoView Content should be visible after switching the Tab Control item
Does it work on other plaforms? Does it work with the official VLC apps?
- OS: Windows
- Version 20H2
- Device: PC
- LibVLC version and architecture 3.0.16 x64
- LibVLCSharp version 3.5.1 x64
The problem is that the ForegroundWindow is not prepared for an Unload -> Load triggered by the Tab control.
In the Wndhost_LayoutUpdated event an error is thrown, triggered by this line:
var locationFromScreen = _bckgnd.PointToScreen(_zeroPoint);
The problem here is that the Wndhost_LayoutUpdated is thrown just before the Background_Unloaded event, even though our anchor "_bckgnd" has already been unloaded at this point (i.e. is no longer connected to a presentation source). So we can avoid this error by simply asking for this in advance. Like this:
if (PresentationSource.FromVisual(_bckgnd) != null)
Additionally, the ForegroundWindow is now unattached from our anchor and hidden due to the Background_Unloaded event.
So we extend the first condition in the Background_Loaded event to ask if the ForegroundWindow is visible https://code.videolan.org/videolan/LibVLCSharp/-/blob/3.x/src/LibVLCSharp.WPF/ForegroundWindow.cs#L72
With this we make sure that the ForegroundWindow is attached to our anchor again.