Skip to content
Merged
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 @@ -111,6 +111,8 @@ protected override void ConnectHandler(AWebView platformView)

protected override void DisconnectHandler(AWebView platformView)
{
DisconnectRootComponents();

_backPressedCallback?.Remove();
_backPressedCallback?.Dispose();
_backPressedCallback = null;
Expand Down
9 changes: 9 additions & 0 deletions src/BlazorWebView/src/Maui/BlazorWebViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ internal void UrlLoading(UrlLoadingEventArgs args) =>

private RootComponentsCollection? _rootComponents;

private void DisconnectRootComponents()
{
if (_rootComponents != null)
{
_rootComponents.CollectionChanged -= OnRootComponentsCollectionChanged;
_rootComponents = null;
}
}

private RootComponentsCollection? RootComponents
{
get => _rootComponents;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ protected override void ConnectHandler(NWebView platformView)
/// <inheritdoc />
protected override void DisconnectHandler(NWebView platformView)
{
DisconnectRootComponents();

platformView.PageLoadFinished -= OnLoadFinished;
base.DisconnectHandler(platformView);
s_webviewHandlerTable.Remove(GetHashCode().ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ protected override WebView2Control CreatePlatformView()
/// <inheritdoc />
protected override void DisconnectHandler(WebView2Control platformView)
{
DisconnectRootComponents();

if (_webviewManager != null)
{
// Start the disposal...
Expand Down
2 changes: 2 additions & 0 deletions src/BlazorWebView/src/Maui/iOS/BlazorWebViewHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ private void MessageReceived(Uri uri, string message)
/// <inheritdoc />
protected override void DisconnectHandler(WKWebView platformView)
{
DisconnectRootComponents();

platformView.StopLoading();

if (_webviewManager != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.WebView.Maui;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.MauiBlazorWebView.DeviceTests.Components;
using Xunit;

namespace Microsoft.Maui.MauiBlazorWebView.DeviceTests.Elements;

[Category(TestCategory.BlazorWebView)]
public class BlazorWebViewLifecycleTests : Microsoft.Maui.DeviceTests.ControlsHandlerTestBase
{
[Fact]
public async Task DisconnectUnsubscribesRootComponentsWithoutClearingThem()
{
EnsureHandlerCreated(additionalCreationActions: builder =>
{
builder.Services.AddMauiBlazorWebView();
});

await InvokeOnMainThreadAsync(() =>
{
// No HostPage isolates subscription cleanup from WebViewManager disposal.
var view = new BlazorWebView();
var component = new RootComponent { ComponentType = typeof(NoOpComponent), Selector = "#app" };
view.RootComponents.Add(component);
var handler = CreateHandler<BlazorWebViewHandler>(view);

try
{
Assert.Single(GetHandlerSubscriptions(view.RootComponents));
((IElementHandler)handler).DisconnectHandler();

Assert.Empty(GetHandlerSubscriptions(view.RootComponents));
Assert.Same(component, Assert.Single(view.RootComponents));

handler.SetVirtualView(view);
Assert.Same(handler, Assert.Single(GetHandlerSubscriptions(view.RootComponents)).Target);
Assert.Same(component, Assert.Single(view.RootComponents));
}
finally
{
((IElementHandler)handler).DisconnectHandler();
}
});
}

static Delegate[] GetHandlerSubscriptions(RootComponentsCollection components)
{
var field = typeof(ObservableCollection<RootComponent>).GetField(
nameof(INotifyCollectionChanged.CollectionChanged), BindingFlags.Instance | BindingFlags.NonPublic);
Assert.NotNull(field);
var subscription = (NotifyCollectionChangedEventHandler)field.GetValue(components);
return Array.FindAll(subscription?.GetInvocationList() ?? Array.Empty<Delegate>(),
callback => callback.Target is BlazorWebViewHandler);
}
}
Loading