12

I have the following XAML code:

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="False" x:Name="PageBar">
        <shell:ApplicationBarIconButton IconUri="/Assets/Icons/appbar.questionmark.rest.png" Text="Help" x:Name="HelpIcon" Click="HelpIcon_Click" />
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="Help" x:Name="HelpItem" Click="HelpIcon_Click" />
            <shell:ApplicationBarMenuItem Text="About" x:Name="AboutItem" Click="AboutItem_Click" />
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

But inside C# code is always null.

Do you know why?

jv42
  • 8,521
  • 5
  • 40
  • 64
VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • At what point in the application/page lifecycle are you trying to access the application bar? Perhaps it hasn't been created at the time when you are trying to access it. – Derek Lakin May 09 '11 at 06:06

2 Answers2

16

In some stupid decision an ApplicationBar isn't a standard Silverlight object, because of that it doesn't really fit in the visual tree, can't be bound to and x:Name doesn't work.

You can refer to the ApplicationBar via a property on the PhoneApplicationPage.

var helpItem = this.ApplicationBar.MenuItems[0];
var aboutItem = this.ApplicationBar.MenuItems[1];
Nigel Sampson
  • 10,549
  • 1
  • 28
  • 31
6

I found this worked for me when I wanted to change the visiblity:

(ApplicationBar as ApplicationBar).IsVisible = true;

I got that answer from Matthew

Community
  • 1
  • 1
Phil
  • 616
  • 2
  • 8
  • 21