Error in navigation: Shell.Current is null. MainPage is null or not an instance of AppShell

Samuel Pimenta 45 Reputation points
2024-01-16T15:45:40.55+00:00

hi, When I click on the buttons this error appears in the console: Error in navigation: Shell.Current is null. MainPage is null or not an instance of AppShell.

  • Macos
  • visual studio for mac
  • .net8
  • .net maui

AppShell.xaml.cs

using AtrianBakers.Pages;

namespace AtrianBakers
{

    public partial class AppShell : Shell
    {
        public AppShell()
        {
            Routing.RegisterRoute("DashboardPage", typeof(DashboardPage));
            Routing.RegisterRoute("UiComponentsPage", typeof(UiComponentsPage));
            
           
        }
    }
}

AppSell.xaml

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="AtrianBakers.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:AtrianBakers.Pages"
    Shell.FlyoutBehavior="Disabled"
    Shell.BackgroundColor="{StaticResource Primary}"
    Shell.TitleColor="{StaticResource White}"
    Title="AtrianBakers">

    <ShellContent Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" />
    <ShellContent Route="UiComponentsPage" ContentTemplate="{DataTemplate local:UiComponentsPage}" />


    <TabBar x:Name="MainTabBar">
        <Tab Icon="tabbar_ic_dashboard.png" Title="Dashboard">
            <ShellContent ContentTemplate="{DataTemplate local:DashboardPage}" Route="DashboardPage" />
        </Tab>

        <Tab Icon="tabbar_ic_cartera.png" Title="Cartera">
            <ShellContent ContentTemplate="{DataTemplate local:CarteraPage}" Route="CarteraPage"/>
        </Tab>

        <Tab Icon="tabbar_ic_rutas.png" Title="Rutas">
            <ShellContent ContentTemplate="{DataTemplate local:RutasPage}" Route="RutasPage"/>
        </Tab>

        <Tab Icon="tabbar_ic_clientes.png" Title="Clientes">
            <ShellContent ContentTemplate="{DataTemplate local:ClientesPage}" Route="ClientesPage"/>
        </Tab>

        <Tab Icon="tabbar_ic_estadisticas.png" Title="Estadísticas">
            <ShellContent ContentTemplate="{DataTemplate local:EstadisticasPage}" Route="EstadisticasPage"/>
        </Tab>
    </TabBar>

    

</Shell>

App.xaml.cs

using AtrianBakers.Pages;

namespace AtrianBakers;

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        MainPage = new LoginPage();

    }
}

LoginPage.xaml.cs

using System;
using Microsoft.Maui.Controls;

namespace AtrianBakers.Pages;

public partial class LoginPage : ContentPage
{
    public LoginPage()
    {
        InitializeComponent();
    }

    private async void OnDashboard_Clicked(object sender, EventArgs e)
    {
        try
        {
            if (Shell.Current != null)
            {
                await Shell.Current.GoToAsync("//DashboardPage");
            }
            else
            {
                // Manejo de error si Shell.Current es nulo
                Console.WriteLine("Error en la navegación: Shell.Current es nulo");
            }
        }
        catch (Exception ex)
        {
            // Manejo de error si se produce una excepción durante la navegación
            Console.WriteLine($"Error en la navegación: {ex.Message}");
        }
    }


    private async void OnUiComponentsClicked(object sender, EventArgs e)
    {
        var mainPage = Application.Current.MainPage;
        if (mainPage != null && mainPage is AppShell shell)
        {
            await Shell.Current.GoToAsync("///UiComponentsPage");
        }
        else
        {
            // Manejo del caso en el que MainPage es nulo
            Console.WriteLine("MainPage es nulo o no es una instancia de AppShell");
        }
    }
}

LoginPage.xaml

<Componentes:ButtonPrimary 
                        Grid.Row="3"
                        Margin="0,24,0,0"
                        x:Name="ButtonPrimary"
                        Clicked="OnDashboardClicked"
                        Text="Iniciar Sesión"/>
<Label                 
Grid.Row="4"                 
Text="UI Componetes"                 
FontSize="Body"                 
TextColor="Black"                 
Margin="0,32,0,0"                 
x:Name="BtnUiComponents"                 
HorizontalOptions="Center">                      <Label.GestureRecognizers>                         
<TapGestureRecognizer Tapped="OnUiComponentsClicked"/>                     </Label.GestureRecognizers>             
</Label>

Can you help me find the error? Captura de pantalla 2024-01-16 a las 16.45.15

Developer technologies | .NET | .NET MAUI
{count} vote

Accepted answer
  1. Anonymous
    2024-01-17T07:38:36.3966667+00:00

    Please open your App.xaml.cs, change your MainPage from new LoginPage(); to new AppShell();, Because your MainPage is not AppShell, when you execute navigated operation by Shell.Current.GoToAsync, you will get null by Shell.Current.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

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