这可能是一个微不足道的初学者问题,我已经为Windows和Silverlight应用程序找到了相当多的相关信息,但没有什么可以直接帮助我.我正在用C#和XAML编写Windows Phone 8.1 / WinRT应用程序,我想以编程方式修改在XAML中创建...

这可能是一个微不足道的初学者问题,我已经为Windows和Silverlight应用程序找到了相当多的相关信息,但没有什么可以直接帮助我.我正在用C#和XAML编写Windows Phone 8.1 / WinRT应用程序,我想以编程方式修改在XAML中创建的应用程序栏.例如,我想在调试版本中包含一个按钮,使用后面的代码中的预处理程序指令.
在下面的XAML代码中,我正在创建一个带有两个按钮的BottomAppBar.如何创建第二个按钮(AppBarAddSampleItemsButton),包括代码背后的所有属性?
<prism:VisualStateAwarePage.BottomAppBar>
<CommandBar >
<CommandBar.PrimaryCommands>
<AppBarButton x:Uid="AppBarNewItemButton"
Label="New item"
Icon="Add" Command="{Binding GoToAddItemPageCommand}" />
</CommandBar.PrimaryCommands>
<CommandBar.SecondaryCommands>
<AppBarButton x:Uid="AppBarAddSampleItemsButton"
Label="Add sample items"
Command="{Binding GoToAddSampleItemssPageCommand}" />
</CommandBar.SecondaryCommands>
</CommandBar>
</prism:VisualStateAwarePage.BottomAppBar>
解决方法:
下面是代码中创建AppBarButton的示例代码,并将其添加到当前页面的BottomAppBar:
private void AddButtonToAppBar()
{
AppBarButton buttonToAdd = new AppBarButton { Label = "Label", Icon = new SymbolIcon(Symbol.Help) };
buttonToAdd.Click += async (sender, e) => await new MessageDialog("Button clicked").ShowAsync();
// add button to Page's BottoAppBar
(BottomAppBar as CommandBar).PrimaryCommands.Add(buttonToAdd);
}
编辑 – 至于绑定(再次从我的头顶,所以你必须检查这个)这应该可行:
Binding myBind = new Binding();
myBind.Path = new PropertyPath("GoToAddSampleItemssPageCommand");
myBind.Source = DataContext;
buttonToAdd.SetBinding(AppBarButton.CommandProperty, myBind);
更多关于DataBinding at MSDN.
本文标题为:c# – 从Code Behind操作Windows Phone 8.1 XAML中的应用程序栏


基础教程推荐
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- Unity shader实现高斯模糊效果 2023-01-16
- C# 解析XML和反序列化的示例 2023-04-14
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- 实例详解C#实现http不同方法的请求 2022-12-26
- C#中的Linq to JSON操作详解 2023-06-08
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#中 Json 序列化去掉null值的方法 2022-11-18