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 @@ -25,18 +25,31 @@ protected override UICollectionViewDelegateFlowLayout CreateDelegator()
// _Only_ called if the user initiates the selection change; will not be called for programmatic selection
public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
if (ItemsView?.ItemsSource is null)
{
return;
}
FormsSelectItem(indexPath);
}

// _Only_ called if the user initiates the selection change; will not be called for programmatic selection
public override void ItemDeselected(UICollectionView collectionView, NSIndexPath indexPath)
{
if (ItemsView?.ItemsSource is null)
{
return;
}
FormsDeselectItem(indexPath);
}

// Called by Forms to mark an item selected
internal void SelectItem(object selectedItem)
{
if (ItemsView?.ItemsSource is null)
{
return;
}

var index = GetIndexForItem(selectedItem);

if (index.Section > -1 && index.Item > -1)
Expand All @@ -52,6 +65,11 @@ internal void SelectItem(object selectedItem)
// Called by Forms to clear the native selection
internal void ClearSelection()
{
if (ItemsView?.ItemsSource is null)
{
return;
}

var selectedItemIndexes = CollectionView.GetIndexPathsForSelectedItems();

foreach (var index in selectedItemIndexes)
Expand Down Expand Up @@ -95,7 +113,7 @@ void FormsDeselectItem(NSIndexPath indexPath)

internal void UpdatePlatformSelection()
{
if (ItemsView == null)
if (ItemsView?.ItemsSource is null)
Comment thread
bhavanesh2001 marked this conversation as resolved.
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,31 @@ protected override UICollectionViewDelegateFlowLayout CreateDelegator()
// _Only_ called if the user initiates the selection change; will not be called for programmatic selection
public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
if (ItemsView?.ItemsSource is null)
{
return;
}
FormsSelectItem(indexPath);
}

// _Only_ called if the user initiates the selection change; will not be called for programmatic selection
public override void ItemDeselected(UICollectionView collectionView, NSIndexPath indexPath)
{
if (ItemsView?.ItemsSource is null)
{
return;
}
FormsDeselectItem(indexPath);
}

// Called by Forms to mark an item selected
internal void SelectItem(object selectedItem)
{
if (ItemsView?.ItemsSource is null)
{
return;
}

var index = GetIndexForItem(selectedItem);

if (index.Section > -1 && index.Item > -1)
Expand All @@ -52,6 +65,11 @@ internal void SelectItem(object selectedItem)
// Called by Forms to clear the native selection
internal void ClearSelection()
{
if (ItemsView?.ItemsSource is null)
{
return;
}

var selectedItemIndexes = CollectionView.GetIndexPathsForSelectedItems();

foreach (var index in selectedItemIndexes)
Expand Down Expand Up @@ -95,7 +113,7 @@ void FormsDeselectItem(NSIndexPath indexPath)

internal void UpdatePlatformSelection()
{
if (ItemsView == null)
if (ItemsView?.ItemsSource is null)
{
return;
}
Expand Down
27 changes: 27 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue29882.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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.Issue29882"
Title="Issue29882">
<VerticalStackLayout>
<Label Text="Issue 29882" AutomationId="MauiLabel" />
<CollectionView
SelectionMode="Single" ItemsSource="{Binding Items}"
x:Name="MyCollectionView" SelectionChanged="MyCollectionView_SelectionChanged"
Margin="10">
<CollectionView.ItemTemplate>
<DataTemplate>
<Border Stroke="LightGray" Padding="10" Margin="5">
<VerticalStackLayout>
<Label Text="{Binding .}"
FontSize="18" AutomationId="{Binding .}"
VerticalOptions="Center"
HorizontalOptions="Center"/>
</VerticalStackLayout>
</Border>
</DataTemplate>
</CollectionView.ItemTemplate>

</CollectionView>
</VerticalStackLayout>
</ContentPage>
33 changes: 33 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue29882.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 29882, "[iOS] Crash occurs when ItemsSource is set to null in the SelectionChanged handler",PlatformAffected.iOS)]
public partial class Issue29882 : ContentPage
{
private ObservableCollection<string> items;
public ObservableCollection<string> Items
{
get => items;
set
{
items = value;
OnPropertyChanged();
}
}
public Issue29882()
{
InitializeComponent();
Items = new ObservableCollection<string>();
for (int i = 1; i <= 10; i++)
{
Items.Add($"Item{i}");
}
BindingContext = this;
}

private void MyCollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Items = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

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

public override string Issue => "[iOS] Crash occurs when ItemsSource is set to null in the SelectionChanged handler";

[Test]
[Category(UITestCategories.CollectionView)]
public void SettingItemSourceToNullShouldNotCrash()
{
App.WaitForElement("Item1");
App.Tap("Item1");
App.WaitForElement("MauiLabel"); // If app doesn't crash, test passes.
}
}
Loading