1

I was trying to make a tabbed view with a breadcrumb navigation, unfortunately the titles are not working the way they're supposed to.

When it seems like the NavigationStack is empty so the previous site title gets overwritten with the current title?

Top PagePage after clicking the plus

AppShell.xaml

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="WorkTime2.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:WorkTime2"
    xmlns:view="clr-namespace:WorkTime2.View"
    Shell.FlyoutBehavior="Disabled">
    <TabBar>
        <Tab Title="Home" Icon="icon_shell_dashboard.png" Route="Home">
            <ShellContent ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" />
        </Tab>
        <Tab Title="Employers" Icon="icon_shell_employer.png" Route="Employers">
            <ShellContent ContentTemplate="{DataTemplate view:EmployerPage}" Route="EmployerPage" />
        </Tab>
        <Tab Title="Settings" Icon="icon_shell_settings.png" Route="Settings">
            <ShellContent ContentTemplate="{DataTemplate view:SettingsPage}" Route="SettingsPage" />
        </Tab>
    </TabBar>
</Shell>

AppShell.xaml.cs

using WorkTime2.View;

namespace WorkTime2;

public partial class AppShell : Shell
{
    public AppShell()
    {
        Routing.RegisterRoute("EmployerPage/AddEmployerPage", typeof(AddEmployerPage));
        InitializeComponent();
    }
}

EmployerPage.xaml

<?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:controller="clr-namespace:WorkTime2.Controller"
             x:Class="WorkTime2.View.EmployerPage"
             Title="Employer">
    <ContentPage.BindingContext>
        <controller:EmployerController />
    </ContentPage.BindingContext>
    <ContentPage.ToolbarItems>
        <ToolbarItem IconImageSource="plus_icon.png" Command="{Binding CreateEmployer}" />
    </ContentPage.ToolbarItems>
    <ContentPage.Content>
    </ContentPage.Content>
</ContentPage>

AddEmployerPage.xaml

<?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:controller="clr-namespace:WorkTime2.Controller"
             x:Class="WorkTime2.View.AddEmployerPage"
             Title="Add Employer">
    <ContentPage.BindingContext>
        <controller:AddEmployerController />
    </ContentPage.BindingContext>
    <ContentPage.Content>
        
    </ContentPage.Content>
</ContentPage>

EmployerController.cs

using System.Windows.Input;
using WorkTime2.Core;

namespace WorkTime2.Controller;

public class EmployerController : ControllerBase
{
    public EmployerController()
    {
        CreateEmployer = new Command(execute: () =>
        {
            Shell.Current.GoToAsync("AddEmployerPage");
            Console.WriteLine("Add clicked!");
            
        }, canExecute: () => true);
    }
    
    public ICommand CreateEmployer { get; set; }
}

Does anyone have an idea how to fix it? Thanks!

I have tried different routing registrations, without the previous one, GoToAsync with / and //

EDIT: AddEmployerController.cs

using WorkTime3.Core;
using WorkTime3.View;

namespace WorkTime3.Controller;

public class AddEmployerController : ControllerBase
{
    private WorkTime3Database _db;
    public AddEmployerController()
    {
        _db = new WorkTime3Database();
        TestCommand = new Command(
            execute: () =>
            {
                Shell.Current.GoToAsync(nameof(AddTimePage)); 
            }, canExecute: () => true);
    }
    private string _title = "Add Employer";
    public string Title
    {
        get => _title;
        set => SetProperty(ref _title, value);
    }
    
    public ICommand TestCommand { get; set; }
}
Niko Klemm
  • 69
  • 5
  • Can you please provide AddEmployerController.cs?Thanks. – Hongxin Sui-MSFT Dec 13 '22 at 08:32
  • I did, thanks for looking into it! – Niko Klemm Dec 13 '22 at 17:10
  • There's an info box in the official documentation about the navigation stack not being created for routes that are part of the Shell visual hierarchy: https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation?view=net-maui-7.0#perform-navigation – Julian Dec 13 '22 at 18:23

1 Answers1

0

For a TabBar navigation you could just use ShellContent. It handles most basic functionality like registering the page.

<?xml version="1.0" encoding="UTF-8"?>
    
<Shell
    x:Class="WorkTime2.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:WorkTime2"
    xmlns:view="clr-namespace:WorkTime2.View"
    Shell.FlyoutBehavior="Disabled">
    <TabBar>
        <ShellContent Title="Home" Icon="icon_shell_dashboard.png" 
                      ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" />
        <ShellContent Title="Employers" Icon="icon_shell_employer.png" 
                      ContentTemplate="{DataTemplate view:EmployerPage}" Route="EmployerPage" />
        <ShellContent Title="Settings" Icon="icon_shell_settings.png" 
                      ContentTemplate="{DataTemplate view:SettingsPage}" Route="SettingsPage" />
    </TabBar>
</Shell>
Teddy
  • 452
  • 1
  • 12
  • 1
    A ```ShellContent``` within another one seems off and throws an error: ```System.InvalidOperationException: ShellContent Content should be of type Page. Title Home, Route Home``` – Niko Klemm Dec 11 '22 at 01:41
  • ohh, sorry copy past mistake – Teddy Dec 11 '22 at 01:44
  • Ok, i have fixed the sample code. – Teddy Dec 11 '22 at 01:47
  • No error, but doesn't make any difference either, unfortunately... :( same issue: breadcrumbs still getting overwritten by current ContentPage title... – Niko Klemm Dec 11 '22 at 01:59
  • I don't know if it helps but i also found this [NavigationPage.BackButtonTitleProperty Field](https://learn.microsoft.com/en-us/dotnet/api/microsoft.maui.controls.navigationpage.backbuttontitleproperty?view=net-maui-7.0) – Teddy Dec 11 '22 at 06:44