Navigation Stack empty when navigating from TabbedPage (Back button missing)

huangapple go评论68阅读模式
英文:

Navigation Stack empty when navigating from TabbedPage (Back button missing)

问题

I am having an issue where I have no NavigationBar on my page and this only happened after I implemented Tabbed pages in .NET MAUI with the Prism MVVM framework. Any help is much appreciated.

Code to navigate to Tabbed page:

await _navigationService.NavigateAsync($"{nameof(HomePage)}?{KnownNavigationParameters.SelectedTab}={nameof(DashboardPage)}");

TabbedPage:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage
    x:Class="TestApp.Views.HomePage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls"
    xmlns:local="clr-namespace:TestApp.Views;assembly=TestApp"
    android:TabbedPage.ToolbarPlacement="Bottom"
    AutomationId="home_page">

    <local:DashboardPage x:Name="dashboardPage" Title="Home" IconImageSource="icon_home_o" />
    <local:CommentListPage x:Name="commentListPage" Title="Comments" IconImageSource="icon_comment_o" />
    <local:MorePage x:Name="morePage" Title="More" IconImageSource="icon_ellipsis_o" />
</TabbedPage>

Dashboard page view model (code navigating to another new view):

//When the navigation happens, this page has NO navigation bar and this NO BACK BUTTON which is a problem
await _navigationService.NavigateAsync(nameof(TestListPage))

TestListPage initialization:

public partial class TestListPage : ContentPage
{
    public TestListPage()
    {
        InitializeComponent();
        //nav stack has a count of 0
        var test = Navigation.NavigationStack;
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        //nav stack has a count of 0
        var test = Navigation.NavigationStack;
    }
}
英文:

I am having an issue where I have no NavigationBar on my page and this only happened after I implemented Tabbed pages in .NET MAUI with the Prism MVVM framework. Any help is much appreciated.

All the pages being shown and their view models have been registered as services.

Code to navigate to Tabbed page:

await _navigationService.NavigateAsync($"{nameof(HomePage)}?{KnownNavigationParameters.SelectedTab}={nameof(DashboardPage)}");

TabbedPage:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage
    x:Class="TestApp.Views.HomePage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls"
    xmlns:local="clr-namespace:TestApp.Views;assembly=TestApp"
    android:TabbedPage.ToolbarPlacement="Bottom"
    AutomationId="home_page">

    <local:DashboardPage x:Name="dashboardPage" Title="Home" IconImageSource="icon_home_o" />
    <local:CommentListPage x:Name="commentListPage" Title="Comments" IconImageSource="icon_comment_o" />
    <local:MorePage x:Name="morePage" Title="More" IconImageSource="icon_ellipsis_o" />
</TabbedPage>

Dashboard page view model (code navigating to another new view):

//When the navigation happens, this page has NO navigation bar and this NO BACK BUTTON which is a problem
await _navigationService.NavigateAsync(nameof(TestListPage))

TestListPage initialisation:

public partial class TestListPage : ContentPage
{
    public TestListPage()
    {
        InitializeComponent();
        //nav stack has a count of 0
        var test = Navigation.NavigationStack;
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        //nav stack has a count of 0
        var test = Navigation.NavigationStack;
    }
}

答案1

得分: 1

I am having an issue where I have no NavigationBar on my page and this only happened after I implemented Tabbed pages in .NET MAUI with the Prism MVVM framework.

我在我的页面上没有NavigationBar,这只发生在我在.NET MAUI中使用Prism MVVM框架实现选项卡页面之后。

I tested the code, and as you described that. You can report it to GitHub. In addition, you have to use Prism MVVM framework? Can CommunityToolkit.Mvvm achieve your needs?

我测试了代码,如您所描述。您可以报告给GitHub。此外,您必须使用Prism MVVM框架吗?CommunityToolkit.Mvvm能够满足您的需求吗?

About Navigation, there're some tips for it:

关于导航,有一些提示:

While a NavigationPage can be placed in a TabbedPage, it's not recommended to place a TabbedPage into a NavigationPage.

虽然可以将NavigationPage放置在TabbedPage中,但不建议将TabbedPage放置在NavigationPage中。

TabbedPage is incompatible with .NET MAUI Shell apps, and an exception will be thrown if you attempt to use TabbedPage in a Shell app.

TabbedPage与.NET MAUI Shell应用程序不兼容,在Shell应用程序中尝试使用TabbedPage会引发异常。

For more details you can refer to the official doc: TabbedPage.

有关更多详细信息,您可以参考官方文档:TabbedPage

Update:

更新:

According to what steve says:

根据Steve的说法:

A workaround I did was instead of putting NavigateAsync(nameof(TestListPage)). I put NavigateAsync($"/{nameof(NavigationPage)}/{nameof(HomePage)}/{nameof(TestListPage)}").

我做的一个解决方法是,不要放置NavigateAsync(nameof(TestListPage)),而是放置NavigateAsync($"/{nameof(NavigationPage)}/{nameof(HomePage)}/{nameof(TestListPage)}")

It can solve the problem.

这可以解决问题。

英文:

> I am having an issue where I have no NavigationBar on my page and this only happened after I implemented Tabbed pages in .NET MAUI with the Prism MVVM framework.

I tested the code, and as you described that. You can report it to GitHub. In addition, you have to use Prism MVVM framework? Can CommunityToolkit.Mvvm achieve your needs?

About Navigation, there're some tips for it:

> While a NavigationPage can be placed in a TabbedPage, it's not recommended to place a TabbedPage into a NavigationPage.
>

> TabbedPage is incompatible with .NET MAUI Shell apps, and an exception will be thrown if you attempt to use TabbedPage in a Shell app.

For more details you can refer to the official doc: TabbedPage.

Update:

According to what steve says:

> A workaround I did was instead of putting NavigateAsync(nameof(TestListPage)). I put NavigateAsync($"/{nameof(NavigationPage)}/{nameof(HomePage)}/{nameof(TestListPage)}").

It can solve the problem.

huangapple
  • 本文由 发表于 2023年4月17日 21:46:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定