我对 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 });
但这是结果
我错过了什么吗?对于这种情况,这是正确的方法吗?