Introduction to Windows Mobile 7 Development

Introduction to Windows Mobile 7 Development

Tutorial Details
  • Technology: Windows Phone 7
  • Difficulty: Beginner
  • Estimated completion time: 45 Minutes

Mobiletuts+ will be covering all major mobile platforms – iPhone, Windows, Android and Blackberry. Today we’ll be taking a look at Windows Phone 7 development. Windows Phone 7 is the latest mobile operating system from Microsoft. It is a clean break from previous Windows Mobile operating systems, such as WinMo 6.5, and offers .NET developers a chance to get in on the mobile application explosion that has happened in recent years. This tutorial will introduce you to the Windows Phone 7 platform, walk you through installing the SDK, and demonstrate the fundamental code necessary to build a simple application.

About the Platform

Windows Mobile 7 development is done using the .NET framework. The .NET framework is a software framework created by Microsoft for use in creating Windows applications. Programmers write applications using one of the several languages supported by the .NET framework, like C#, and the applications then execute inside of a runtime environment called the Common Language Runtime. For Windows Phone 7, there are two distinct development approaches you can take when creating your application.

The first approach is to use Silverlight for Windows Phone. Silverlight was originally envisioned as a way for developers to create rich internet applications. It has seen a sharp increase in market adoption in recent years, driven mostly by the fact that Netflix uses Silverlight to stream videos and NBC used Silverlight for its online broadcast of the Olympic games. A Silverlight application combines declarative markup (called XAML) to construct the user interface and code written in a .NET framework language to control an application’s behavior. If you’re developing a data driven application for Windows Phone 7, you should probably use Silverlight.

Alternatively, you can use the XNA framework to develop your Windows Phone 7 app. XNA is Microsoft’s game development framework and has been used in recent years to create both Windows and Xbox 360 applications. If you’re creating a game for Windows Phone 7, you’ll likely use the XNA framework. The XNA framework is quite powerful, but that power comes with a considerable learning curve and longer development cycles.

Getting Set Up

Let’s get started by making sure your development environment is set up correctly. You’ll need to be running Windows Vista or Windows 7 and you need to have a DirectX 10 capable graphics card installed in your computer. After verifying that you meet these requirements, visit http://bit.ly/9FXxQC to download the development tools.

Scroll to the bottom of the page and download the file named VM_BOOT\vm_web.exe. Once the file has downloaded, double click on it to install the complete Windows Phone Developer Tools package. The package includes:

  • Visual Studio 2010 Express for Windows Phone
  • Windows Phone Emulator
  • Silverlight for Windows Phone
  • XNA 4.0 Game Studio

Your computer will likely restart at least once while the tools are being installed. After you’ve installed the developer tools on your system, you are ready to get started.

Opening Visual Studio

The application you’ll create in this tutorial is a simple one. It displays a simple button that you can tap. When you tap it, the button rotates around the phone’s interface. We’ll develop this application using Silverlight for Windows Phone. It should take less than 10 minutes to create.

To begin, open Microsoft Visual Studio 2010 Express for Windows Phone. The easiest way to find it is to open the Start menu and begin typing “Visual Studio.” The application should show up in your search result list, similar to the following:

Searching for Visual Studio 2010 Windows Phone 7 Development

Click on the Visual Studio 2010 item to open up the development environment. If this is your first time opening the application, it may take a few minutes to start as Visual Studio will initialize some settings for you.

Creating Your Project

When you open Visual Studio, you’ll be greeted with the application’s start page. There is a lot of content on this page, including development tips, recent news, and project related actions. It is sometimes helpful to browse the content here to learn more about the platform, but for now just click on the “New Project…” link in the left sidebar.

A dialog box will pop up that guides you through creating your new project. Make sure the “Windows Phone Application” item is selected as your project template. Then, give your project a name. For this tutorial, I recommend calling your project “Rotating_Button.” Finally, confirm that the “Create directory for solution” checkbox is selected. This helps to organize your development efforts. Your settings should match mine:

Creating a New Windows Phone 7 Project

Click “OK” to create your new project.

Navigating the UI Editor

