Search code examples
xamlxamarinxamarin.formsxamarin.android

XAMARIN prevent page loading after tap on ShellContent (DataTemplate)


I made some progress on my android app. Currently I struggle to find solution to this problem

(Code reference in my older question Creating nested navigation in Xamarin shell)

As selector does his job, this DataTemplate will be selected as NavigationHeaderTemplate

protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
    if (item is ShellGroupItem && list.Contains(((ShellGroupItem)item).Title))
    {
        // Make sure a header item is not clickable.
        ((ShellGroupItem)item).IsEnabled = false;
        return NavigationHeaderTemplate;
    }
    else
        return NavigationItemTemplate;
}

So far, Selector only once check for each NavigationHeaderTemplate (as it should be), and for NavigationItemTemplate it checks every time visibility changes (as it should maybe be, its not quite optimal but it works properly)

After first tap on NavigationHeaderTemplate it only changes visibility of its items (thats correct) but after another tap it changes visibility and goes to AboutPage. As you see in selection phase NavigationHeaderTemplate should be set as disabled

((ShellGroupItem)item).IsEnabled = false;

But somehow at second tap it goes to AboutPage.

<ShellContent Title="Header"  ContentTemplate="{DataTemplate local:AboutPage}"/>

My question is: How to prevent Header to load pages, it should only change visibility of items (items are used for links)

This doesn't work

<ShellContent Title="Header"  ContentTemplate="{DataTemplate local:AboutPage}" IsEnabled="False"/>

Solution

  • After first tap on NavigationHeaderTemplate it only changes visibility of its items (thats correct) but after another tap it changes visibility and goes to AboutPage.

    You can try to change FlyoutItemTemplateSelector by following code:

     public class FlyoutItemTemplateSelector : DataTemplateSelector
    {
        public DataTemplate NavigationHeaderTemplate { get; set; }
        public DataTemplate NavigationItemTemplate { get; set; }
    
        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            if (item is ShellGroupItem && ((ShellGroupItem)item).Title == "Header")
            {
                // Make sure a header item is not clickable.
                ((ShellGroupItem)item).IsEnabled = false;
                return NavigationHeaderTemplate;
            }
            else
                return NavigationItemTemplate;
        }
    }
    

    I tap the header one or more time, it just change visibility of its items.