c# – Index was out of range when navigating between pages back and forth


I have a project written in C# MAUI 8. Application is created using Shell. The problem occurs when app is deployed to iPhone or to simulator (VS2022 paired with Mac with XCode). I have never a chance to get some experience working with Apple devices so every tip/hint would be valuable for me.

Situation: To navigate between pages i use await Shell.Current.Navigation.PushAsync(somePage) and await Shell.Current.Navigation.PopAsync().

Let’s say i have Page A, B and C. Using methods mentioned above i want to achieve such navigation flow: A -(PushAsync)> B -(PushAsync)> C -(PopAsync and here exception occurs)> B

Message: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

Source: Microsoft.IOS

StackTrace: at ObjCRuntime.Runtime.ThrowException(IntPtr gchandle) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:line 2594 at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 60 at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94 at MyApp.Program.Main(String[] args) in X:\Projects\MyApp\Platforms\iOS\Program.cs:line 13

Content of MyApp/Platforms/iOS/Program.cs

public class Program
{
    static void Main(string[] args)
    {
        UIApplication.Main(args, null, typeof(AppDelegate));
    }
}

What should i do now? How to debug this exception?

I have been trying to substitute Push and Pops by using GoToAsyncs but the effect is the same.

All the code works fine on Android devices.

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();
    }
}
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="MyApp.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MyApp"
    xmlns:views="clr-namespace:MyApp.Views"
    Shell.TabBarIsVisible="False">

    <ShellContent
        Title="Login"
        ContentTemplate="{DataTemplate views:LoginPage}"
        FlyoutItemIsVisible="False"
        Route="LoginPage"
        Shell.FlyoutBehavior="Disabled" />
</Shell>

MauiProgram.cs

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseMauiCommunityToolkit()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                fonts.AddFont("FA6-Solid-900.otf", "FA6Solid");
            });

        var a = Assembly.GetExecutingAssembly();
        using var stream = a.GetManifestResourceStream("MyApp.appsettings.json");

        var config = new ConfigurationBuilder()
            .AddJsonStream(stream)
            .Build();
        builder.Configuration.AddConfiguration(config);
        
        builder.Services.AddSingleton<AppShell>();
        builder.Services.AddTransient<LoginPage>();
        builder.Services.AddSingleton<PageB>();
        builder.Services.AddTransient<PageC>();

        return builder.Build();
    }
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img