The template that you’ve selected provides you with a completely working application. To see it in action, simply press CTRL+F5 to compile the application and launch it in the Windows Phone Emulator. The emulator launches with a single page containing an application title and a page title.

That default UI just won’t do for our application, so let’s make some modifications. Visual Studio should have opened the file MainPage.xaml for editing when you created the project. If not, double click the file’s name in the Solution Explorer to open it.

You should see a split screen view. On one side of the development environment you can see how the current file will look when your application is run. This is design mode. On the other side you have the XAML markup that declares how your interface should look. Any changes you make on one side will be represented on the other. This is similar to a WYSIWYG HTML editor like Dreamweaver.

The MainPage.xaml file open in Design/Code Mode Windows Phone 7 Development

The first thing we want to do is delete everything inside of the layout grid so we can provide our own markup. You should see a Grid tag named LayoutRoot. Delete everything inside this tag. You’ll end up with the following code:

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}">
</Grid>

The design mode view should show a blank screen at this point.

Creating Your Application’s Layout

You now need to add the UI for your application. Our application consists of a single button that will rotate around a grid. The grid is 2×2, so let’s go ahead and declare that the layout grid should have two rows and two columns. Change your layout grid markup to the following:

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
</Grid>

Adding the Button

After you’ve defined your layout grid, it is time to create the button that will be tapped by your users. You want the button to start in the upper left box of the grid, so you’ll declare that it goes in Row 0 and Column 0.

In XAML, you tell an element to place itself within a grid by declaring the element and then assigning it the appropriate row and column indices. Inside of your layout grid, add a button using the following markup:

<Button
    Grid.Column="0"
    Grid.Row="0"
    Content="Tap Me!"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    />

This markup says the button should position itself in column and row 0, should stretch both horizontally and vertically within the grid cell it occupies, and that it should have the text “Tap Me!”

Your application’s UI code should now resemble the following:

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneBackgroundBrush}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button
        Grid.Column="0"
        Grid.Row="0"
        Content="Tap Me!"
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        />
</Grid>

At this point, you can fire up your application and see that your UI is in place. Just like earlier, press CTRL+F5 to compile and launch your application in the Windows Phone emulator. If you’ve done everything correctly, you’ll see a button in your emulator nested firmly in the top left corner.

After completing the application's UI Windows Phone 7 Development

You can tap the button by clicking it with your mouse, but nothing will happen. We’ll remedy that next by adding some event handling code.

Adding Event Handling

Go back to Visual Studio and make sure that you’ve got MainPage.xaml open in design/code mode. You need to add an event handling attribute to the button that you’ve created. Modify the button declaration, adding a ManipulationStarted attribute. Make sure you allow Visual Studio to create an event handling function for you. Your button markup should now look like this:

<Button
    Grid.Column="0"
    Grid.Row="0"
    Content="Click Me!"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    ManipulationStarted="Button_ManipulationStarted" />

Now, open the code behind file for MainPage.xaml. It is named MainPage.xaml.cs and you can find it in the Solution Explorer by clicking the arrow next to MainPage.xaml.

Click the arrow next to MainPage.xaml to find the code behind file. Windows Phone 7 Development

Inside of MainPage.xaml.cs you’ll see the event handler that Visual Studio created. Visual Studio has most likely named it Button_ManipulationStarted and, for the purposes of this application, that should be just fine. Change your function declaration to the following:

private void Button_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
    Button b = sender as Button;
    int col = Grid.GetColumn(b);
    int row = Grid.GetRow(b);
    if (col == row)
    {
        Grid.SetColumn(b, ++col % 2);
    }
    else
    {
        Grid.SetRow(b, ++row % 2);
    }
}

The first thing this method does is create a new variable of type Button and assigns it a reference to the sender variable. This is important because Button derives from UIElement and the next few operations require a UIElement object as the parameter. Next, the method gets the current column and row index by using a static method of the Grid class. Finally, the method uses a simple algorithm to determine where to move the button and change the row or column index as appropriate. You know that if the row and column indices are the same, then the column needs to change. If the row and column indices are different then the row needs to changes. In either case you use another static method of the Grid class to set the button’s row or column. The appropriate index is determined using some simple math.

