Customize Shell template breaks navigation

Rainer 171 Reputation points

I'm trying to customize my Shell Flyout and I'm running into a few problems.

Problem #1 — Can't style the content's vertical alignment

I've managed to style my items and menu items using Shell.ItemTemplate and Shell.MenuItemTemplate, but when I do this the list of items is centered vertically and I would like to have it at the top vertically. As far as I can tell there is no property to adjust this.

Problem #2 — Setting Shell.FlyoutContent breaks navigation

Because of problem #1, I decided to fully customize the shell using Shell.FlyoutContent with a CollectionView and appropriate templates in it. This works and the content looks like how I want it to look. But, when I do this, navigation stops working, that is, when I click on an item nothing happens at all.

Problem #3 — Using a TapGestureRecognizer doesn't work for the first ShellContent

To solve problem #2 I decided to attache a TapGestureRecognizer to the ShellContent. This almost works, but doesn't work for the first item in the collection. I've tried reordering the items and it's always just the first item that doesn't work. As you can see in the code below, I bind to NavigateTo. In my code if I put a break point in that it does not get hit when I tap on the first item, but does work for every other item.

Actually, it's even slightly weirder. Tapping on the first item never works. Tapping on the second item only sometimes works. Sometimes I need to tap it multiple times for it to work. For all of the other items it always works on the first tap.

My relevant code is:

		BackgroundColor="{AppThemeBinding Light={StaticResource LightColorSemiTransparent},
											Dark={StaticResource DarkColorSemiTransparent}}"
		BindingContext="{x:Reference This}"
		ItemsSource="{Binding FlyoutItems}">
			<DataTemplate x:DataType="ShellContent">
					Style="{DynamicResource StackHorizontal}"
							Command="{Binding NavigateTo, Source={x:Reference This}}"
							CommandParameter="{Binding .}"
							NumberOfTapsRequired="1" />
						Source="{Binding FlyoutIcon}"
						Style="{DynamicResource ImageMenuIcon}"
						VerticalOptions="Center" />
						Style="{DynamicResource LabelBody}"
						Text="{Binding Title}"
						VerticalOptions="Center" />

Bonus question:

I'm trying to understand the difference between Shell.FlyoutContent and Shell.FlyoutContentTemplate. Is it true that there are 100% equivalent?



Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,558 questions
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,813 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 67,631 Reputation points Microsoft Vendor


    You can achieve this NavigateTo in the AppShell backgroud code. Then we can get the current selected ShellItem.then traverse IList<ShellSection>, if get the same title, change the current CurrentItem by  Shell.Current.CurrentItem.CurrentItem= shellSection; and close this Flyout page with Shell.Current.FlyoutIsPresented = false;

    public partial class AppShell : Shell
        public ICommand NavigateTo { get; set; }
        public AppShell()
            Routing.RegisterRoute("CatsPage", typeof(CatsPage));
            Routing.RegisterRoute("DogsPage", typeof(DogsPage));
            Routing.RegisterRoute("MainPage", typeof(MainPage));
            Routing.RegisterRoute("MonkeysPage", typeof(MonkeysPage));
            NavigateTo = new Command((e) =>{
                BaseShellItem res = e as BaseShellItem;
                IList<ShellSection> shellSections= Shell.Current.CurrentItem.Items;
                foreach (ShellSection shellSection in shellSections)
                        Shell.Current.CurrentItem.CurrentItem= shellSection;
                        Shell.Current.FlyoutIsPresented = false;
                    BindingContext = this;

    Please do not forget to add Title for your <Tab> or <ShellContent> like following code.

    <FlyoutItem FlyoutDisplayOptions="AsMultipleItems">
    <Tab Title="Domestic"
                <ShellContent Title="Cats"
                              ContentTemplate="{DataTemplate local:CatsPage}" />
                <ShellContent Title="Dogs"
                              ContentTemplate="{DataTemplate local:DogsPage}" />
            <ShellContent Title="Monkeys"
                          ContentTemplate="{DataTemplate local:MonkeysPage}" />

    As note:Avoid duplicate questions in one thread

    Best Regards,

    Leon Lu

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

1 additional answer

Sort by: Most helpful
  1. Rainer 171 Reputation points

    Ok, I figured out problem #3.

    My header was the code below. With this the image actually invisibly extends below the bottom of the header. It seems when I was tapping on the first item in my list the image was receiving that tap, so it didn't get handled my my item. If I add Margin="0" to the Grid then this fixes that problem. That seems like a kind of random hack that is not very understandable, but at least it works. I would consider this a bug in MAUI.

    BTW, if you're curious, the reason I have the -30 margins it because my logo image has margins in the image that I don't want shown in this case. Of course I could just have multiple versions of my logo image (with and without those margins) but I felt reusing this image was more efficient.

    		<Grid HeightRequest="200">
    				Source="app_logo.png" />
    0 comments No comments