Writing Your First Windows Phone 8 App

Steps

Start your Visual Studio 2012. Select New project to create a new application and name it “HelloWorldSilverlight” or as you wish. The figure is shown below.



Click Ok to create the new app. Now we can see a popup as shown in the following screen



In this popup, select your target as Windows Phone app either 7.1 or 8.0. In my case I'm selecting 8.0. Now click Ok to see the Windows Phone 8 interface in the design view with XAML code as shown below.



Now drag and drop the controls from the toolbox to get the user input. In my case, we will use a TextBlock, TextBox and Button. The TextBlock is used to show the user as “Enter Your Name” and TextBox is used to get the user input (name it nametxtbox) and Button to trigger some event. Change the button name to “Start “. We can change the control properties in the Properties menu as shown below.



After changing some properties our screen will look like this.



Now the design part is completed and we will create the code behind “Start” button event. Write the code below to get the user input from the nametxtbox and it provides a welcome message.

Simply double-click the “Start” button and it will automatically create the event handler and it go to the coding page.

C# Code


using
 System;
using
 System.Collections.Generic;
using
 System.Linq;
using
 System.Net;
using
 System.Windows;
using
 System.Windows.Controls;
using
 System.Windows.Navigation;
using
 Microsoft.Phone.Controls;
using
 Microsoft.Phone.Shell;
using
 HelloWorldSilverlight.Resources;

namespace
 HelloWorldSilverlight
{

   public
 partial class MainPage : PhoneApplicationPage
   {

      // Constructor

      public
 MainPage()
      {
         InitializeComponent();

      }

      private
 void Button_Click_1(object sender, RoutedEventArgs e)
      {

         string
 name=Nametxtbox.Text.ToString();
         MessageBox
.Show("Welcome :" + name);
      }
   }
}




Now build and run the application by pressing the F5 key in your keyboard. We can see the output in the Windows Phone 8 Emulator as shown below.

Enter your name in the TextBox and press the “Start” button.



In this chapter we finished the “Hello world Silverlight” application in Windows Phone 8 using Visual Studio 2012

Up Next
    Ebook Download
    View all
    Learn
    View all