Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggling Header/Footer in CollectionView Dynamically throws an exception #25743

Merged
merged 3 commits into from
Nov 18, 2024
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 @@ -577,7 +577,7 @@ internal void UpdateView(object view, DataTemplate viewTemplate, ref UIView uiVi

uiView?.Dispose();
uiView = null;

formsElement?.Handler?.DisconnectHandler();
Copy link
Member

@rmarinho rmarinho Nov 12, 2024

Choose a reason for hiding this comment

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

Do we need this fix on ItemsViewController2 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

@rmarinho ,
I've tested this scenario with CollectionViewHandler2, and since the header and footer handling differs in that implementation, the issue does not occur. Dynamically setting the header, footer, and empty view to null works without any exceptions in CollectionViewHandler2, so this fix isn't needed there.

formsElement = null;
}
else
Expand Down
51 changes: 51 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25724.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue25724"
xmlns:local="clr-namespace:Maui.Controls.Sample.Issues"
x:Name="ThisMainPage"
Title="Main Page">

<Grid RowDefinitions="Auto,*">

<HorizontalStackLayout
Grid.Row="0"
Padding="20"
HorizontalOptions="Center"
Spacing="20">
<Button AutomationId="ToggleHeaderButton" Text="Toggle Header" Clicked="ToggleHeader"></Button>
<Button AutomationId="ToggleFooterButton" Text="Toggle Footer" Clicked="ToggleFooter"></Button>
</HorizontalStackLayout>

<CollectionView x:Name="CollectionView" Grid.Row="1" ItemsSource="{Binding ItemList}">

<CollectionView.Header>
<Label
Padding="10"
FontAttributes="Bold"
FontSize="Large"
Text="This Is A Header" />
</CollectionView.Header>

<CollectionView.ItemTemplate>
<DataTemplate>
<Label Padding="20,5,5,5" Text="{Binding .}" />
</DataTemplate>
</CollectionView.ItemTemplate>

<CollectionView.EmptyView>
<Label Padding="20,5,5,5" Text="Empty" />
</CollectionView.EmptyView>

<CollectionView.Footer>
<Label
Padding="10"
FontAttributes="Bold"
FontSize="Large"
Text="This Is A Footer" />
</CollectionView.Footer>

</CollectionView>

</Grid>
</ContentPage>
54 changes: 54 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25724.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#nullable enable
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 25724, "ObjectDisposedException When Toggling Header/Footer in CollectionView Dynamically", PlatformAffected.iOS)]

public partial class Issue25724 : ContentPage
rmarinho marked this conversation as resolved.
Show resolved Hide resolved
{
object? header;
object? footer;
public Issue25724()
{
InitializeComponent();
BindingContext = new Issue25724ViewModel();
}

void ToggleHeader(object sender, System.EventArgs e)
{
header = CollectionView.Header ?? header;

if (CollectionView.Header == null)
CollectionView.Header = header;
else
CollectionView.Header = null;
}

void ToggleFooter(object sender, System.EventArgs e)
{
footer = CollectionView.Footer ?? footer;

if (CollectionView.Footer == null)
CollectionView.Footer = footer;
else
CollectionView.Footer = null;
}

internal class Issue25724ViewModel
{
public ObservableCollection<string> ItemList { get; set; }
public Issue25724ViewModel()
{
// Initialize the ItemList
ItemList = new ObservableCollection<string>();
}

}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if IOS || MACCATALYST
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

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

public override string Issue => "ObjectDisposedException When Toggling Header/Footer in CollectionView Dynamically";

[Test]
[Category(UITestCategories.CollectionView)]
public void CollectionViewDynamicHeaderShouldNotCrashOnDisplay()
{
App.WaitForElement("This Is A Header");
App.Tap("ToggleHeaderButton"); // This is the button that toggles the header to null
App.Tap("ToggleHeaderButton");// This is the button that toggles the header back to the original value
App.WaitForElement("This Is A Header");
}
}
}
#endif
Loading