.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Windows Phone >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Programmatically create an Application bar at runtime in Windows Phone 8

Posted By:Karthikeyan Anbarasan       Posted Date: May 22, 2013    Points: 200    Category: Windows Phone    URL: http://www.dotnetspark.com  

In this article we are going to see how we can create an Application bar at runtime programmatically
 

Introduction:


In this article we are going to see how we can create an Application bar at runtime programmatically, in our earlier article we have seen how to do a static application bar but here it's going to be dynamic so we can change the application bar as per the requirement with the code base. To know about application bar, it is a row of Individual set of buttons that are inside an ellipsis on the phone screen. The user can make use of this set of buttons to navigate and do some user activities that are necessary for the application which is user friendly and easily accessible. Application bar also has a set of options which are Menu items which can be used as a menu on demand by click on the . button at the right side of the Application Bar. Let us see the step by step process on how to programmatically create the application bar at runtime in a Windows Phone 8 application.

Steps:


Open Visual Studio 2012 IDE in an administrator mode (Right Click and select Run as an Administrator) and select File à New Project to select Windows Phone application development template that is available under the Windows Phone tab in the left menu as shown in the screen below.




Now we can see the project created with a list of default files and folders that are basically required to run the application as shown in the screen below. Since we selected the blank project we will not see much of the inbuilt data that will be preloaded in most of the other templates that are available with the Visual Studio IDE.




Now run the application directly and we can see we don't have any application bar triggered at the bottom of the page. Now we will write our code which basically triggers the Application bar at run time based on the number of items we require. So let's do it by navigating to the code behind and write the below code which creates the Application Bar at runtime using the ApplicationBar class as shown in the screen below.

C# Code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ProgAppBarSample.Resources;

namespace ProgAppBarSample
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            this.ApplicationBar = new ApplicationBar();
            this.ApplicationBar.IsVisible = true;
            this.ApplicationBar.Opacity = 1;
            this.ApplicationBar.IsMenuEnabled = true;
            this.ApplicationBar.Mode = ApplicationBarMode.Minimized; 

            ApplicationBarIconButton apButon = new ApplicationBarIconButton();
            apButon.IconUri = new Uri("/Images/home.png", UriKind.Relative);
            apButon.Text = "Home";
            this.ApplicationBar.Buttons.Add(apButon);

            ApplicationBarIconButton apButon1 = new ApplicationBarIconButton();
            apButon1.IconUri = new Uri("/Images/About.png", UriKind.Relative);
            apButon1.Text = "About";
            this.ApplicationBar.Buttons.Add(apButon1);
            
            ApplicationBarMenuItem mItem = new ApplicationBarMenuItem();
            mItem.Text = "Contact";
            this.ApplicationBar.MenuItems.Add(mItem); 
        }


    }
}




Basically the code above has 2 app bar item and one menu item that will be created on run time on the application load. Now build and execute the application by simple pressing F5 key from the keyboard and we can see Visual Studio will build the application and open the Emulator to display the output as shown in the screen below.




Conclusion:


So in this article we have seen how to programmatically create an application bar at runtime with a list of items and menus that are basically required to have a nice user interaction to the Windows Phone 8 application.
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend