C#-UWP模板10创建动态汉堡包菜单

我对UWP的东西很了解.我正在尝试创建动态汉堡包菜单.
我能够创建PrimaryButtons元素,并按预期在XAML中进行绑定:

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;

但是我想有一个更干净的解决方案,所以我尝试扩展HamburgerButtonInfo类:

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);
        }
    }
}

放在一起,我期望得到相同的结果:

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

但这是结果

C#-UWP模板10创建动态汉堡包菜单

我想念什么吗?对于这种情况,这是正确的方法吗?

解决方法:

能做到吗绝对.

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

看起来像这样(我在“搜索”示例中尝试过).

C#-UWP模板10创建动态汉堡包菜单

它更加冗长,因为XAML语法非常紧凑,但是您可以根据需要在代码隐藏中进行操作.如果可以的话,您可能只想更改现有按钮的可见性.

祝你好运!

上一篇:c#-将cookie设置为WebView控件


下一篇:c#-在Windows 10中以指定的访问模式运行应用程序时无法显示MessageBox