简体   繁体   中英

How to edit Hamburger Menu items list at runtime?

I'm using Template 10 in my UWP application. However I need to Enable/Disable the hamburger buttons according to my conditions. I'm setting IsFullScreen property to true initially because I want to show the hamburger menu after user logs in the application. Because the Shell page is loaded initially then at runtime if I create a new instance of it then the application is running on full screen and I'm unable to see the Menu. Thanks for the help in advance.

Accessing Shell page instance

You will first need to be able to access the Shell page instance. You can do this in two ways. If you know, there will always be a single instance, you can add a static property pointing to it like this:

public static Shell Instance { get; private set; }

And set the instance in the constructor:

public Shell()
{
    //InitializeComponent(), etc....
    Instance = this;
}

Now from anywhere, you can use Shell.Instance to access it. If you could theoretically have multiple windows, you can access the instance of current Shell using Windows.Current.Content . If you used the Template 10 Hamburger template, you would do:

var dialog = (ModalDialog)Window.Current.Content;
var shell = (Shell)dialog.Content;

Disabling/enabling

The items in HamburgerMenu control are of type HamburgerButtonInfo and have an IsEnabled property which you can use for enabling/disabling. If you add a x:Name="Menu" to the HamburgerMenu control, you can then write a enabling method like this:

public void SetMenuEnabled(bool enable)
{
   foreach (var primaryButton in Menu.PrimaryButtons)
   {
      primaryButton.IsEnabled = false;
   }
   foreach (var secondaryButton in Menu.SecondaryButtons)
   {
      secondaryButton.IsEnabled = false;
   }
}

You can put this method in the Shell page and call it via Shell.Instance.SetMenuEnabled(false) to disable and Shell.Instance.SetMenuEnabled(true) to enable all buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM