2015 New Year's Day first bomb--wp8.1 application bar (C # code behind)

Source: Internet
Author: User

The first time to write a blog post, but also ask you to take care of each other ha. Don't say much nonsense, go straight to the chase.

I believe that you should be very clear about how to add an application bar in XAML, you can open the new Pviot app to have the system comes with the menu bar. This article is mainly about how to add the application bar dynamically and minimize the menu bar in the background of C #.

Start by creating a new blank app, and then type the following code under Gird:

<pivot name="Pivot" selectionchanged="pivot_selectchanged" >            <pivotitem header="First">                                          </PivotItem>                        < PivotItem header="Second">            </PivotItem>                        <pivotitem Header ="third">            </PivotItem>        </Pivot>

Then define 3 menus in MainPage.xaml.cs:

CommandBar Refresh; CommandBar add; CommandBar min;

After that, we set a method Prepareappbars () to implement the application bar, which is implemented within the onnavigatedto () or constructor or page load, where I choose to implement within the Onnavigatedto ():

        protected override void Onnavigatedto (NavigationEventArgs e)        {            //Todo:prepare page for display here.            Prepareappbars ();            Todo:if your application contains multiple pages, ensure that is            //handling the hardware Back button by Regi Stering for the            //Windows.Phone.UI.Input.HardwareButtons.BackPressed event.            If you were using the navigationhelper provided by some templates,            //This event was handled for you.        }

  

  private void Prepareappbars () {//pivot The first page of the menu bar refresh = new CommandBar (); Refresh.            IsOpen = true; Appbarbutton refreshbtn = new Appbarbutton () {Icon = new Bitmapicon () {urisource = new Uri ("Ms-appx:///assets/refresh.pn            G ")}};            Refreshbtn.label = "Refresh";            Refreshbtn.click + = Refreshbtn_click;            Refreshbtn.isenabled = true; A similar second menu Appbarbutton secondbtn = new Appbarbutton () {Icon = new Bitmapicon () {urisource = new Uri ("Ms-app            X:///assets/add.png ")}};            Secondbtn.label = "Add";            Secondbtn.click + = Secondbtn_click;            Secondbtn.isenabled = true;            Appbarbutton Secondcommand = new Appbarbutton ();            Secondcommand.label = "Menu One";            Secondcommand.click + = Secondcommand_click;            Appbarbutton secondCommand2 = new Appbarbutton ();            Secondcommand2.label = "Menu Two"; Secondcommand2.click + = Secondcommand2_clicK Refresh.            Secondarycommands.add (Secondcommand); Refresh.                     Secondarycommands.add (SECONDCOMMAND2); Refresh.            Primarycommands.add (REFRESHBTN); Refresh.           Primarycommands.add (SECONDBTN);            Pivot second Page Menu bar add = new CommandBar (); Add.            IsOpen = true; Appbarbutton Pivot_secondappbar = new Appbarbutton () {Icon = new Bitmapicon () {urisource = new Uri ("Ms-appx:///assets/ad            D.png ")}};            Pivot_secondappbar.label = "Add";            Pivot_secondappbar.click + = Pivot_secondappbar_click;            Secondbtn.isenabled = true; Add.            Primarycommands.add (Pivot_secondappbar);            Pivot third page Menu bar minimized min = new CommandBar ();            Appbarbutton Thirdcommand = new Appbarbutton ();            Thirdcommand.label = "menu three";            Thirdcommand.click + = Thirdcommand_click; Min.                       Secondarycommands.add (Thirdcommand); Min. Closeddisplaymode = AppbarcloseddisPlaymode.minimal; }

Next, we define all the stand-alone events for the application bar:

   void Thirdcommand_click (object sender, RoutedEventArgs e) {messagedialog msg = new Messagedialog (menu            Three "); Msg.        Showasync (); } void Pivot_secondappbar_click (object sender, RoutedEventArgs e) {messagedialog msg = new Messa            Gedialog ("Pivot second page added"); Msg.        Showasync (); } void Secondbtn_click (object sender, RoutedEventArgs e) {messagedialog msg = new Messagedialog (            "Add"); Msg.        Showasync (); } void Secondcommand2_click (object sender, RoutedEventArgs e) {messagedialog msg = new Messagedi            Alog ("Menu II"); Msg.        Showasync (); } void Secondcommand_click (object sender, RoutedEventArgs e) {messagedialog msg = new Messagedia            Log ("menu One"); Msg.        Showasync (); } void Refreshbtn_click (object sender, RoutedEventArgs e) {messagedialog msg = new Messagedialog            ("Refresh"); Msg. ShowasyNC (); }

Having done this, we can load the specified application bar when the Pivot_selectchanged page is selected:

  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 New Year's Day first bomb--wp8.1 application bar (C # code behind)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.