At this point, we’re done with the sample application. Press CTRL+F5 to compile and launch your application and then try clicking your button. You’ll see it move around the grid each time you click. Try using the emulator’s controls to change the phone’s orientation and you’ll see that the application adapts with no problems at all.

Learning More

We’ve only just touched on the subject of Windows Phone 7 development in this tutorial. While you’re now familiar with the basics of creating an application with Silverlight, you’ll likely want to learn more about XAML, Silverlight, XNA, and Windows Phone 7 development in general. Luckily, there are already several resources you can turn to:

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.aafrin.com Aafrin

    What are the hand phone that uses win 7 OS? I never came across one

    • http://www.nickohrn.com Nick Ohrn
      Author

      There are currently no handsets being shipped with Windows Phone 7 OS. However, the first units are expected to ship later this year with the Dell Lightning being eye opening amazing as far as hardware specs go. Engadget has some leaked photos: http://www.engadget.com/2010/04/21/dell-lightning

  • Reid

    Great tutorial. So happy to see a beginners tutorial that actual starts at the beginning! Hope to see alot more!

  • http://www.pixelrockit.com Bartosz Oczujda

    That's a good start, can't wait for something more advanced :-)

  • http://aoverton.com Austin Overton

    Top notch! This was an excellent introduction to Windows Phone 7 programming and really cleared up a lot of the basic issues that most other sites don't cover. It seems that if you know already know C# then WP7 development will be cake!

    • http://www.nickohrn.com Nick Ohrn
      Author

      If you know C# and are familiar with the latest UI interfaces that Microsoft has exposed with WPF and Silverlight, then Windows Phone 7 development should be really easy. The implementation of many common application types should be straightforward and I fully expect the application ecosystem to boom as .NET developers finally get into the mobile game.

  • Jules

    Mobile Tuts is on a roll! Here's hoping you guys look at WebOS (Palm Pre & Pixi) next. I'm sure the typical Tuts+ visitor would appreciate the use of HTML5, CSS and JavaScript to develop full featured mobile applications.

    Keep up the good work.

  • http://sproutventure.com Dan Cameron

    Great tut!

  • dragon noir

    Nice tuto :)

  • Sonia

    So happy to see a beginners tutorial that actualy starts at the beginning!!!…thanks you for that.

    But i am looking for tutorial to develop a calendar like in outlook for example

    I have created a Web application running from a web browser that is basically a tool for creating and viewing schedules … it's done!

    however, I must develop the read access to my schedule on one or more mobile devices (iPhone, Windows Smartphones, Blackberry, Android)

    can you help with tutorial or a good link about this ?

    P.S : I am sorry for my english, i am a french

  • Okeowo Aderemi

    Seriously waiting for the Introduction to Nokia Development though i know it still like to read Nettuts tutorials on coding simple to learn here and fun

  • David

    I understand why there aren't more Win7 tutorials, considering there aren't any phones to see this work on, but I would like to see this train continue to move. I'm also curious if this will actually be big or if it will implode like the Kin.

  • Pingback: Introduction to XNA on Windows Phone 7 | Windows Phone Secrets

  • Masaki Katata

    Greate Tutorial Thank You

  • aaran

    although everywhere you go the is a war between windows and mac i would love to develop for this platform on the mac, i develop for the iphone, android and would love to get to windows. i wish they thought of this.

  • I Write Code

    Excellent tutorial. Could you please update the download link for the required tools?

  • Mukesh

    Really very nice tutorial.

    The link http://bit.ly/9FXxQC says “page cannot be found!”, please provide the latest link, I am really exited to start development!

  • Girish

    Thanx, it was a wonderful tutorial for starter.

  • Navjot Singh

    Nice piece of Info about the basics which are not available on the other sites…..But the link given for downloading the Development kit is not working…..!

  • http://vadivel.blogspot.com Vadivel

    Thanks for a nice beginners tutorial. Btw both the links which you provided doesn’t seem to work at this time.

  • John Smith

    Hi,

    I have a requirement like how to place html page which contains the image and placed into the web browser control.

    Please help me with sample code.

    Bye
    John

  • http://www.sreerajms.webs.com Sreeraj

    Hi This is very nice one. I am just born in this field I need more more article to learn Windows mobile programming. I am very much interested in this field.
    Thanks,
    Sree

  • http://WindowsMobileApplicationDevelopment Windows Mobile Application Development

    Thanks for giving introduction about windows mobile 7 development. Yes, The window application for mobile device was release by Microsoft. It is known as SOS means smart operating system. Window application created using Silverlight or XNA framework. XNA generally use for creating game and Silverlight generally used for creating application like calculator or currency converter.

  • http://www.think360studio.com/ John

    Great tutorial.

  • Veyron

    Hello. I want to know, if possible, what kind of APIs we can use to develop applications to wm7. I’ve searched almost everywhere and i cant find any! Please help!
    Greetings

  • Shravanthi

    can sum1 provide more links like dis 1 about windows phone 7 app development

  • Rick

    If Microsoft want developers to take the platform seriously they need to open up a little. Without developers it’ll be another failed smartphone platform. Where are all the developers? Developing for the iPhone….on what? MACS!

    Therefor MS need to get their act together and release mac based development tools. I know I certainly wont be building anything for the WinPho7 as I cant see having to spend money on a PC as a good investment. I certainly wont make my money back when I sell the App as nobody’s using the platform yet!

    • sijju

      totally agree! they need to provide development tools for MAC OS X

  • Bob Richfield

    I’m trying to complete the tutorial Rotating_Button. First I tried to download the software from the site listed in the tutoral, The site apparently is no longer valid. Then I went directly to Microsoft and I thought I downloaded and installed all the necessary software. I then created a project as you indicated and made no modifications, just pressed ctrl/F5, the results were an error message ‘Zune software is not installed. Install the latest version of Zune software. Any help would be appreciated

    • Kiran Gandhi

      download zune software

  • vicky

    very helpful for a beginer like me, thanx alot

  • Pingback: Desenvolvimento Mobile | Bitmasters

  • Nilay

    Nice Tut,pal.

  • kaushal

    what is extension of this code? plz say me

  • http://WinPhoneFan.com Will Brown

    Here’s a great step by step video series on the Microsoft site for absolute beginners like me:

    http://channel9.msdn.com/Series/Windows-Phone-7-Development-for-Absolute-Beginners

    You don’t have to have ANY programming experience to start this tutorial, so it’s a great place to start if you want to learn.

  • Augusto

    Hey man thanks for the introduction !!! u did well :D

  • http://www.mobosurvey.com Tuan Bui

    Great article! Thank you.

  • Ivan

    Hi, can you tell me are those tools FREE of charge ?? Because it’s M$ products :D

    Great tutorial BTW and hope you’ll share more great examples with the world ;)

  • http://www.windowsphone7development.net windows phone 7 development

    I have been very enthusiastic in getting into Phone apps development . Really useful information is provided .I feel like I got a head start with this article . Thanks

  • Ewenike Chijioke

    Nice Tut!! Sure learned enough to get me started…

  • shamsi

    nice work ….
    now some advance …….

  • Arun

    Thanks.. You explained it well..
    It really helps a lot for a beginner like me.

    Thankx.

  • http://www.linkedin.com/in/prashantakerkar Prashant Akerkar

    Informative Article by Nick.

    Thanks & Regards,
    Prashant S Akerkar

  • Anonymous

    This is an excellent post.
    Very helpful for beginners to get started with something.
    Any helpful links for more tutorials or guides to easily understanf various classes and other stuff for windows phone?

  • Ajay

    Really a great Tut with detail explanation .. it is worthy for the newbie like me.

    Thanks,

  • http://goarticles.com/article/Developers-Are-Switching-to-the-C-Sharp-Platform-for-Better-Performance/7113926/ AdenaHewitt

    I wanted to thank you for this great read!! I definitely enjoying every
    little bit of it.I have you bookmarked to check out new stuff you post.

  • Divyanshu Joshi

    this help greatly on staring with windows 7 development, thanks:-)
    http://www.awrtechnologies.com

  • Rahul Aragade

    outdated :(

  • aruna
  • pranith

    itz a bit easy 2 understand!!!!!

  • pranith

    xpecting more xamples from u