Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
Padding="12"
Spacing="6">

<WebView
HeightRequest="300"
WidthRequest="300"
Source="https://github.com/dotnet/maui" />

<Label Text="Current Window Frame:" />
<Label Text="{Binding Window.X, StringFormat='X = {0:0.00}'}" />
<Label Text="{Binding Window.Y, StringFormat='Y = {0:0.00}'}" />
Expand Down
16 changes: 14 additions & 2 deletions src/Controls/src/Core/HandlerImpl/WebView/WebView.Windows.cs
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @PureWeen just added a WindowChanged event - and we may want to make that public. Maybe when a window is closing, we can propagate a Window = null change?

{
base.OnPropertyChanging(propertyName);

if (Window is not null && propertyName == nameof(Window))
Window.Destroying -= OnWindowDestroying;
}

void OnWindowDestroying(object? sender, EventArgs e)
{
Handler?.DisconnectHandler();
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/Controls/src/Core/WebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Core/src/Handlers/WebView/WebViewHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down