Forum Xamarin.Forms

How can one disable Menu Item in App Shell?

toyinstoyins USMember ✭✭✭

I want to be to disable a menu Item or shell item in an Hamburger Menu in App Shell based on a condition upon logging in to the app.
Is there a way to do that in Xamarin.Forms?

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    I want to be to disable a menu Item or shell item in App Shell based on a condition upon logging in to the app.

    The Xamarin.Forms MessagingCenter class implements the publish-subscribe pattern, allowing message-based communication between components that are inconvenient to link by object and type references.

    Try to update the UI of Shell in the login page using MessagingCenter.

    Check the code:

    Login event

    private void Button_Clicked(object sender, EventArgs e)
    {
        ...
        MessagingCenter.Send<object>(this, "hi");
    }
    

    AppShell.xaml.cs

    public AppShell()
    {
        InitializeComponent();
    
        MessagingCenter.Subscribe<object>(this, "hi", (sender) => {
            item_2.IsEnabled = false;
        });
    }
    

    AppShell.xaml

    <Shell xmlns="http://xamarin.com/schemas/2014/forms" 
           ...
           x:Class="Shell_1.AppShell">
        ...
    
        <FlyoutItem x:Name="item_1" Title="item_1">
            ...
        </FlyoutItem>
        <FlyoutItem x:Name="item_2" Title="item_2">
            ...
        </FlyoutItem>
    
    </Shell>
    
  • toyinstoyins USMember ✭✭✭

    Hi Jarvan, item_1 or item_2 are not detected in the AppShell constructor. Is there anything that I'm missing?

  • JarvanJarvan Member, Xamarin Team Xamurai

    item_1 or item_2 are not detected in the AppShell constructor. Is there anything that I'm missing?

    Could you post the related code? It'll help to reproduce the issue and get the cause.

  • toyinstoyins USMember ✭✭✭

    I used one of your examples online. Is it possible to change the Selected FlyoutMenuItem background color, programmatically?

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited May 11

    I try to use MenuItemTemplate to change the background color but it doesn't. The background of ShellContent will be changed when it's selected, try to use ShellContent instead.

    <ShellContent Title="About"
                Icon="info.png"
                ContentTemplate="{DataTemplate views:AboutPage}" />
    
Sign In or Register to comment.