位置:首頁 > 軟件操作教程 > 編程開發(fā) > C# > 問題詳情

C# CommandBar 控件

提問人:劉團圓發(fā)布時間:2020-12-11

    CommandBar為用戶提供的功能與桌面應(yīng)用程序的工具欄基本相同,但應(yīng)該讓它們簡單得多,通常限制工具欄上可用的選項少于8項。

    —次可以顯示多個CommandBar,但請記住,這會使用戶界面很雜亂,不應(yīng)該只為顯示更多選項而顯示多個工具欄。另一方面,如果想提供多種導(dǎo)航,有時同時把工具欄顯示在頂部和底部會比較好。

    Visual Studio附帶了 CommandBar控件,很容易創(chuàng)建這種類型的控件。下面的示例創(chuàng)建一個應(yīng)用程序的工具欄,其中包含許多標準項。


試一試  創(chuàng)建 CommandBar: Ch25Ex05

(1)回到先前的Bas icNavigation示例。

(2)在三個頁面上都添加一個CommandBar。把它作為每個頁面上Grid控件的子元素:

    <CommandBar>

      <AppBarToggleButton x:Name="toggleButtonBold" Icon ="Bold" Label="Bold" Click=

"AppBarToggleButtonBold_Click"/>

      <AppBarSeparator />

      <AppBarButton Icon="Back" Label="Back" Click="buttonGoBack_Click"/>

      <AppBarButton Icon="Forward" Label="Forward" Click=

"AppBarButtonForward_Click"/>

      <CominandBar. SecondaryCommands>

        <AppBarButton Icon=HCamera" Label="Take picture" />

        <AppBarButton Icon="Help" Label="Help" />

      </CommandBar,SecondaryCommands>

    </CommandBar>

(3)把下面的事件處理程序添加到所有三個頁面上:

    private void AppBarButtonForwardClick(object sender, RoutedEventArgs e)

    {

      if (Frame.CanGoForward) this.Frame.GoForward();

    }

    private void AppBarToggleButtonBold_Click(object sender, RoutedEventArgs e)

    {

      AppBarToggleButton toggleButton = sender as AppBarToggleButton; 

      bool isChecked = toggleButton.IsChecked.HasValue ?

                (bool)toggleButton?.IsChecked.Value : false; 

      textBlockCaption.FontWeight = isChecked ? FontWeights.Bold :

FontWeights.Normal;

    }

(4)把下面的using語句添加到所有頁面上:

    using Windows.UI.Text;

(5)在所有三個頁面上,把文本框的margin改為丨0,50,10,10。

(6)運行該應(yīng)用程序。


示例說明

  運行這個應(yīng)用程序時,現(xiàn)在可以使用命令欄按鈕在曾經(jīng)訪問過的頁面列表中來回移動。命令欄本身很容易處理。

  命令欄用三種類型來建立。第一個是AppBarToggleButton。

      <AppBarToggleButton x:Name="toggleButtonBold" Icon="Bold" Label= "Bold" Click= 

      "AppBarToggleButtonBold_Click" />

  這種類型的按鈕可用來顯示開啟或關(guān)閉的狀態(tài)。

  第二種類型是AppBarButton,與任何其他按鈕類似,事實上可以看到,AppBarButtonBack按鈕的單擊事件是由前面示例中ButtonBack的同一個事件處理程序處理的。

    <AppBarButton Icon="Back" Label="Back" Click="buttonGoBack_Click"/>

用于命令欄的第三中類型是AppBarSeperator。這種控件只顯示命令欄中的分隔線。

最后,兩個按鈕位于CommandBar.SecondaryCommands標簽內(nèi):

    <CommandBar. SecondaryCominands>

      <AppBarButton Icon="Camera" Label="Take picture" />

      <AppBarButton Icon="Help" Label="Help" />

    </CommandBar.SecondaryCommands>

  </CommandBar>

這些命令不直接顯示在命令欄上,相反,在單擊顯示的三個點時,它們顯示為下拉框。

繼續(xù)查找其他問題的答案?

相關(guān)視頻回答
回復(fù)(0)
返回頂部