diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21374.xaml b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21374.xaml
new file mode 100644
index 000000000000..a66ada398dd5
--- /dev/null
+++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21374.xaml
@@ -0,0 +1,48 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21374.xaml.cs b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21374.xaml.cs
new file mode 100644
index 000000000000..ddbc0080629b
--- /dev/null
+++ b/src/Controls/samples/Controls.Sample.UITests/Issues/Issue21374.xaml.cs
@@ -0,0 +1,86 @@
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.Runtime.CompilerServices;
+using System.Windows.Input;
+using Microsoft.Maui.Controls;
+using Microsoft.Maui.Controls.Xaml;
+
+namespace Maui.Controls.Sample.Issues
+{
+ [XamlCompilation(XamlCompilationOptions.Compile)]
+ [Issue(IssueTracker.Github, 21374, "Error when adding to ObservableCollection", PlatformAffected.iOS)]
+ public partial class Issue21374 : ContentPage
+ {
+ public Issue21374()
+ {
+ InitializeComponent();
+ }
+ }
+
+ public class Issue21374Model
+ {
+ public string Text { get; set; }
+ }
+
+ public class Issue21374ViewModel : INotifyPropertyChanged
+ {
+ string _success;
+ ObservableCollection _items = new ObservableCollection();
+
+ public ICommand PopulateItemsCommand => new Command(PopulateItems);
+
+ public string Success
+ {
+ get => _success;
+ set
+ {
+ _success = value;
+ OnPropertyChanged("Success");
+ }
+ }
+
+ public ObservableCollection Items
+ {
+ get => _items;
+ set
+ {
+ if (_items != value)
+ {
+ _items = value;
+ OnPropertyChanged("Items");
+ }
+ }
+ }
+
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ void PopulateItems()
+ {
+ try
+ {
+ for (int j = 0; j < 10; j++)
+ {
+ Items.Add(new Issue21374Model { Text = $"Item {j + 1}" });
+ }
+
+ Success = "Success";
+ }
+ catch
+ {
+ Success = "Failed";
+ }
+ }
+
+ protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
+ {
+ PropertyChangedEventHandler changed = PropertyChanged;
+
+ if (changed == null)
+ {
+ return;
+ }
+
+ changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs b/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs
index 9499aaba8124..2ac979ffd6f2 100644
--- a/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs
+++ b/src/Controls/src/Core/Handlers/Items/iOS/ItemsViewLayout.cs
@@ -392,17 +392,11 @@ public override UICollectionViewLayoutInvalidationContext GetInvalidationContext
if (preferredAttributes.RepresentedElementKind != UICollectionElementKindSectionKey.Header
&& preferredAttributes.RepresentedElementKind != UICollectionElementKindSectionKey.Footer)
{
- if (OperatingSystem.IsIOSVersionAtLeast(12) || OperatingSystem.IsTvOSVersionAtLeast(12))
- {
- return base.GetInvalidationContext(preferredAttributes, originalAttributes);
- }
-
try
{
// We only have to do this on older iOS versions; sometimes when removing a cell that's right at the edge
// of the viewport we'll run into a race condition where the invalidation context will have the removed
// indexpath. And then things crash. So
-
var defaultContext = base.GetInvalidationContext(preferredAttributes, originalAttributes);
return defaultContext;
}
@@ -410,6 +404,10 @@ public override UICollectionViewLayoutInvalidationContext GetInvalidationContext
{
Application.Current?.FindMauiContext()?.CreateLogger()?.LogWarning(ex, "NSRangeException");
}
+ catch (ObjCRuntime.ObjCException ex) when (ex.Name == "NSInvalidArgumentException")
+ {
+ Application.Current?.FindMauiContext()?.CreateLogger()?.LogWarning(ex, "NSInvalidArgumentException");
+ }
UICollectionViewFlowLayoutInvalidationContext context = new UICollectionViewFlowLayoutInvalidationContext();
return context;
diff --git a/src/Controls/tests/UITests/Tests/Issues/Issue21374.cs b/src/Controls/tests/UITests/Tests/Issues/Issue21374.cs
new file mode 100644
index 000000000000..3b1e267dfabd
--- /dev/null
+++ b/src/Controls/tests/UITests/Tests/Issues/Issue21374.cs
@@ -0,0 +1,32 @@
+using NUnit.Framework;
+using UITest.Appium;
+using UITest.Core;
+
+namespace Microsoft.Maui.AppiumTests.Issues
+{
+ public class Issue21374 : _IssuesUITest
+ {
+ public Issue21374(TestDevice device) : base(device)
+ {
+ }
+
+ public override string Issue => "Error when adding to ObservableCollection";
+
+ [Test]
+ [Category(UITestCategories.CollectionView)]
+ public void AddingItemsToObservableCollectionNoCrash()
+ {
+ this.IgnoreIfPlatforms(new[]
+ {
+ TestDevice.Android,
+ TestDevice.Mac,
+ TestDevice.Windows
+ });
+
+ App.WaitForElement("WaitForStubControl");
+ App.Click("WaitForStubControl");
+ var result = App.WaitForElement("Success").GetText();
+ Assert.AreEqual("Success", result);
+ }
+ }
+}
\ No newline at end of file