2015元旦第一弹——WP8.1应用程序栏(C#后台代码编写)

     //第一次写博文,以后还请各位道友互相关照哈。废话不多说,直接进入正题。

     相信大家对于如何在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#后台代码编写)

上一篇:飞信api接入方法


下一篇:Windows KINECT Development 01 - Introduction of Microsoft KINECT