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.
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.
public partial class MainPage : PhoneApplicationPage
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";
ApplicationBarIconButton apButon1 = new ApplicationBarIconButton();
apButon1.IconUri = new Uri("/Images/About.png", UriKind.Relative);
apButon1.Text = "About";
ApplicationBarMenuItem mItem = new ApplicationBarMenuItem();
mItem.Text = "Contact";
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.
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.