MicroSugarDeveloperOrg / Avalonia.WebView

MIT License
202 stars 32 forks source link

WebView causes application to crash when navigating to a different tab (InvalidCastException) #20

Closed derekantrican closed 6 months ago

derekantrican commented 1 year ago

Screen recording (the "Pip Details for ...." is the WebView control):

OfficeDominoAnalyzer2_lNWopb8dI2

Here's the exception details:

System.InvalidCastException
  HResult=0x80004002
  Message=Unable to cast object of type 'Avalonia.WebView.Windows.WebViewHandler' to type 'Avalonia.Controls.Platform.INativeControlHostDestroyableControlHandle'.
  Source=Avalonia.Controls
  StackTrace:
   at Avalonia.Controls.NativeControlHost.DestroyNativeControlCore(IPlatformHandle control)
   at AvaloniaWebView.Shared.Handlers.ViewHandler.DestroyNativeControlCore(IPlatformHandle control)
   at Avalonia.Controls.NativeControlHost.DestroyNativeControl()
   at Avalonia.Controls.NativeControlHost.CheckDestruction()
   at Avalonia.Threading.DispatcherOperation.InvokeCore()
   at Avalonia.Threading.DispatcherOperation.Execute()
   at Avalonia.Threading.Dispatcher.ExecuteJob(DispatcherOperation job)
   at Avalonia.Threading.Dispatcher.ExecuteJobsCore()
   at Avalonia.Threading.Dispatcher.OnOSTimer()
   at Avalonia.Win32.Win32Platform.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
   at Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg)
   at Avalonia.Win32.Win32DispatcherImpl.RunLoop(CancellationToken cancellationToken)
   at Avalonia.Threading.DispatcherFrame.Run(IControlledDispatcherImpl impl)
   at Avalonia.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken)
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args)
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, String[] args, ShutdownMode shutdownMode)
   at OfficeDominoAnalyzer2.Program.Main(String[] args) in OfficeDominoAnalyzer2\Program.cs:line 17

I'm on

At the moment I don't have a min repro example but if you really need one I can work on making one.

ChisterWu commented 1 year ago

I will try it.

derekantrican commented 10 months ago

@ChisterWu Any luck reproducing this?

derekantrican commented 9 months ago

@ChisterWu I can reproduce with simply this:

    <TabControl>
        <TabItem Header="Main">

        </TabItem>
        <TabItem Header="Player">
            <WebView/>
        </TabItem>
    </TabControl>

Put that into a Window, start the application, click "Player" tab, then click "Main" tab.

derekantrican commented 8 months ago

I found a (horrible, but working) workaround for the above example (while referencing this post):

XAML:

        <Grid RowDefinitions="Auto,*">
            <TabStrip x:Name="tabStrip" SelectionChanged="TabStrip_SelectionChanged">
                <TabItem Header="Main"/>
                <TabItem Header="Player"/>
            </TabStrip>
            <WebView x:Name="webView" Grid.Row="1" IsVisible="False"/>
        </Grid>

XAML.CS:

    private void TabStrip_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        TabStrip tabStrip = sender as TabStrip;
        if (tabStrip.SelectedItem != null)
        {
            this.FindControl<WebView>("webView").IsVisible = (tabStrip.SelectedItem as TabItem).Header as string == "Player";
        }
    }
derekantrican commented 6 months ago

For some reason I'm having trouble reproducing this with the sample in this repo. I changed SampleWebView\MainView.axaml to this:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:SampleWebView.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:views="clr-namespace:SampleWebView.Views"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="SampleWebView.Views.MainWindow"
        Icon="/Assets/avalonia-logo.ico"
        Title="SampleWebView">
    <TabControl>
        <TabItem Header="Tab1"/>
        <TabItem Header="Tab2">
            <WebView x:Name="PART_WebView" Url="http://www.bilibili.com"/>
        </TabItem>
    </TabControl>
</Window>

And this is the result:

SampleWebView Desktop_4PS7D8WtSm

So while it doesn't crash the application, the browser doesn't show back up when the tab is navigated back to. (And I'm not sure why it doesn't crash in this test - perhaps some differences in the version of Avalonia or something)

rabbitism commented 6 months ago

thank for the fix

derekantrican commented 6 months ago

Going to assume this is closed & fixed via https://github.com/AvaloniaUI/Avalonia/pull/15029 (once this goes out with a release of Avalonia)