In this article I will talk about how to create global application bar dynamically in Windows Phone.
There are two way to create global application bar dynamically.
Let's write code
Option 1
Step 1: Create a silverlight for Windows Phone project.
Step 2: Open App.xaml and place below code of Application.Resources
<Application.Resources> <shell:ApplicationBar x:Key="GlobalAppBar" IsVisible="True" IsMenuEnabled="True"> </shell:ApplicationBar> </Application.Resources>
Step 3: Now open App.xaml.cs and place below code in Application_Launching method.
ApplicationBar appBar = ((ApplicationBar)Application.Current.Resources["GlobalAppBar"]); ApplicationBarIconButton appBarButtonAdd = new ApplicationBarIconButton(new Uri("/Images/appbar.add.png", UriKind.Relative)) { Text = "Test" }; appBarButtonAdd.Click += new EventHandler(appBarButtonAdd_Click); appBar.Buttons.Add(appBarButtonAdd); ApplicationBarMenuItem appBarMenuAdd = new ApplicationBarMenuItem("Menu 1"); appBarMenuAdd.Click += new EventHandler(appBarMenuAdd_Click); appBar.MenuItems.Add(appBarMenuAdd);
In the above code I am creating an application bar button and an application bar menu item, after creating the application bar button and an application bar menu item bind it to ApplicationBar. The first line of the method gives access to the application bar. Step 4: Now add event handler for the application bar button and application bar menu item.
void appBarButtonAdd_Click(object sender, EventArgs e) { MessageBox.Show("Button 1 clicked"); }
void appBarMenuAdd_Click(object sender, EventArgs e) { MessageBox.Show("Menu 1 clicked"); }
Step 5: Now open MainPage.xaml and add below line of code in PhoneApplicationPage
ApplicationBar = "{StaticResource GlobalAppBar}"
Above line needs to be added in all the pages where the application bar needs to be displayed. Step 6: Now run the application and you will get the application bar in the MainPage.
Option 2 Step 1: Create a silverlight for Windows Phone project.
Step 2: Add a cs class and name it Helper.cs
Step 3: Place below code in Helper.cs which will create application bar button. In below method we are not adding application bar button to the Application Bar.
public ApplicationBarIconButton CreateAppBarButton() { ApplicationBarIconButton appBarButtonAdd; appBarButtonAdd = new ApplicationBarIconButton(new Uri("/Images/appbar.add.png", UriKind.Relative)) { Text = "Test" }; appBarButtonAdd.Click += new EventHandler(appBarButtonAdd_Click); return appBarButtonAdd; }
void appBarButtonAdd_Click(object sender, EventArgs e) { MessageBox.Show("Button 1 clicked"); }
Step 4: Place below code in Helper.cs which will create application bar menu item. In below method we are not adding application bar menu item to the Application Bar.
public ApplicationBarMenuItem CreateAppBarMenu() { ApplicationBarMenuItem appBarMenuAdd = new ApplicationBarMenuItem("Menu 1"); appBarMenuAdd.Click += new EventHandler(appBarMenuAdd_Click); return appBarMenuAdd; }
void appBarMenuAdd_Click(object sender, EventArgs e) { MessageBox.Show("Menu 1 clicked"); }
Step 5: Now place below code in the OnNavigatedTo method like below in the code behind of xaml page to display application bar with button and menu item.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { if (ApplicationBar == null) { ApplicationBar = new ApplicationBar(); Helper hlp = new Helper();
ApplicationBarIconButton appBarButton = hlp.CreateAppBarButton(); ApplicationBar.Buttons.Add(appBarButton);
ApplicationBarMenuItem appBarMenuAdd = hlp.CreateAppBarMenu(); ApplicationBar.MenuItems.Add(appBarMenuAdd); } }
This ends the article of creating dynamic global application bar.
|