skip to Main Content

Im creating a .NET MAUI app and following along with a guide on youtube (https://www.youtube.com/watch?v=ddmZ6k1GIkM&t=406s) but altering what the app shows, but the functionality is mostly the same.

When I add navigation to a page and execute that code to go to the new page, I get ArgumentOutOfRangeException and the break mode screen comes up in Visual Studio 2022.

Im using the MVVM community toolkit version 8.1 (latest).
Im not passing any values from page A to new page B (I will once I sort this error out).

My ViewModel for page A contains a [RelayCommand] bound to a button on my UI (this is working fine) but at the end of this [RelayCommand] there is a call to a method which then takes you to the next page (intentionally, as there is no need for user input at this point so I want a different page to come up by itself).

That [RelayCommand] does some calculation then calls GoToInfoPage(), this is not marked [RelayCommand] as it doesnt need to be bound to anything on the UI.

GoToInfoPage() is below

async Task GoToInfoPage()
{
 await Shell.Current.GoToAsync(nameof(InfoPage));
}

The XAML on the InfoPage is simple right now:

<?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"
             xmlns:viewmodel="clr-namespace:People.ViewModels"
             x:DataType="viewmodel:InfoPageViewModel"
             x:Class="People.Views.InfoPage"
             Title="InfoPage">

    <VerticalStackLayout>
        <Label Text="InfoPage screen" />
    </VerticalStackLayout>
</ContentPage>

Its code behind is:

public partial class InfoPage : ContentPage
{
    public InfoPage(InfoPageViewModel vm)
    {
        InitializeComponent();
        BindingContext = vm;
    }
}

The view and VM are registered in MauiProgram.cs

builder.Services.AddTransient<InfoPage>();
builder.Services.AddTransient<InfoPageViewModel>();

The route is registered in AppShell.xaml.cs:

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();

        Routing.RegisterRoute(nameof(InfoPage), typeof(InfoPage));
    }
}

The ViewModel for InfoPage is:

public partial class InfoPageViewModel : ObservableObject
    {
        public InfoPageViewModel()
        {
            
        }
    }

My AppShell.xaml is below, the page im navigating to is not going to part of the main nav of the app and will only be displayed at certain times:

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="People.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:views="clr-namespace:People.Views"
    Shell.FlyoutBehavior="Flyout">
    <ShellContent Title="Home"
            ContentTemplate="{DataTemplate views:MainPage}" 
            Icon="{OnPlatform Android='icon1.png', iOS='icon1.png', MacCatalyst='icon1.png'}"/>
</Shell>

The error is shown here too (there is no parameter ‘index’ that im using so must be part of the framework as it says)
enter image description here

What am I doing wrong here for this error to come up? Any help really appreciated

2

Answers


  1. Chosen as BEST ANSWER

    Looks like I solved it. Thanks to those who answered/commented to help.

    Basically a rookie mistake in the end though. It didnt help that the Break Mode screen came up, but hope it helps someone else in future

    My [RelayCommand] executes a method which does something, then moves the user to a new page.

    What I didnt realise was after the user is moved to the new page, any remaining code still gets executed on the previous page, I assumed that any processing of further code on the previous page would stop as soon as the user was navigated to the new one. So, all I needed to do was put a return; after the Shell.Current.GoToAsync() and it solved the issue


  2. I think you are using the Visible property inside the ShellPage? I’m right? Please add shell page here
    I think you have similar problems Github

    [Update answer]

    Try change your Shell

    <?xml version="1.0" encoding="UTF-8" ?> <Shell
         x:Class="People.AppShell"
         xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:views="clr-namespace:People.Views"
         Shell.FlyoutBehavior="Disabled">
       
          <TabBar>
             <Tab x:Name="Home">
                 <ShellContent ContentTemplate="{DataTemplate views:MainPage}" Route="MainPage"/>
             </Tab>       
         </TabBar> </Shell>
    
    
    public partial class AppShell : Shell
    {
        public AppShell()
        {
            InitializeComponent();
    
            Routing.RegisterRoute(nameof(InfoPage), typeof(InfoPage));
            this.Home.Icon = "icon1.png";
        }
    }
    

    Method update too

    async Task GoToInfoPage()
    {
      await Shell.Current.GoToAsync($"//{nameof(MainPage)}/{nameof(InfoPage)}");
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search