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
56 changes: 56 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue35277.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 35277, "COMException when restoring a page content after swapping it out", PlatformAffected.UWP)]
public class Issue35277 : ContentPage
{
ScrollView _originalScrollView;

public Issue35277()
{
var swapButton = new Button
{
Text = "Swap and Restore",
HorizontalOptions = LayoutOptions.Center,
AutomationId = "SwapAndRestoreButton"
};
swapButton.Clicked += OnSwapAndRestoreClicked;

_originalScrollView = new ScrollView
{
Content = new VerticalStackLayout
{
Spacing = 20,
VerticalOptions = LayoutOptions.Center,
Children =
{
swapButton,
new Label
{
Text = "Original Content",
HorizontalOptions = LayoutOptions.Center,
AutomationId = "OriginalLabel"
}
}
}
};

Content = _originalScrollView;
}

void OnSwapAndRestoreClicked(object sender, EventArgs e)
{
var savedScrollView = _originalScrollView;
Content = new Label
{
Text = "Temporary Content",
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
AutomationId = "TemporaryLabel"
};

Microsoft.Maui.ApplicationModel.MainThread.BeginInvokeOnMainThread(() =>
{
Content = savedScrollView;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue35277 : _IssuesUITest
{
public Issue35277(TestDevice device) : base(device) { }

public override string Issue => "COMException when restoring a page content after swapping it out";

[Test]
[Category(UITestCategories.ScrollView)]
public void ScrollViewContentShouldRestoreWithoutCOMException()
{
App.WaitForElement("SwapAndRestoreButton");
App.Tap("SwapAndRestoreButton");
// If no COMException is thrown, the original ScrollView content (with the button) restores successfully
App.WaitForElement("SwapAndRestoreButton");
}
}
20 changes: 13 additions & 7 deletions src/Core/src/Handlers/ScrollView/ScrollViewHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ protected override void ConnectHandler(ScrollViewer platformView)

protected override void DisconnectHandler(ScrollViewer platformView)
{
// Cascade disconnect to the PresentedContent's handler. Without this, when the
// ScrollView is restored as ContentPage.Content after being swapped out, the
// PresentedContent's native view still has a stale parent (the old ContentPanel),
// causing a WinUI COM exception "Element already has a parent" (Issue #35277).
// Cascading here ensures ToPlatform() creates a fresh native view with no parent.
VirtualView?.PresentedContent?.Handler?.DisconnectHandler();
base.DisconnectHandler(platformView);
platformView.ViewChanged -= ViewChanged;
}
Expand Down Expand Up @@ -149,20 +155,20 @@ static void UpdateContentPanel(IScrollView scrollView, IScrollViewHandler handle
{
currentPaddingLayer.CachedChildren.Clear();
}

return;
}

if (currentPaddingLayer is not null)
{
currentPaddingLayer.CachedChildren.Clear();
}

var nativeContent = scrollView.PresentedContent.ToPlatform(handler.MauiContext);

if (currentPaddingLayer is not null)
{
// Only update if content has changed or is missing
if (currentPaddingLayer.CachedChildren.Count == 0 || currentPaddingLayer.CachedChildren[0] != nativeContent)
{
currentPaddingLayer.CachedChildren.Clear();
currentPaddingLayer.CachedChildren.Add(nativeContent);
}
currentPaddingLayer.CachedChildren.Add(nativeContent);
}
else
{
Expand Down
Loading