is it possible to return to the home in .net maui?

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);

Upvotes: 0

Views: 95

Answers (1)

Leon Lu
Leon Lu

Reputation: 9244

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>

Upvotes: 1

Related Questions