0

I'm creating an app in Maui and I'm getting a null reference error when I try to return to the home page of my app, notice data in my database

command to return

 private async void BtnBack_Clicked(object sender, EventArgs e)
    {
       App.Current.MainPage = new NavigationPage(new TabbedPageMenu());
    }

Edit

Here are some images to illustrate the idea

enter image description here

enter image description here

enter image description here

On the first two pages I use await Navigation.PushModalAsync(new NamePage()); In the last image I use App.Current.MainPage = new NavigationPage(new TabbedPageMenu()); as if to restart the app, in xmarian it worked, but in maui it didn't

Edit add the source LoginPage

private async void BtnLogin_Clicked(object sender, EventArgs e)
{
    App.Current.MainPage = new NavigationPage(newTabbedPageMenu());
}

TabbedPageMenu

<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="GymAPP.View.TabbedPageMenu"       
         xmlns:pages="clr-namespace:GymAPP.View"
         xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls"            
         android:TabbedPage.ToolbarPlacement="Bottom"
         >
<TabBar>
    <Tab Title="Home" Icon="home.png">
        <ShellContent ContentTemplate="{DataTemplate pages:HomePage}"/>
    </Tab>
    <Tab Title="Treinos" Icon="prod.png">
        <ShellContent ContentTemplate="{DataTemplate pages:TreinosPage}"/>
    </Tab>
    <Tab Title="Configuração" Icon="gear.png">
        <ShellContent ContentTemplate="{DataTemplate pages:ConfigPage}"/>
    </Tab>
</TabBar>

TreinosPage

  private void CvParcelamento_SelectionChanged(object sender, 
  TappedEventArgs e)
 {
  var btn = (Frame)sender;
  var item = (Parcelamento)btn.BindingContext;
  Preferences.Set("Parcelamento", item.ParcelamentoId.ToString());
  if (item == null) return;
  Navigation.PushModalAsync(new ExeTreinoPage());
 }

ExeTreinoPage

private async void BtnFinalizar_Clicked(object sender, EventArgs e)
{
 await Navigation.PushModalAsync(new ParabensPage());
}

enter image description here

I added the source structure at the end

Edit I had to use PushAsync, PushModalAsync, this command to return the root page didn't work

to open

var page = Navigation.NavigationStack.LastOrDefault();
await Navigation.PushAsync(new ParabensPage());
Navigation.RemovePage(page);

to close

var page = Navigation.NavigationStack.LastOrDefault(); await Navigation. PopToRootAsync(); Navigation.RemovePage(page);

4
  • you are creating a new instance of your pages, not navigating back to the page that already exists. Have you read the docs on navigation? Commented Apr 4, 2024 at 22:00
  • Hi Jason, my idea is if I were restarting the app and going back to the main page, push and pop don't fit, as the client would have the possibility to go back to the previous page Commented Apr 5, 2024 at 18:11
  • I observed that you use NavigationPage, in MAUI NavigationPage structure, you can use PopToRootAsync, this method pops all but the root page off the navigation stack:private async void BtnBack_Clicked(object sender, EventArgs e){ Navigation.PopToRootAsync();} Commented Apr 8, 2024 at 3:26
  • Hello @Sean He-MSFT , I tried this Option and the screen does nothing, it accesses this command and does not close the screen, I will edit the question and add the source Commented Apr 9, 2024 at 12:17

1 Answer 1

1

I found that your TabbedPageMenu is using the shell structure. In MAUI, the Shell structure cannot be used with TabbedPage and NavigationPage, so I suggest you refer to the following code to change the TabbedPageMenu content:

<TabbedPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     x:Class="GymAPP.View.TabbedPageMenu"       
     xmlns:pages="clr-namespace:GymAPP.View"
     xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls"            
     android:TabbedPage.ToolbarPlacement="Bottom">
    <Pages:HomePage Title="Home" IconImageSource="home.png"/>
    <Pages:TreinosPage Title="Treinos" IconImageSource="prod.png"/>
    <Pages:ConfigPage Title="Configuração" Icon="gear.png"/>
</TabbedPage>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.