MOBILE

Building Your First Windows Phone 7 Application (part 3) - Writing Your First Windows Phone Code

1/16/2011 9:26:16 AM

3. Writing Your First Windows Phone Code

In this section, we will be writing C# code that will handle the button click event that will populate the TextBlock named "textBlock1" with "hello World!"

  1. To add behavior to the OK button, double-click the OK button on the Design surface of your project. Visual Studio will display MainPage.xaml.cs where you can see the btnOk_Click method is automatically added. You will add proper code in the next step to handle the button click event.

    using System.Windows;
    using Microsoft.Phone.Controls;

    namespace HelloWorld
    {
    public partial class MainPage : PhoneApplicationPage
    {
    public MainPage()
    {
    InitializeComponent();
    // Setting SupportOrientations will control the behavior
    // of the page responding properly to the rotation of the
    // phone. So if you rotate the phone to the landscape
    // your page will change to landscape view.
    SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape
    ;
    }

    private void btnOk_Click(object sender, RoutedEventArgs e)
    {

    }
    }
    }


  2. In MainPage.xaml you will notice that the button Click event handler is automatically added to OK button.

    <Button Content="OK" Height="70"
    HorizontalAlignment="Right" Margin="0,155,-4,0"
    Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />


  3. In MainPage.xaml.cs replace the button1_click method with the following code.

    private void button1_Click(object sender, RoutedEventArgs e)
    {
    txtMessage.Text = "Hello World!";
    }

4. Running Your First Silverlight Windows Phone Application

Your Hello World application is complete. Now it's time to build the application and run it in the Windows Phone 7 emulator.

  1. To build the solution, select Build => Build Solution on the Visual Studio menu.

  2. To run the application, select Debug => Start Debugging.

  3. When the emulator appears, click OK and you will see "Hello World!" as shown in Figure 7.

    Figure 7. Hello World in Windows Phone 7 Emulator
  4. Click the rotate control on the Windows Phone 7 emulator, as shown in Figure 8.

    Figure 8. Clicking Windows Phone 7 Emulator rotate control

Notice in the landscape view that the TextBox is automatically resized, stretched out to make full use of the landscape orientation of the device, as shown in Figure 9.

Figure 9. Hello World landscape view

  1. Stop the application debugging by selecting Debug => Stop Debugging.

The Windows Phone 7 emulator can take a long time to start, so you want to avoid closing it down whenever possible. If you need to stop an application in the middle of a debugging run, it's better to use the Visual Studio Debug => Stop Debugging command instead of completely closing down the Windows Phone 7 emulator. Using this technique, the next time the application debugging starts, the project will be loaded into the emulator without first waiting for the emulator to be started.

Other  
  •  Introducing Windows Phone 7 and the Windows Phone Platform
  •  Windows Phone Application Platform
  •  iPhone Application Development : Basic User Input and Output
  •  Mobile Phone Game Programming : A Quick J2ME Primer
  •  Mobile Phone Game Programming : Java As a Mobile Game Platform
  •  Mobile Phone Game Programming : Getting to Know Mobile Platforms
  •  Mobile Application Security : The Apple iPhone - Local Data Storage: Files, Permissions, and Encryption
  •  Mobile Application Security : The Apple iPhone - Permissions and User Controls
  •  iPhone Application Developmen : Using the View-Based Application Template (part 3)
  •  iPhone Application Developmen : Using the View-Based Application Template (part 2) - Preparing the View Controller Outlets and Actions
  •  iPhone Application Developmen : Using the View-Based Application Template (part 1)
  •  Mobile Application Security: Application Format
  •  Mobile Application Security: Security Testing
  •  Mobile Application Security: The Apple iPhone - Development
  •  Building Android Apps : Installing KiloGap in the Emulator
  •  Building Android Apps : Build KiloGap
  •  Building Android Apps: Create an Android Virtual Device
  •  Building Android Apps: Going Native - Setting Up the Environment
  •  Building Android Apps: Introduction to PhoneGap
  •  iPhone Application Development : How Xcode and Interface Builder Implement MVC
  •  
    Top 10
    TuneAudio Marvel – Loudspeaker (Part 2)
    TuneAudio Marvel – Loudspeaker (Part 1)
    Wilson Audio Alexia - High Performance Loudspeakers (Part 3)
    Wilson Audio Alexia - High Performance Loudspeakers (Part 2)
    Wilson Audio Alexia - High Performance Loudspeakers (Part 1)
    Best Brands In Electronic World (Part 2)
    Best Brands In Electronic World (Part 1)
    Top 10 Electronics Devices – May 2013
    Great Sound, Wire-Free
    Hands-On Future Input Devices
    Most View
    Datacolor SpyderLensCal (Part 2)
    Build a Smut Proof PC
    Canon Powershot D20 - Super-Durable Waterproof Camera
    Windows Tips & Tricks (Part 4)
    Tiki USB Microphone - Fluent talking
    The HP Virtual Server Environment : The Partitioning Continuum at a Glance
    Choose The Right Business Broadband (Part 2)
    Group Test: Android Tablets – November 2012 (Part 1) - Acer Iconia A110, Acer Iconia A210
    BizTalk 2006 : Dealing with Extremely Large Messages (part 2) - Large Message Encoding
    Samsung Galaxy Note II In-Depth Review (Part 6)
    Best Video Editing Software – November 2012 (Part 1)
    How To Install FreeDOS (Part 1)
    Mobile Application Security : SymbianOS Security - Introduction to the Platform
    Nokia Lumia 810 – T-Mobile (Part 1)
    7 Necessary Accessories For DSLR
    AMD Radeon HD 7850 2GB vs. Nvidia GeForce GTX 660 2GB vs. AMD Radeon HD 7870 2GB (Part 2)
    How To Use Skype To Help Your Business
    The 30 Most Important Technology Trends (Part 3)
    Windows 7 : Using Advanced Security Options (part 1) - Configuring the Action Center & Performing a Manual Scan
    HP Network Node Manager 9 : Before we Manage with NNMi (part 2) - Understanding Smart Plug-ins - iSPI Performance for Metrics