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.
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;
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.