Windows Phone 7 Development : Wiring Up Events to an Application Bar ( part 2)

1/30/2011 10:07:08 AM

3. Reacting to Save Button Events

Continuing the walkthrough, let's now add an event handler to the Save button of the Application Bar. You'll write code so that the user clicks the Save button, and the application will hide the text box and change the text of the textblock to thank the user for entering a name.

  1. Locate MainPage.xaml in the Solution Explorer, and right-click and select View Code. Add the following line of code to the MainPage() constructor code. Don't forget to use the "Tab+Tab" trick to let Visual Studio automatically generate skeleton code for you (described in the previous section):

    btnSave.Click += new EventHandler(btnSave_Click);

  2. Add the following code to the btnSave_Click function:

    void btnSave_Click(object sender, EventArgs e)
    textBlock1.Text = "Thank you, "+ textBox1.Text;

    textBox1.Visibility = Visibility.Collapsed;

  3. Press F5 to see the results of your work. When you click the "+" icon, you will be prompted to enter your name. Once you enter your name and press the Save button on the Application Bar, the application displays a simple thank-you message. If, for some reason, the full text of the message does not fit within the textblock we created, you can increase both the width and the height of the textblock by setting the TextWrapping property of the textblock to "Wrap."

Now you're ready to enhance the Application Bar even further by writing code for your menu items to do some meaningful work.

4. Reacting to Menu Events

The code you write to react to menu click events is almost identical to code for Application Bar button events, with the "glue code" attached to the menu item instead of the Application Bar button. The block of code shown here displays a simple text message when the user clicks on the first menu item in the Application Bar that you created previously. Note that only a portion of the MainPage() constructor is shown, since the rest of it remains unchanged from the prior walkthrough.

menuItem1.Click+=new EventHandler(menuItem1_Click);

void menuItem1_Click(object sender, EventArgs e)
textBlock1.Visibility = Visibility.Visible;
textBlock1.Text = "You just clicked on Menu Item 1";

Press F5 to run the application now. You should see an Application Bar appear with an ellipsis in the right corner. If you press the ellipsis, two menu items become visible. Once clicked, the text on the phone screen changes to reflect the name of the menu item clicked.

In the real application, you will certainly want to do something more meaningful than what we have done. For instance, you may have menu items for "Help" and "About." If the user clicks Help, a Web Browser control (discussed in the next section) could be programmed to display a set of application Help files. If the "About" menu item is clicked, you can use the Web Browser control again to show your company's web page, or to simply display basic contact information.

One final thing we need to look at before leaving this article is using XAML to link event handling code to XAML elements.

Figure 3. If you need to edit XAML code but it's not visible on the screen, click the Expand button to show XAML for the current page.

5. Adding Event Handlers with XAML

It is also possible to write the necessary code that attaches (or "glues") a certain event to a managed code in XAML. For code readability and understandability purposes, this approach may be preferable to the managed code approach that we have already discussed. Imagine that you are trying to maintain an application that someone else wrote—it would be easier for you to understand and trace application behavior by starting with the XAML design elements and following their "glue code" into the event handlers. The steps you follow to wire up events in XAML are pretty straightforward, as illustrated here.

  1. Locate MainPage.xaml in the Solution Explorer and double-click that file to bring up XAML designer.

  2. If only Windows Phone 7 design surface is shown and no XAML code is visible, click the Expand Pane (<<) button in the lower right portion of the screen, as shown in Figure 7-6.

  3. Paste the following XAML in MainPage.xaml (it is identical to XAML from the "Adding a Local Application Bar Using XAML"):

    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
    <shell:ApplicationBarIconButton IconUri="/Images/"
    <shell:ApplicationBarIconButton IconUri="/Images/"
    <shell:ApplicationBarIconButton IconUri="/Images/"
    <shell:ApplicationBarMenuItem Text="Menu Item 1" IsEnabled="True">
    <shell:ApplicationBarMenuItem Text="Menu Item 2" IsEnabled="True">

  4. Locate the <shell:ApplicationBarIconButtonIconUri="/Images/" Text="add"> statement in XAML and add "Click" to the end of that statement, so that it resembles the code here:

    <shell:ApplicationBarIconButton IconUri="/Images/" Text="add"  Click=">

