by Santhakumar Munuswamy
Posted on 24 六月 2016
超人
在本文中,我们将讨论如何理解UWP中的应用栏和命令栏。 我们可以通过UWP中的示例应用程序轻松实现App Bar和Command Bar。大家都知道AppBar,Command Bar是Windows运行时API中Windows.UI.Xaml.Controls包上的默认类。仅当您在Windows 8中开发通用Windows 8应用程序时,才可以使用AppBar。如果要在Windows 10中开发新应用程序,则强烈建议使用CommandBar控件而不是UWP中的AppBar。
普遍 Windows Platform series
背景
命令栏是一个UI组件及其轻量级控件,可用于复杂的内容(例如图像或文本块)以及简单的建议(例如AppBarButton,AppBarToggleButton和AppBarSeparator控件)。它可以与任何导航模式和主要三个属性(例如PrimaryCommands,SecondaryCommands和Content)一起使用。
怎么样 to implement AppBar and CommandBar and run UWP App
We are going to discuss how to implement AppBar and CommadBar with a sample apps on UWP and show the demo in multiple device. We will see the step by step guidelines for the 超人 AppBar and CommandBar 应用程式 creation here.
步 1:
打开 Visual Studio 2015. Go to file menu, point to new and then click new project. New Project window 将 open,
您 can select a installed template like “ Universal” under the Windows on Visual C# Template, and then
选择 a Blank App (Universal Windows) and type Project Name UWPCommandBar. Choose the project location
路径 and then click on the OK button.

现在, you can see the 超人CommandBar 项目 structure as in the following screen shot.

步 2:
As you all know, we have discussed project folder structure and files on my previous article at earlier.
步 3:
As you all know, we have toolbox available on visual studio in that which is available on CommandBar
控制 in the below screen shots

现在, you can see the auto generated code as below
<Page.BottomAppBar>
<CommandBar>
<CommandBar.Content>
<Grid/>
</CommandBar.Content>
<AppBarButton Icon="Accept" Label="应用程式barbutton"/>
<AppBarButton Icon="Cancel" Label="应用程式barbutton"/>
</CommandBar>
</Page.BottomAppBar>
首页.xaml
<Page
x:Class="超人CommandBar.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWPCommandBar"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.BottomAppBar>
<CommandBar ClosedDisplayMode="Compact">
<CommandBar.PrimaryCommands>
<AppBarButton 名称="home" Icon="首页" Label="首页" Click="应用程式Home_Click"/>
<AppBarButton 名称="admin" Icon="Admin" Label="Admin" Click="应用程式Admin_Click"/>
<AppBarButton 名称="contact" Icon="联系" Label="联系" Click="应用程式Contact_Click"/>
</CommandBar.PrimaryCommands>
<CommandBar.SecondaryCommands>
<AppBarButton 名称="setting" Icon="Setting" Label="Setting"/>
</CommandBar.SecondaryCommands>
</CommandBar>
</Page.BottomAppBar>
<Grid 背景="{ThemeResource 应用程式licationPageBackgroundThemeBrush}">
<StackPanel>
<TextBlock Text="首页" Padding="25 25 25 25"></TextBlock>
</StackPanel>
</Grid>
</Page>
首页.xaml.cs
using System;
using 视窗.UI.Xaml;
using 视窗.UI.Xaml.Controls;
// The 空白 Page item 模板 is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace 超人CommandBar
{
/// <summary>
/// An empty page 那 能够 be 用过的 on its own or navigated to 与in a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
这个.InitializeComponent();
}
private void 应用程式Home_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(MainPage));
}
private void 应用程式Admin_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(About));
}
private void 应用程式Contact_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(Contact));
}
}
}
步 4:
现在, if you can run the 超人CommandBar 应用程式s with the different devices, you can see how
an 应用 看起来, as shown below:
选择 a Debug and 移动 Emulator 10.0.10586.0 WVGA4 inch 512 MB option to run the apps
怎么样 to Customize the CommendBar in UWP
We can easily customize the UI/UX based on your requirements in UWP. I have used in
应用栏Separator for the UI looks pretty in the sample universal windows apps.
步 1:
<Page.TopAppBar>
<CommandBar ClosedDisplayMode="Minimal" 背景="#1FA2E1"></CommandBar>
</Page.TopAppBar>
<Page.BottomAppBar>
<CommandBar ClosedDisplayMode="Compact" 背景="#1FA2E1">
<CommandBar.PrimaryCommands>
<AppBarButton 名称="home" Icon="首页" Label="首页" Click="应用程式Home_Click"/>
<AppBarSeparator/>
<AppBarButton 名称="admin" Icon="Admin" Label="Admin" Click="应用程式Admin_Click"/>
<AppBarSeparator/>
<AppBarButton 名称="contact" Icon="联系" Label="联系" Click="应用程式Contact_Click"/>
<AppBarSeparator/>
</CommandBar.PrimaryCommands>
<CommandBar.SecondaryCommands>
<AppBarButton 名称="setting" Icon="Setting" Label="Setting"/>
</CommandBar.SecondaryCommands>
</CommandBar>
</Page.BottomAppBar>
第2步:
现在, if you can run the 超人CommandBar 应用程式s with the different devices, 您 can see how an 应用 看起来, as 显示n below:
选择 a Debug and 移动 Emulator 10.0.10586.0 WVGA4 inch 512 MB option to run the apps

选择 a 除错 和 Local Machine 选项 to 跑 的 应用

源代码
您 能够 download 的 source 码 to 这里
结论
I hope you understood the CommendBar, AppBar, Customize UI and run it on multiple devices. I have covered
所有 the required things. If you find anything that I missed in this article, please let me know. 请 share your
有价值 feedback or suggestions.