|
This is my fourth article on ApplicationBar and still discovering new things about ApplicationBar.
The last three articles were.
1. Part 10 - Windows Phone 7- ApplicationBar
2. Part 53 - Windows Phone 7 - Dynamic ApplicationBar
3. Part 60 - Windows Phone 7 - Global Application Bar
In this article I will find out how to access ApplicationBarIconButton and ApplicationBarMenuItems. We will also discuss on how to access Global ApplicationBar Buttons and MenuItems.
ApplicationBar
Step 1: Create Silverlight for Windows Phone project and it will have MainPage.xaml will be added to it by default.
Step 2. There will be ApplicationBar section in MainPage.xaml.Uncomment ApplicationBar section and add x:Name to first ApplictionBarIconButton like below highlighted.
<phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> <shell:ApplicationBarIconButton x:Name="AppBarIconButton" IconUri="/Images/appbar_button1.png" Text="Button 1"/> <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/> <shell:ApplicationBar.MenuItems> <shell:ApplicationBarMenuItem Text="MenuItem 1"/> <shell:ApplicationBarMenuItem Text="MenuItem 2"/> </shell:ApplicationBar.MenuItems> </shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar>
Step 3: Add below line in the MainPage method of the MainPage.xaml.cs. I am just trying to change the image of the ApplicationBarIconButton.
AppBarIconButton.IconUri = new Uri("ApplicationIcon.png", UriKind.Relative);
Step 4: Now run the application. You will get NullReferenceException.
Now if you debug if you will notice AppBarIconButton is null.

Step 5: Now, to access ApplicationBarIconButton we can use below line of code.
ApplicationBarIconButton appBarButton = (ApplicationBarIconButton)ApplicationBar.Buttons[1];
appBarButton.IconUri = new Uri("ApplicationIcon.png", UriKind.Relative);
ApplicationBarMenuItem appBarMenuItem = (ApplicationBarMenuItem)ApplicationBar.MenuItems[1];
ApplicationBar provides list of Buttons and MenuItems in the ApplicationBar.
Global ApplicatinBar
Step 1: Now add ApplicationBar in App.xaml to make it global. Name first ApplicationBarIconButton as AppBarIconButton like shown below in highlighted.
<Application.Resources> <shell:ApplicationBar x:Key="GlobalAppBar" IsVisible="True" IsMenuEnabled="True"> <shell:ApplicationBarIconButton x:Name="AppBarIconButton" IconUri="/Images/add.png" Text="add"/> <shell:ApplicationBarIconButton IconUri="/Images/minus.png" Text="minus"/> <shell:ApplicationBar.MenuItems> <shell:ApplicationBarMenuItem Text="MenuItem 1"/> <shell:ApplicationBarMenuItem Text="MenuItem 2"/> </shell:ApplicationBar.MenuItems> </shell:ApplicationBar> </Application.Resources>
Step 2: Now you type name of the first ApplicationBarIconButton which is AppBarIconButton press .(dot). You won't get intellisense which means AppBarIconButton doesn't exist.
Step 3: Let's try to access ApplicationBarIconButton as we did earlier. Now you will get error of "An object reference is required for the non-static field, method, or property 'Microsoft.Phone.Shell.ApplicationBar.Buttons.get'".

Step 4: Below is the way to access global application bar in the codebehind of App.xaml.cs.
ApplicationBar appBar = ((ApplicationBar)Application.Current.Resources["GlobalAppBar"]); ApplicationBarIconButton appBarButton = (ApplicationBarIconButton)appBar.Buttons[0]; appBarButton.IconUri = new Uri("ApplicationIcon.png", UriKind.Relative);
This ends the article of accessing local ApplicationBar and Global ApplicationBar in codebehind.
|