Share via

How to fix InvalidationContext error

Timothy John Gandionco 0 Reputation points
2023-06-20T14:43:01.1566667+00:00

Hello,

I'm getting this kind of error when running MAUI project for iOS. I'm running on an iOS simulator.

View

<CollectionView ItemsSource="{Binding HundredsList}">
    <CollectionView.ItemsLayout>
		<LinearItemsLayout Orientation="Vertical" />
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>
             <Label Text="{Binding Text}"></Label>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

Models

//Enum
public enum InputType
{ 
	Grid = 0, 
	Numpad = 1
}

//GridOptions
public class GridOptions
{
 	private string text;
 	public string Text
 	{
 		get => text;
 		set => text = value;
 	}
     private object val;
     public object Val
     {
         get => val;
         set => val = value;
     }
}

//Hymn
public class Hymn
{
     public string Number { get; set; }
     public string Title { get; set; }
     public string Lyrics { get; set; }
     public string FirstLine { get; set; }
}

//HymnList
using System.Collections.Generic;

public class HymnList: List<Hymn>
{
    public HymnList() : base() { }
    public HymnList(IEnumerable<Hymn> hymns) : base()
	{
 		this.AddRange(hymns);
   	}
   	public Hymn this[string number]
 	{
 		get => Find(x => x.Number == number); 
	}
}

//Globals.cs
private HymnList hymnList = new HymnList();
public HymnList HymnList
{ 			
	get => hymnList;
	set
	{
		hymnList = value;
	} 
}


//main model extending MvvmHelpers.BaseViewModel;
using MvvmHelpers;
private Globals globalInstance = Globals.Instance;

private InputType hymnInputType;
public InputType HymnInputType 
{
     get => hymnInputType;
     set
     {
         SetProperty(ref hymnInputType, value, nameof(HymnInputType));
     }
}

private ObservableRangeCollection<GridOptions> hundredsList = new ObservableRangeCollection<GridOptions>();
public ObservableRangeCollection<GridOptions> HundredsList
{
     get => hundredsList;
     set
     {
         hundredsList = value;
         SetProperty(ref hundredsList, hundredsList, nameof(HundredsList));
         OnPropertyChanged();
     }
}

private async void InitGrid()
{
     if (HymnInputType == InputType.Grid && !HundredsList.Any())
     {
         await Task.Delay(2000); 
         maxNum = Convert.ToInt32(new Regex("fst").Replace(globalInstance.HymnList[globalInstance.HymnList.Count - 1].Number, ""));
         maxNumString = globalInstance.HymnList[globalInstance.HymnList.Count - 1].Number;
         var totalHundreds = maxNum / 100 + 1;
         for (var i = 0; i < totalHundreds; i++)
         {
             HundredsList.Add(new Models.GridOptions
             {
                 Text = (100 * i + 1) + "-" + Math.Min(100 * (i + 1), maxNum),
                 Val = i
             });
         }
     }
}

Upon adding item to HundredsList collection , I get this error:

Upon adding item to HundredsList collection , I get this error:

"The invalidation context ((null)) sent to -[UICollectionViewFlowLayout invalidateLayoutWithContext:] is not an instance of type UICollectionViewFlowLayoutInvalidationContext or a subclass. Collection view: <UICollectionView: 0x7fc57cd07a00; frame = (0 0; 0 2796); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600003821f50>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x600003ca4180>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}; layout: <Microsoft_Maui_Controls_Handlers_Items_ListViewLayout: 0x7fc57db3bcc0>; dataSource: <Microsoft_Maui_Controls_Handlers_Items_ReorderableItemsViewController_1: 0x7fc57bf19dc0>>"

"   at ObjCRuntime.Runtime.ThrowException(IntPtr gchandle)\n   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)\n   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)\n   at MobiHymn_MAUI.Program.Main(String[] args) in /Users/timothyg/Projects/MobiHymn_MAUI/MobiHymn_MAUI/Platforms/iOS/Program.cs:line 13"

Please help not familiar with the error. I can run successfully on Android.

Please help not familiar with the error. I can run successfully on Android.

Versions:

.NET Version:	7.0
VS for Mac:		17.5.7 (build 6)
MAUI version:	7.0.86
iOS Simulator: 	Phone 14 Pro Max
OS:				iOS 16.4

NugetPackages:

Refactored.MvvmHelpers:	1.6.2

Please help not familiar with the error. I can run successfully on Android.

Developer technologies | .NET | .NET Multi-platform App UI

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.