UWP Template 10 create a dynamic hamburgermenu

老子叫甜甜 提交于 2019-12-23 20:06:17

问题


I'm pretty noob with UWP stuff. I'm trying to create dynamic hamburger menu. I was able to create PrimaryButtons element, and binding it in XAML worked as espected:

var loginButton = new HamburgerButtonInfo();
loginButton.ClearHistory = true;
loginButton.PageParameter = "";
loginButton.PageType = typeof(Views.Login);
var stackPanel = new StackPanel { Orientation = Orientation.Horizontal };
stackPanel.Children.Add(new SymbolIcon { Symbol = Symbol.Contact, Width = 48, Height = 48 });
stackPanel.Children.Add(new TextBlock { Text = "Login", VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(12, 0, 0, 0) });
loginButton.Content = stackPanel;

But I'd like to have a cleaner solution, so I tried to extend HamburgerButtonInfo class:

class MenuItem : HamburgerButtonInfo
{
    private Symbol symbol;
    private String text;
    StackPanel stackpanel = new StackPanel { Orientation = Orientation.Horizontal };
    TextBox textbox = new TextBox { VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(12, 0, 0, 0) };
    SymbolIcon symbolicon = new SymbolIcon { Width = 48, Height = 48 };

    public MenuItem():base()
    {
        StackPanel.Children.Add(symbolicon);
        StackPanel.Children.Add(textbox);
        this.Content = StackPanel;
    }

    public String Text
    {
        get { return text; }
        set {
            textbox.Text = value;
            Set(ref text, value);
        }
    }

    public StackPanel StackPanel
    {
        get { return stackpanel; }
    }

    public Symbol Symbol
    {
        get { return symbol; }
        set {
            symbolicon.Symbol = value;
            Set(ref symbol, value);
        }
    }
}

Putting it all together, I expected to get the same result:

PrimaryButtons.Add(loginButton);
PrimaryButtons.Add(new MenuItem() { PageType=typeof(Views.Login), PageParameter="", ClearHistory=true, Text="Login", Symbol=Symbol.Contact });

But here's the result

Am I missing something? Is that the right approach for this scenario?


回答1:


Can it be done? Absolutely.

var stackPanel = new StackPanel
{
    Orientation = Orientation.Horizontal
};
stackPanel.Children.Add(new SymbolIcon
{
    Width = 48,
    Height = 48,
    Symbol = Symbol.UnSyncFolder
});
stackPanel.Children.Add(new TextBlock
{
    Margin = new Thickness(12, 0, 0, 0),
    VerticalAlignment = VerticalAlignment.Center,
    Text = "UnSync Folder"
});
var button = new HamburgerButtonInfo
{
    Content = stackPanel,
    ButtonType = HamburgerButtonInfo.ButtonTypes.Toggle,
    ClearHistory = false,
    PageType = typeof(Views.DetailPage)
};
MyHamburgerMenu.PrimaryButtons.Add(button);

Looks like this (I tried it in the Search sample).

It's more verbose because the XAML syntax is so compact, but you can do it in code-behind if you want to. You might just want to change visibility of an existing button if that is an option.

Best of luck!



来源:https://stackoverflow.com/questions/35231721/uwp-template-10-create-a-dynamic-hamburgermenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!