问题
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?
回答1:
I found this worked for me when I wanted to change the visiblity:
(ApplicationBar as ApplicationBar).IsVisible = true;
I got that answer from Matthew
回答2:
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];
来源:https://stackoverflow.com/questions/5933109/applicationbar-is-always-null