2015元旦第一彈——WP8.1應用程式欄(C#後台代碼編寫)

來源:互聯網
上載者:User

標籤:

     //第一次寫博文,以後還請各位道友互相關照哈。廢話不多說,直接進入正題。

     相信大家對於如何在XAML添加應用程式欄應該很清楚,不清楚的話,可以開啟建立個Pviot應用 就有系統內建的功能表列。 本文主要講的是如何在C#後台進行動態添加應用程式欄以及功能表列最小化。

首先建立一個空白應用,然後在Gird下鍵入以下代碼:

 <Pivot Name="pivot" SelectionChanged="Pivot_SelectChanged">            <PivotItem Header="First">                                          </PivotItem>                        <PivotItem Header="Second">            </PivotItem>                        <PivotItem Header="Third">            </PivotItem>        </Pivot>

然後在MainPage.xaml.cs定義3個菜單:

 CommandBar refresh; CommandBar add; CommandBar min;

之後,我們定一個方法PrepareAppBars()來實現應用程式欄,這個方法要在OnNavigatedTo()或者建構函式內或者頁面載入內實現,這裡我選擇在OnNavigatedTo()內實現:

        protected override void OnNavigatedTo(NavigationEventArgs e)        {            // TODO: Prepare page for display here.            PrepareAppBars();            // TODO: If your application contains multiple pages, ensure that you are            // handling the hardware Back button by registering for the            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.            // If you are using the NavigationHelper provided by some templates,            // this event is handled for you.        }

  

  private void PrepareAppBars()        {            //Pivot第一個頁面功能表列            refresh = new CommandBar();            refresh.IsOpen = true;            AppBarButton RefreshBtn = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/refresh.png") } };            RefreshBtn.Label = "重新整理";            RefreshBtn.Click += RefreshBtn_Click;            RefreshBtn.IsEnabled = true;            // 相似的第二個菜單            AppBarButton SecondBtn = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/add.png") } };            SecondBtn.Label = "添加";            SecondBtn.Click += SecondBtn_Click;            SecondBtn.IsEnabled = true;            AppBarButton secondCommand = new AppBarButton();            secondCommand.Label = "菜單一";            secondCommand.Click += secondCommand_Click;            AppBarButton secondCommand2 = new AppBarButton();            secondCommand2.Label = "菜單二";            secondCommand2.Click += secondCommand2_Click;            refresh.SecondaryCommands.Add(secondCommand);            refresh.SecondaryCommands.Add(secondCommand2);                     refresh.PrimaryCommands.Add(RefreshBtn);            refresh.PrimaryCommands.Add(SecondBtn);           //Pivot第二個頁面功能表列            add = new CommandBar();            add.IsOpen = true;            AppBarButton Pivot_SecondAppBar = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/add.png") } };            Pivot_SecondAppBar.Label = "添加";            Pivot_SecondAppBar.Click += Pivot_SecondAppBar_Click;            SecondBtn.IsEnabled = true;            add.PrimaryCommands.Add(Pivot_SecondAppBar);            //Pivot第三個頁面功能表列最小化            min = new CommandBar();            AppBarButton thirdCommand = new AppBarButton();            thirdCommand.Label = "菜單三";            thirdCommand.Click += thirdCommand_Click;            min.SecondaryCommands.Add(thirdCommand);                       min.ClosedDisplayMode = AppBarClosedDisplayMode.Minimal;                }

  接著,我們定義 應用程式欄的所有單機事件:

   void thirdCommand_Click(object sender, RoutedEventArgs e)        {            MessageDialog msg = new MessageDialog("菜單三");            msg.ShowAsync();        }        void Pivot_SecondAppBar_Click(object sender, RoutedEventArgs e)        {            MessageDialog msg = new MessageDialog("Pivot第二個頁面添加");            msg.ShowAsync();        }        void SecondBtn_Click(object sender, RoutedEventArgs e)        {            MessageDialog msg = new MessageDialog("添加");            msg.ShowAsync();        }        void secondCommand2_Click(object sender, RoutedEventArgs e)        {            MessageDialog msg = new MessageDialog("菜單二");            msg.ShowAsync();        }        void secondCommand_Click(object sender, RoutedEventArgs e)        {            MessageDialog msg = new MessageDialog("菜單一");            msg.ShowAsync();        }        void RefreshBtn_Click(object sender, RoutedEventArgs e)        {            MessageDialog msg = new MessageDialog("重新整理");            msg.ShowAsync();        }

  做完這些之後呢,我們就可以在Pivot_SelectChanged頁面選擇的時候載入指定的應用程式欄了:

  private void Pivot_SelectChanged(object sender, SelectionChangedEventArgs e)        {            switch (pivot.SelectedIndex)            {                case 0:                    BottomAppBar = refresh;                    break;                case 1:                    BottomAppBar = add;                    break;                case 2:                    BottomAppBar = min;                    break;            }        }

  

 

2015元旦第一彈——WP8.1應用程式欄(C#後台代碼編寫)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.