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
80 changes: 80 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue33067.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Collections.ObjectModel;
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

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

The System.Collections.ObjectModel namespace is not used in this file and should be removed.

Suggested change
using System.Collections.ObjectModel;

Copilot uses AI. Check for mistakes.

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 33067, "[Windows, Android] ScrollView Content Not Removed When Set to Null", PlatformAffected.Android | PlatformAffected.UWP)]

public class Issue33067 : ContentPage
{
ScrollView _scrollView = null!;
Label _originalContent = null!;
Button _setNullButton = null!;
Button _addContentButton = null!;

public Issue33067()
{
CreateUI();
}

void CreateUI()
{
// Create the original content label
_originalContent = new Label
{
Text = "This is a sample label inside the ScrollView that can be set to null and added back.",
Padding = new Thickness(20),
AutomationId = "ContentLabel",
FontSize = 16
};

// Create the ScrollView
_scrollView = new ScrollView
{
BackgroundColor = Colors.LightGray,
HeightRequest = 300,
Content = null
};

// Create the "Set Content to Null" button
_setNullButton = new Button
{
Text = "Set Content to Null",
AutomationId = "SetNullButton"
};
_setNullButton.Clicked += OnSetContentNullClicked;

// Create the "Add Content" button
_addContentButton = new Button
{
Text = "Add Content",
AutomationId = "AddContentButton"
};
_addContentButton.Clicked += OnAddContentClicked;

// Create the main layout
var layout = new VerticalStackLayout
{
Spacing = 20,
Padding = new Thickness(30),
Children = { _setNullButton, _addContentButton, _scrollView }
};

// Set the content of the page
Content = layout;
}

void OnSetContentNullClicked(object sender, EventArgs e)
{
// Set ScrollView content to null
_scrollView.Content = null;
}

void OnAddContentClicked(object sender, EventArgs e)
{
// Restore the original content
if (_originalContent != null)
{
_scrollView.Content = _originalContent;
}
Comment on lines +75 to +78
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

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

The null check for _originalContent is unnecessary here because it's initialized as null! (null-forgiving operator) on line 10 and is always assigned a non-null value in CreateUI() before any button click can occur. This check can be simplified to just _scrollView.Content = _originalContent;

Suggested change
if (_originalContent != null)
{
_scrollView.Content = _originalContent;
}
_scrollView.Content = _originalContent;

Copilot uses AI. Check for mistakes.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;
public class Issue33067 : _IssuesUITest
{
public Issue33067(TestDevice device) : base(device) { }

public override string Issue => "[Windows, Android] ScrollView Content Not Removed When Set to Null";

[Test, Order(1)]
[Category(UITestCategories.ScrollView)]
public void VerifyScrollViewContentShouldNull()
{
App.WaitForElement("SetNullButton");
App.WaitForNoElement("ContentLabel");
}

[Test, Order(2)]
[Category(UITestCategories.ScrollView)]
public void VerifyScrollViewContentWhenSetToNull()
{
App.WaitForElement("SetNullButton");
App.Tap("AddContentButton");
App.WaitForElement("ContentLabel");
App.Tap("SetNullButton");
App.WaitForNoElement("ContentLabel");
}
}
15 changes: 13 additions & 2 deletions src/Core/src/Handlers/ScrollView/ScrollViewHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,26 @@ The methods below exist to support inserting/updating the extra padding/margin l

static void UpdateInsetView(IScrollView scrollView, IScrollViewHandler handler, ICrossPlatformLayout crossPlatformLayout)
{
if (scrollView.PresentedContent == null || handler.MauiContext == null)
if (handler.MauiContext is null)
{
return;
}

// Find existing inset panel once
var currentPaddingLayer = FindInsetPanel(handler);

// If PresentedContent is null, clean up any existing content and return
if (scrollView.PresentedContent is null)
{
currentPaddingLayer?.RemoveAllViews();
return;
}

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

if (FindInsetPanel(handler) is ContentViewGroup currentPaddingLayer)
if (currentPaddingLayer is not null)
{
// Only update if content has changed or is missing
if (currentPaddingLayer.ChildCount == 0 || currentPaddingLayer.GetChildAt(0) != nativeContent)
{
currentPaddingLayer.RemoveAllViews();
Expand Down
19 changes: 16 additions & 3 deletions src/Core/src/Handlers/ScrollView/ScrollViewHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,34 @@ The methods below exist to support inserting/updating the padding/margin panel.

static void UpdateContentPanel(IScrollView scrollView, IScrollViewHandler handler, ICrossPlatformLayout crossPlatformLayout)
{
if (scrollView.PresentedContent == null || handler.MauiContext == null)
if (handler.MauiContext is null)
{
return;
}

var scrollViewer = handler.PlatformView;
var currentPaddingLayer = GetContentPanel(scrollViewer);

// If PresentedContent is null, clean up any existing content and return
if (scrollView.PresentedContent is null)
{
if (currentPaddingLayer is not null)
{
currentPaddingLayer.CachedChildren.Clear();
}

Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

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

Trailing whitespace should be removed from this empty line. This can be caught by running dotnet format before committing.

Suggested change

Copilot uses AI. Check for mistakes.
return;
}

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

if (GetContentPanel(scrollViewer) is ContentPanel currentPaddingLayer)
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);

}
}
else
Expand Down
Loading