什么是AppBar?
AppBar可以用来显示导航,命令等,可以放在页面的顶部或底部,默认情况下AppBar是隐藏的,用户可以通过鼠标右键,Win+z或用手指从屏幕顶端或底端划过来显示或隐藏AppBar.
这是Windows应用商店顶端的AppBar,顶端AppBar通常用来做导航:
这是Windows8自带应用 “人脉” 底端的AppBar,底端AppBar通常用来显示命令按钮:
下面创建一个简单的拥有AppBar的应用,打开VS2012,FILE -> New -> Project -> Windows Store -> Blank App(XAML) -> 命名项目为 “AppBarDemo” -> OK.
MainPage.xaml:
19 10 11 18 1912 1713 14 15 1620 3321 22 3223 3124 25 2627 28 29 3034 3635
MainPage.xaml.cs:
1 namespace AppBarDemo 2 { 3 ///4 /// An empty page that can be used on its own or navigated to within a Frame. 5 /// 6 public sealed partial class MainPage : Page 7 { 8 public MainPage() 9 {10 this.InitializeComponent();11 }12 13 private void GetResultsButton_Click(object sender, RoutedEventArgs e)14 {15 DisplayTextBlock.Text += "\nShow results button is clicked!";16 }17 18 private void AddButton_Click(object sender, RoutedEventArgs e)19 {20 DisplayTextBlock.Text += "\nAdd button is clicked!";21 }22 23 private void RemoveButton_Click(object sender, RoutedEventArgs e)24 {25 DisplayTextBlock.Text += "\nRemove button is clicked!";26 }27 }28 }
按F5运行看下效果:
通过点击鼠标右键,Win+z或用手指从屏幕顶端或底端划过可使AppBar显示,点击AppBar中的按钮看下效果:
看下AppBar类的定义:
1 // Summary: 2 // Represents the container control that holds app UI components for commanding 3 // and experiences. 4 [MarshalingBehavior(MarshalingType.Agile)] 5 [Threading(ThreadingModel.Both)] 6 [Version(100794368)] 7 [WebHostHidden] 8 public class AppBar : ContentControl 9 {10 // Summary:11 // Initializes a new instance of the AppBar class.12 public AppBar();13 14 // Summary:15 // Gets or sets a value that indicates whether the AppBar is visible.16 //17 // Returns:18 // True to display the AppBar and make it visible. False to hide the AppBar.19 public bool IsOpen { get; set; }20 //21 // Summary:22 // Identifies the IsOpen dependency property.23 //24 // Returns:25 // The identifier for the IsOpen dependency property.26 public static DependencyProperty IsOpenProperty { get; }27 //28 // Summary:29 // Gets or sets a value that indicates whether the AppBar does not close on30 // light dismiss.31 //32 // Returns:33 // True if the AppBar does not close on light dismiss. False if the AppBar34 // is hidden on light dismiss.35 public bool IsSticky { get; set; }36 //37 // Summary:38 // Identifies the IsSticky dependency property.39 //40 // Returns:41 // The identifier for the IsSticky dependency property.42 public static DependencyProperty IsStickyProperty { get; }43 44 // Summary:45 // Occurs when the AppBar changes from visible to hidden.46 public event EventHandler
可以看到有一个IsOpen和一个IsSticky属性,可以通过从后台代码中把IsOpen设置为true来打开AppBar. AppBar默认是在用户与AppBar以外的部分进行交互时,AppBar自动隐藏,例如当AppBar在打开状态时,用鼠标左键点击应用的非AppBar部分,AppBar则会自动关闭,但将IsStick属性设置为true时,除非用户显式地点击鼠标右键或按Win+z, 或用手指从屏幕顶端或底端划过才会隐藏,否则AppBar会一直显示。给TopAppBar设置一个IsSticky属性,再运行,点击鼠标右键调出AppBar,在应用的非AppBar位置用鼠标左键点击以下,会发现BottomAppBar隐藏了,而TopAppBar还在。
同时AppBar还有一个Opened和一个Closed事件,可以在其中做一些事情。
更多关于AppBar的使用,可以参考以下链接: