This topic has not yet been rated - Rate this topic

Walkthrough: Creating an Application Bar Test Application for Windows Phone

Windows Phone

March 22, 2012

In this walkthrough, you build an Application Bar test application. The application that you create in this walkthrough enables you to test the different Application Bar properties, such as opacity, color, and mode. You can also test the Application Bar on a plain page, a pivot page, and a panorama page.

The user interface of each page of the application contains radio buttons for each of the Application Bar properties. As you click the radio buttons, the changes are made to the Application Bar dynamically, enabling you to test the different properties and their interactions with each other. For more information, see Application Bar Overview for Windows Phone.

NoteNote:

A completed version of this application is available as a download. For more information, see Code Samples for Windows Phone.

In this walkthrough, you perform the following tasks.

  • Create an Application Bar with four icon buttons and two menu items.

  • Dynamically change the properties of the Application Bar.

  • Create handlers for the Application Bar click events.

  • Use the navigation service to navigate between the pages of your application.

The completed application looks like the following.

The completed solution contains the following components.

The app in the emulatorThe files in solution explorer

To complete this walkthrough, you must have the Windows Phone SDK installed. For more information, see Installing the Windows Phone SDK.

First, you create a new Windows Phone application project named AppBarTester. In later steps, you add code to your application that assumes the application name is AppBarTester. If you choose a different name for your application, you must change the namespace references in the code.

To create the application project

  1. In Visual Studio, on the File menu, point to New and then click Project.

    The New Project dialog appears.

  2. In the left pane, click Installed Templates, expand Visual C# or Visual Basic, and then click Silverlight for Windows Phone.

  3. In the list of project types, click Windows Phone Application.

  4. In the Name box, type AppBarTester.

  5. Click OK.

    The New Windows Phone Application dialog appears.

  6. In the Target Windows Phone Version drop-down list, select Windows Phone 7.1 and then click OK.

    The new application project is created and opens in Visual Studio.

This application contains a main page that contains links to three secondary pages. The main page is used for navigation only; the three secondary pages are where you test the Application Bars. In this procedure, you create the main page, add the three secondary pages, and set up the navigation. In later procedures, you add Application Bars to the secondary pages.

To create the main page

  1. In Solution Explorer, double-click MainPage.xaml to open it in the designer.

  2. In the XAML code, locate the GRID element named LayoutRoot. Replace everything from there to the end of the file with the following code. This creates three links to the secondary pages.

        <!--LayoutRoot is the root grid where all page content is placed-->
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <!--TitlePanel contains the name of the application and page title-->
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="ApplicationTitle" Text="App Bar Tester" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock x:Name="PageTitle" Text="main page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
            </StackPanel>
    
            <StackPanel Grid.Row="1">
                <TextBlock Text="experiment with an Application Bar on different page types:" Margin="24" Style="{StaticResource PhoneTextLargeStyle}" TextWrapping="Wrap" />
    
                <HyperlinkButton Content="a plain page" Name="linkPlainPage" Margin="24" Click="linkPlainPage_Click" />
                <HyperlinkButton Content="a pivot page" Name="linkPivotPage" Margin="24" Click="linkPivotPage_Click" />
                <HyperlinkButton Content="a panorama page" Name="linkPanoramaPage" Margin="24" Click="linkPanoramaPage_Click" />
            </StackPanel>
        </Grid>
    </phone:PhoneApplicationPage>
    
  3. In Solution Explorer, right-click MainPage.xaml and then click View Code to open the code-behind file in the code editor.

  4. In the MainPage class, after the constructor, add the following code. This code creates the click event handlers for the three links. The code in each method navigates to the corresponding page. You create the pages in the next procedure.

    private void linkPlainPage_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/PlainPage.xaml", UriKind.Relative));
    }
    
    private void linkPivotPage_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/PivotPage.xaml", UriKind.Relative));
    }
    
    private void linkPanoramaPage_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/PanoramaPage.xaml", UriKind.Relative));
    }
    

To add the secondary pages

  1. In Solution Explorer, right-click the project, point to Add, and then click New Item.

    The Add New Item dialog appears.

  2. Select Windows Phone Portrait Page, enter the name PlainPage.xaml, and then click Add.

  3. Repeat step 1, select Windows Phone Pivot Page, enter the name PivotPage.xaml, and then click Add.

  4. Repeat step 1, select Windows Phone Panorama Page, enter the name PanoramaPage.xaml, and then click Add.

In this procedure, you run the application to test the application structure and navigation.

To test the application

  1. On the File menu, click Save All. (Ctrl+Shift+S)

  2. On the Build (or the Debug menu in Visual Studio 2010 Express for Windows® Phone 7.1), click Build Solution. (Ctrl+Shift+B)

  3. On the standard toolbar, set the deployment target of the application to Windows Phone Emulator.

    Target on Standard Toolbar selecting emulator

  4. On the Debug menu, click Start Debugging. (F5)

    The application starts in the emulator, and the main page appears.

  5. Tap the first link and verify that you navigate to the correct page.

  6. Press the Back button to return to the main page.

  7. Test the other two links and verify that they navigate to the correct pages.

  8. On the Debug menu, click Stop Debugging. (F5)

In this procedure, you add four image files to your application to use as the Application Bar button icons. The image files are part of a standard set of icons that is installed with the Windows Phone SDK. For more information, see Application Bar Icon Buttons for Windows Phone.

To add icon button images to your application

  1. In Solution Explorer, right-click your project, point to Add, and then click New Folder.

  2. Name the folder Images.

  3. In Solution Explorer, right-click the folder Images, point to Add, and then click Existing Item.

    The Add Existing Item dialog appears.

  4. Browse to one of the following locations to locate the standard icons:

    C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Icons\dark

    C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.1\Icons\dark

  5. Select all of the following files, and then click Add.

    • appbar.delete.rest.png

    • appbar.feature.settings.rest.png

    • appbar.questionmark.rest.png

    • appbar.save.rest.png

  6. In Solution Explorer, select all four new files.

  7. In the Properties window, set the following properties for the new files.

    Property

    Value

    Build Action

    Content

    Copy to Output Directory

    Copy if newer

    Caution note Caution:

    Build Action must be set to Content to use the icons as button images.

  8. Rename the files according to the information in the following table.

    Old name

    New name

    appbar.delete.rest.png

    delete.png

    appbar.feature.settings.rest.png

    settings.png

    appbar.questionmark.rest.png

    help.png

    appbar.save.rest.png

    save.png

To continue building this application, complete the procedures in one or more of the following optional topics. You do not need to complete the procedures in order.

Did you find this helpful?
(1500 characters remaining)