通過應用程式列(AppBar)可以在需要時向使用者顯示各種應用程式命令。應用程式列提供與使用者當前頁面或當前選定的內容相關的各種命令。預設情況下,應用程式列處於隱藏狀態。當使用者沿螢幕邊緣從頂部或底部用手指划動時會顯示應用程式列,還可以通過單擊滑鼠右鍵顯示。在使用者啟動命令、點擊應用介面或重複划動手勢後,應用程式列會自動消失。如果需要進行多選命令操作時,也可以以讓應用程式列始終可見。
建立應用程式列
以下側邊欄(BottomAppBar)為例,利用AppBar 控制項編寫一個具有文本編輯功能的應用程式列。
<Page.BottomAppBar> <AppBar x:Name="BottomAppBar"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="50*"/> <ColumnDefinition Width="50*"/> </Grid.ColumnDefinitions> <StackPanel x:Name="LeftPanel" Orientation="Horizontal" Grid.Column="0" HorizontalAlignment="Left"> <Button x:Name="Edit" Style="{StaticResource EditAppBarButtonStyle}" Click="Edit_Button_Click"/> <Button x:Name="Save" Style="{StaticResource SaveAppBarButtonStyle}"/> <Button x:Name="Delete" Style="{StaticResource DeleteAppBarButtonStyle}"/> </StackPanel> <StackPanel x:Name="RightPanel" Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Right"> <Button x:Name="Refresh" Style="{StaticResource RefreshAppBarButtonStyle}"/> </StackPanel> </Grid> </AppBar></Page.BottomAppBar>
從上面代碼可以看出編寫應用程式列本身並沒有什麼複雜之處,而且應用程式列中使用按鍵的風格在Win8 應用中也都提供了。在項目工程Common 檔案夾中找到StandardStyles.xaml 裡面有很多注釋掉的ButtonStyle,在裡面找到你需要的提出來即可。
編輯應用程式列
接下來,我們在應用介面中添加兩個CheckBox:一個用來將應用程式列固定,另一個用來新增或刪除應用程式列按鍵。
<StackPanel Orientation="Vertical" Grid.Row="1" Margin="120,50,0,0"> <CheckBox x:Name="IsSticky" Content="IsSticky" Click="IsSticky_Click" /> <CheckBox x:Name="AddHelpButton" Content="Add Help Button" Click="AddHelpButton_Click" /></StackPanel>
兩個CheckBox 點擊事件代碼如下,當應用程式列IsSticky 屬性啟用後,只能通過划動下邊螢幕或滑鼠右鍵將取消顯示。
private void IsSticky_Click(object sender, RoutedEventArgs e){ CheckBox cb = sender as CheckBox; AppBar ap = pageRoot.FindName("BottomAppBar") as AppBar; if (ap != null) { ap.IsSticky = (bool)cb.IsChecked; }}private void AddHelpButton_Click(object sender, RoutedEventArgs e){ CheckBox cb = sender as CheckBox; if ((bool)cb.IsChecked) { Button helpButton = new Button(); helpButton.Name = "Help"; helpButton.Style = App.Current.Resources["HelpAppBarButtonStyle"] as Style; RightPanel.Children.Add(helpButton); } else { RightPanel.Children.RemoveAt(1); }}
源碼下載
http://sdrv.ms/SSPQM2