diff --git a/src/Controls/samples/Controls.Sample/Pages/Core/MultiWindowPage.xaml b/src/Controls/samples/Controls.Sample/Pages/Core/MultiWindowPage.xaml
index 26a69ac68560..4ba2119b2fcd 100644
--- a/src/Controls/samples/Controls.Sample/Pages/Core/MultiWindowPage.xaml
+++ b/src/Controls/samples/Controls.Sample/Pages/Core/MultiWindowPage.xaml
@@ -10,6 +10,11 @@
Padding="12"
Spacing="6">
+
+
diff --git a/src/Controls/src/Core/HandlerImpl/WebView/WebView.Windows.cs b/src/Controls/src/Core/HandlerImpl/WebView/WebView.Windows.cs
index 115f0ac8a7d5..b1d79ccbf701 100644
--- a/src/Controls/src/Core/HandlerImpl/WebView/WebView.Windows.cs
+++ b/src/Controls/src/Core/HandlerImpl/WebView/WebView.Windows.cs
@@ -1,9 +1,21 @@
-using Microsoft.Maui.Controls.Platform;
-using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
+#nullable enable
+using System;
namespace Microsoft.Maui.Controls
{
public partial class WebView
{
+ protected override void OnPropertyChanging(string? propertyName = null)
+ {
+ base.OnPropertyChanging(propertyName);
+
+ if (Window is not null && propertyName == nameof(Window))
+ Window.Destroying -= OnWindowDestroying;
+ }
+
+ void OnWindowDestroying(object? sender, EventArgs e)
+ {
+ Handler?.DisconnectHandler();
+ }
}
}
\ No newline at end of file
diff --git a/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt b/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt
index 1e2360ee9067..251574c360a4 100644
--- a/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt
+++ b/src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt
@@ -1,5 +1,6 @@
#nullable enable
*REMOVED*override Microsoft.Maui.Controls.RefreshView.MeasureOverride(double widthConstraint, double heightConstraint) -> Microsoft.Maui.Graphics.Size
+override Microsoft.Maui.Controls.WebView.OnPropertyChanging(string? propertyName = null) -> void
~Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.FrameRenderer(Microsoft.Maui.IPropertyMapper mapper) -> void
~Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.FrameRenderer(Microsoft.Maui.IPropertyMapper mapper, Microsoft.Maui.CommandMapper commandMapper) -> void
override Microsoft.Maui.Controls.View.ChangeVisualState() -> void
diff --git a/src/Controls/src/Core/WebView.cs b/src/Controls/src/Core/WebView.cs
index ae41abb07993..f78890b18879 100644
--- a/src/Controls/src/Core/WebView.cs
+++ b/src/Controls/src/Core/WebView.cs
@@ -194,6 +194,10 @@ protected override void OnPropertyChanged(string propertyName)
if (source != null)
SetInheritedBindingContext(source, BindingContext);
}
+#if WINDOWS
+ else if (Window is not null && propertyName == nameof(Window))
+ Window.Destroying += OnWindowDestroying;
+#endif
base.OnPropertyChanged(propertyName);
}
diff --git a/src/Core/src/Handlers/WebView/WebViewHandler.Windows.cs b/src/Core/src/Handlers/WebView/WebViewHandler.Windows.cs
index 878bb1a90283..3d1df953afdf 100644
--- a/src/Core/src/Handlers/WebView/WebViewHandler.Windows.cs
+++ b/src/Core/src/Handlers/WebView/WebViewHandler.Windows.cs
@@ -35,10 +35,14 @@ protected override void DisconnectHandler(WebView2 platformView)
platformView.CoreWebView2.HistoryChanged -= OnHistoryChanged;
platformView.CoreWebView2.NavigationStarting -= OnNavigationStarting;
platformView.CoreWebView2.NavigationCompleted -= OnNavigationCompleted;
+
+ platformView.CoreWebView2.Stop();
}
platformView.CoreWebView2Initialized -= OnCoreWebView2Initialized;
+ platformView.Close();
+
base.DisconnectHandler(platformView);
}
diff --git a/src/Core/tests/DeviceTests/Handlers/WebView/WebViewHandlerTests.Windows.cs b/src/Core/tests/DeviceTests/Handlers/WebView/WebViewHandlerTests.Windows.cs
index efedc99bf5ce..76fd20d90357 100644
--- a/src/Core/tests/DeviceTests/Handlers/WebView/WebViewHandlerTests.Windows.cs
+++ b/src/Core/tests/DeviceTests/Handlers/WebView/WebViewHandlerTests.Windows.cs
@@ -1,9 +1,33 @@
-using Microsoft.UI.Xaml.Controls;
+using System.Threading.Tasks;
+using Microsoft.Maui.Controls;
+using Microsoft.Maui.DeviceTests.Stubs;
+using Microsoft.UI.Xaml.Controls;
+using Xunit;
namespace Microsoft.Maui.DeviceTests
{
public partial class WebViewHandlerTests
{
+ [Fact(DisplayName = "Disconnect Handler close the Platform WebView2")]
+ public async Task DisconnectHandlerCloseWebView()
+ {
+ var webView = new WebViewStub()
+ {
+ Source = new UrlWebViewSourceStub { Url = "https://dotnet.microsoft.com/" }
+ };
+
+ var handler = await CreateHandlerAsync(webView);
+
+ var coreWebView2 = await InvokeOnMainThreadAsync(() =>
+ {
+ var platformView = (WebView2)webView.Handler.PlatformView;
+ webView.Handler.DisconnectHandler();
+ return platformView.CoreWebView2;
+ });
+
+ Assert.Null(coreWebView2);
+ }
+
WebView2 GetNativeWebView(WebViewHandler webViewHandler) =>
webViewHandler.PlatformView;