構建應用程式列的目的的顯示導航、命令和始終隱藏不需要的使用的工具。我們可以把應用程式列放在頁面 頂部或底部或同時存在頂部和底部。
預設情況在AppBar是隱藏的,當使用者單擊右鍵、按下Win+Z 、或從螢幕的頂部或底部邊緣輕鬆時可顯示或關閉AppBar。當然我們也可以通過編程的方式將AppBar設 置為當使用者做選擇或與應用互動時顯示。
構建AppBar基本步驟
通常我們構建一個應用的 AppBar,只需要三步就可以完成:
如何構建AppBar
應用中添加AppBar,需要將AppBar控制項指定給Page的TopAppBar或 BottomAppBar屬性。
XAML代碼可如下:
<Page.BottomAppBar> <AppBar x:Name="bottomAppBar" Padding="10,0,10,0"> <Grid> <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"> <Button Style="{StaticResource EditAppBarButtonStyle}" /> <Button Style="{StaticResource RemoveAppBarButtonStyle}" /> <Button Style="{StaticResource AddAppBarButtonStyle}" /> </StackPanel> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> <Button Style="{StaticResource RefreshAppBarButtonStyle}" /> <Button Style="{StaticResource HelpAppBarButtonStyle}" /> </StackPanel> </Grid> </AppBar> </Page.BottomAppBar>
XAML代碼中引用的資源樣式可以在應用程式解決方案的Common 檔案夾中StandardStyles.xaml檔案中找到。
運行效果:
若我們想在載入頁面時開啟AppBar,可以在XAML代碼中將AppBar控制項的IsOpen屬性值設定為true, 也可以在C#代碼中控制開啟AppBar。
private void OpenButton_Click(object sender, RoutedEventArgs e) { topAppBar.IsOpen = true; }
當使用者在應用的AppBar以外任何位置進行互動時,預設情況會解除AppBar進行隱藏。我們可 以將IsSticky屬性值設定為true來改變解除模式。
此時使用者只有右擊、按下Win+Z、或從螢幕的 頂部或底部邊緣輕掃時才會隱藏AppBar。
private void StickyButton_Click(object sender, RoutedEventArgs e) { bottomAppBar.IsSticky = true; }