Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How does one disable a ShellContent in a ShellSection using Shell in Xamarin.Forms

toyinstoyins USMember ✭✭✭
    I have this in my Shell.xaml.

  <FlyoutItem Shell.TabBarIsVisible="False" Route="header" x:Name="FlyoutItem1"              FlyoutDisplayOptions="AsMultipleItems">
                    <ShellContent x:Name="BookItItem" Route="bookit" Title="BOOK IT" ContentTemplate="{DataTemplate views:HomePage}" />
<ShellSection x:Name="ExploreItem" Route="explore" Title="EXPLORE">
                    <ShellContent ContentTemplate="{DataTemplate subs:ExplorePage}" />
</ShellSection>
                    <ShellContent x:Name="ContactItem" Route="contact" Title="CONTACT" ContentTemplate="{DataTemplate login:ContactPage}" />
                </FlyoutItem>

                 <FlyoutItem Shell.TabBarIsVisible="False" Route="main" FlyoutDisplayOptions="AsMultipleItems">
                    <ShellContent x:Name="MyReservationsItem" Route="reservation" Title="MY RESERVATIONS" ContentTemplate="{DataTemplate            reservations:MyReservationsPage}" />
                    <ShellContent x:Name="MyGuestItem" Route="guest" Title="MY GUESTS" ContentTemplate="{DataTemplate guests:MyGuestsPage}" />
                    <ShellContent x:Name="OverViewItem" Route="account" Title="OVERVIEW"  Icon="" ContentTemplate="{DataTemplate settings:OverViewPage}" />
                    <ShellContent x:Name="PasswordSecurityItem" Route="password"  Title="PASSWORD &amp; SECURITY" ContentTemplate="{DataTemplate settings:PasswordSecurityPage}" />
                    <ShellContent x:Name="EmailPreferencesItem" Route="email" Title="EMAIL PREFERENCES" ContentTemplate="{DataTemplate settings:EmailPreferencesPage}"  />
                </FlyoutItem>

How do I disable the shellcontent "ExplorePage"?
I tried this ExploreItem.Items[0].IsEnabled = false;

It did not disable the flyoutMenuItem.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Currently, we don't have a direct api to dynamically hide or show flyout items.
    What we can do is to remove and add items when needed like:
    Firstly, try to name a field for your xaml ShellSection

    ShellSection ExploreSection;
    

    And then register a messaging center to adjust the tabs:

    public AppShell()
    {
        InitializeComponent();
        // ...
    
        ExploreSection = ExploreItem;
    
        MessagingCenter.Subscribe<object, bool>(this, "ChangeItems", (sender, isVisible) =>
        {
            if (isVisible)
            {
                if (!FlyoutItem1.Items.Contains(ExploreSection))
                {
                    FlyoutItem1.Items.Add(ExploreSection);
                }
            }
            else
            {
                if (FlyoutItem1.Items.Contains(ExploreSection))
                {
                    FlyoutItem1.Items.Remove(ExploreSection);
                }
            }
        });
    }
    

    Trigger this messaging center when needed:

    // Add
    MessagingCenter.Send<object, bool>(this, "ChangeItems", true);
    // Remove
    MessagingCenter.Send<object, bool>(this, "ChangeItems", false);
    
Sign In or Register to comment.