microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.3k stars 676 forks source link

WebView2 control in the pages of tabview won't stop after tab was closed #8193

Closed IShiraiKurokoI closed 7 months ago

IShiraiKurokoI commented 1 year ago

Describe the bug

The microsoft doc says use code below to close tab

private void Tabs_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
{
    sender.TabItems.Remove(args.Tab);
}

But if I create a webview tab use the code below(CustomWebPage is a page contains a webview2 control,which will change tabitem header and create new tab dynamically),when I closed the tab, I could still hear the audio of the website playing, and the webview thread can still be seen in the task manager. I know there is a method called "CoreWebView2Controller.Close", but how can I call it? Or is there a simple way to close the tab and dispose(release) all the controls and resources used in it?

            TabViewItem newItem = new TabViewItem();
            newItem.Header = Title;
            Frame frame = new Frame();
            frame.Navigate(typeof(CustomWebPage), new CustomParameter(this, newItem,url));
            newItem.Content = frame;
            TabView.TabItems.Add(newItem);
            TabView.SelectedIndex= TabView.TabItems.Count-1;

By the way, can I obtain the icon of website in webview2 without some complex and unreliable javascript(like the method in wpf: corewebview2.faviconuri)

Steps to reproduce the bug

  1. create a window with a tabview
  2. use method like above to create a tab with a webview2
  3. open some website and play video or audio
  4. close the tab
  5. media goes on

Expected behavior

No response

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.2.3: 1.2.230118.102

Windows version

Windows 11 (22H2): Build 22621

Additional context

No response

DmitriyKomin commented 7 months ago

Hey @IShiraiKurokoI, you can call WebView2.Close in your _TabsTabCloseRequested (that will, among other things, call CoreWebView2Controller.Close, and should synchronously stop web audio playback).