Setting up an ApplicationBar using XAML

As the sample code from Microsoft uses code to create an application bar, I thought it might be interesting to show how this is done using XAML only as I find it a bit cleaner.

First thing to remember is that in order for a PhoneApplicationPage to make use of a ApplicationBar created in XAML, it needs to first have been declared before the page is instantiated. This means adding the definition to the parent’s resources, in this case App.xaml. If you don’t do this, you’ll end up dancing with null reference exceptions.

So, to reach the ApplicationBar class at all, we need a reference to Microsoft.Phone.Shell.

reference

Next, we need to add this namespace to App.xaml.

xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone.Shell"

We then create a resource that we will use to bind later in our page.

    <Application.Resources>
        <shell:ApplicationBar x:Key="appBar">
           
        </shell:ApplicationBar>
    </Application.Resources>

Now we have an empty application bar ready for use. To make it a bit more interesting, we’ll add a button on it.

        <shell:ApplicationBar x:Key="appBar">
            <shell:ApplicationBar.Buttons>
                <shell:ApplicationBarIconButton IconUri="/Images/appbar.save.rest.png"/>
            </shell:ApplicationBar.Buttons>
        </shell:ApplicationBar>

(Remember: Build Action on icons need to be set to Content)

For fun, we’ll add some menu items as well.

        <shell:ApplicationBar x:Key="appBar" IsMenuEnabled="True">
            <shell:ApplicationBar.Buttons>
                <shell:ApplicationBarIconButton IconUri="/Images/appbar.save.rest.png"/>
            </shell:ApplicationBar.Buttons>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="Test 1"/>
                <shell:ApplicationBarMenuItem Text="Test 2"/>
                <shell:ApplicationBarMenuItem Text="Test 3"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>

All values can be databound, so you can do a lot of fun quite easily using XAML only.

To enable the application bar to show menu items (ie. show the little toggle button “…”) you have to set IsMenuEnabled to true as in the example above.

That should be sufficient for now, so let’s bind it up to our application page.

The PhoneApplicationPage class exposes a property called ApplicationBar, and you can bind resources to it like most other properties in Silverlight.

Here’s how we do it:

<phoneNavigation:PhoneApplicationPage x:Class="ApplicationBarExample1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phoneNavigation="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}"

ApplicationBar="{StaticResource appBar}">

And here’s the result:

sample

 

How about binding the visibility of the application bar to a toggle button?

Just add this in your page (notice the IsChecked databinding):

<wp:ToggleControlSwitch Content="Toggle bar" Height="60" HorizontalAlignment="Left" Margin="20,23,0,0" Name="toggleControlSwitch1" VerticalAlignment="Top" Width="200" IsChecked="{Binding Path=ApplicationBar.Visible,ElementName=appPage,Mode=TwoWay}" />

toggleoff toggleon

No code involved whatsoever 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: