Skip to content

UI crashes with exception: "Unable to create WPF Window in VideoView."

Description:

When using a LibVLCSharp WPF VideoView and switching between tabs, the UI crashes with an exception. switching_tabs_crash

Expected behavior:

Expected no crash

Exception:

LibVLCSharp.Shared.VLCException: Unable to create WPF Window in VideoView. ---> System.InvalidOperationException: This Visual is not connected to a PresentationSource.
   at System.Windows.Media.Visual.PointToScreen(Point point)
   at LibVLCSharp.WPF.ForegroundWindow.Background_Loaded(Object sender, RoutedEventArgs e)

Stacktrace:

   at LibVLCSharp.WPF.ForegroundWindow.Background_Loaded(Object sender, RoutedEventArgs e)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
   at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
   at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
   at MS.Internal.LoadedOrUnloadedOperation.DoWork()
   at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

LibVLCSharp/LibVLC Versions:

  • LibVLCSharp 3.7.0
  • LibVLCSharp.WPF 3.7.0
  • VideoLan.LibVLC.Windows 3.0.18

Operating system:

Windows 10 Professional

How to reproduce:

Create a WPF application and use the following MainWindow.xaml (change the NameSpace to your own):

<Window x:Class="NameSpace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MSStreaming_VLC_WPF"
        xmlns:wpf="clr-namespace:LibVLCSharp.WPF;assembly=LibVLCSharp.WPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TabControl>
            <TabItem Header="VLC 1">
                <TabControl>
                    <TabItem Header="VLC 2">
                        <wpf:VideoView x:Name="VideoView" />
                    </TabItem>
                    <TabItem Header="Other 2">
                        <TextBlock Text="Other content" />
                    </TabItem>
                </TabControl>
            </TabItem>
            <TabItem Header="Other 1">
                <TextBlock Text="Other content" />
            </TabItem>
        </TabControl>
    </Grid>
</Window>

Add some exception handling to the App.xaml.cs:

    public partial class App
    {
        // Prevent an exception to crash the application
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            DispatcherUnhandledException += (sender, args) =>
            {
                // Prevent the application from crashing
                args.Handled = true;

                // Display a message box with the exception details
                MessageBox.Show("An unhandled exception occurred: " + args.Exception.Message, "Unhandled Exception", MessageBoxButton.OK, MessageBoxImage.Error);
                // Log the exception including inner exceptions
                Debug.WriteLine(args.Exception.ToString());
                Debug.WriteLine(args.Exception.StackTrace);
            };
        }
    }

Reproduction steps (also see gif):

  • Click on Other 2
  • Click on Other 1
  • Click on VLC 1

Observe the application popups with an exception

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information