Note how Visual Studio automatically shows a choice of "New Event Handler" right after the double quotes. If you press the Tab key now, the skeleton code for the "worker function" will be automatically inserted in the MainPage.xaml.cs file and it will have a default name of ApplicationBarMenuItem_Click. To add functionality to the Application Bar button click event, open MainPage.xaml.cs (by right-clicking the MainPage.xaml file and selecting View Code) and edit that function in a way similar to the preceding button click event function.

  •  Windows Phone 7 Development : Wiring Up Events to an Application Bar ( part 1) - Reacting to Add Button Events
  •  Adding an Application Bar to a Windows Phone 7 Application (part 3) - Adding an Application Bar Using Managed Code
  •  Adding an Application Bar to a Windows Phone 7 Application (part 2) - Adding a Local Application Bar Using XAML & Adding Menu Items
  •  Adding an Application Bar to a Windows Phone 7 Application (part 1) - Adding Images for Use with Application Bar Buttons & Adding a Global Application Bar Using XAML
  •  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 3) - Finishing the Interface
  •  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 2) - Adding an Image View
  •  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 1)
  •  iPhone Application Development : User Input and Output
  •  Windows Phone 7 : Using Accelerometer Data to Move a Ball
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 4) - Device Libraries
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 3) - Transcoders
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 2) - Detecting the Context
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 1) - HTTP
  •  Using Windows Phone 7 Technologies : Retrieving Accelerometer Data (part 2)
  •  Using Windows Phone 7 Technologies : Retrieving Accelerometer Data (part 1)
  •  Using Windows Phone 7 Technologies : Understanding Orientation and Movement
  •  Programming the Mobile Web : HTML 5 (part 4) - Client Storage
  •  Programming the Mobile Web : HTML 5 (part 3) - Offline Operation
  •  Programming the Mobile Web : HTML 5 (part 2) - The canvas Element
  •  Programming the Mobile Web : HTML 5 (part 1)
    Top 10
    Operating System Options (Part 2)
    Operating System Options (Part 1)
    Adobe Premiere Elements 11.0
    Dragon NaturallySpeaking 12.0 Premium
    Expert Computing Advice – January 2013 (Part 2)
    Expert Computing Advice – January 2013 (Part 1)
    Managing Your Files In Windows 8
    Kindle Fire HD - Amazon’s Kindle e-Reader
    Versus Touchpad 9.7 - Attractive Pre-Loaded Software
    The Fourth-Generation iPad Review
    Most View
    BizTalk 2006 : Editing and Resubmitting Suspended Messages (part 2) - Pseudo-Walkthrough to Perform Edits and Resubmits
    The Language of Apple Platforms : Memory Management
    IIS 7.0 : Securing Communications with Secure Socket Layer (SSL)
    ASP.NET and AJAX
    Microsoft Windows Home Server 2011 : Automating Client Logons
    Nexus 7 Vs Kindle Fire
    Showdown: lOS vs Android vs WP7 (Part 2)
    100 Windows Speed-Up Tips (Part 7) - Five ways Jump Lists can save your time
    Use Photoshop Touch To Edit Your Photos
    Exchange Server 2007 : Configure the Client Access Server - Create and Apply ActiveSync Mailbox Policies
    MySQL Server Monitoring (part 1) - SQL Commands
    Intel vs AMD - The Choice Is Harder Than Ever (Part 1)
    Corsair Vengeance C70 Mid - Tower Gaming Case
    Programming Microsoft SQL Server 2005: Using the Data Mining Wizard and Data Mining Designer (part 5) - Viewing Mining Models
    Ultrasone HFI-580 Headphone Review
    All About Compact System Cameras (Part 1) - Nikon 1 V1
    Choosing a super-zoom camera (part 7) - Nikon Coolpix S9100 & Olympus SZ-30MR
    Canon EOS 650D - Big Improvements (Part 1)
    Using Non-Windows Systems to Access Exchange Server 2010 : Configuring and Implementing Entourage for the Mac
    Enabling Presence Information in SharePoint with Microsoft Communications Server 2010