Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Programming Windows Phone 7 – Hello World

, 9 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple Windows Phone 7 application

I was really excited to see the upcoming release of Windows Phone 7. This is truly a great answer from Microsoft to the rest of world. Apple iOS which is currently the most popular mobile platform, Google Android, BlackBerry, etc. were taking pace with the new technologies and Microsoft was still battling to find a space in the mobile world. Of course, Windows Mobile operating system was popular some time before but the smart phones were not really popular and on aging, the platform itself is being outdated in front of other new age platforms.

All programs for Windows Phone 7 can be written in .NET managed code. Currently C# is the only programming language supported. The tools for Windows Phone 7 can be downloaded freely from Windows Phone 7 website. It includes:

  • Visual Studio 2010 Express Edition
  • XNA Game Studio 4.0
  • On Screen Phone Emulator

Windows Phone 7 can be programmed mainly using XNA FrameWorks and the popular Silverlight platform. XNA is used for high performance games (usually 3D games) and Silverlight is usually used for the 2D graphical applications.

Windows Phone 7 contains the stripped down version of Silverlight 3. Microsoft has avoided the Silverlight features which are not really compatible with the Windows Phone 7 platform. The animations for the programs can be down using the Microsoft Expression Blend.

Ultimately, it is the programmer’s responsibility to choose which platform should be used for programming Windows Phone 7 application.

Windows Phone 7 contains the Azure services to access the cloud service. One of the examples is XBox Live (runs on cloud). Programs are location aware and can access data through Bing, social networking websites, etc.

Windows Phone 7 features Multi-Touch screen with 3 navigation buttons. Back (like the back button in the browser. This will terminate the application), Start button (to navigate to home screen), Search. Currently the native resolution of a Windows Phone hardware is 400×800. Also 320×480 resolution screen is expected. The hardware component features are:

  • Wi-Fi
  • Camera
  • Accelerometer – to detect the movement of the phone
  • GPS based Location
  • Vibration (programmable)
  • FM Radio
  • Push notifications

Once after downloading the tools, you can either start programming in the Visual Studio Express Edition or Visual Studio 2010 (if it’s already installed).

Here I’m demonstrating a basic Windows Phone 7 application in Silverlight. The code is only tested in Simulator not on any real hardware.

The basic layouts will automatically be created using the XAML and the corresponding C# source will also be created. Notice that the standard controls available in the Windows phone 7 are not similar to the controls available in Windows. They’re transparent and designed for the phone. The entire theme is based on black. You can simply start debugging/execute the code using the emulator available. It’s better not to close the emulator Windows frequently as it may take time to start it up. The program will automatically be deployed and starts in the emulator.

The user actions like home screen, back button, search, etc. will end up the application by default. The application must manage itself to restore the previous state if necessary on quit. On startup, Windows phone 7 emulator will ask for the basic setup. It’s very easy to set it up and this is a one time process. The emulator can be used for changing the orientation between portrait and landscape. The buttons are available on the right top corner of the emulator window on hovering the mouse.

The demo application loads the posts from this blog and displays it, with tap on the post titles listed, the article will be loaded on webBrower.

Loading the Feed Items

The Silverlight application had put a lot of restrictions in accessing the APIs and resources of other websites. Loading news feed (RSS/Atom) is not really painful using the C# code. Silverlight supports asynchronous read operations for web requests. SyndicationFeed class is not supported by default. Basically, it’s safe to add this assembly to the project (System.ServiceModel.Syndication.dll). You can browse to Windows SDK folder, locate and add the same assembly in the “Client” folder.

private void buttonRefresh_Click(object sender, RoutedEventArgs e)
{
    // Load the feed items
    LoadFeedItems();
}

private void LoadFeedItems()
{
    // Asynchronously load the feed content.
    WebClient client = new WebClient();
    Uri address = new Uri("http://feeds.feedburner.com/sharingmythoughts");
    client.OpenReadCompleted += client_openReadComplete;
    // Add the callback on completion
    client.OpenReadAsync(address);
    webBrowser.Visibility = Visibility.Collapsed;
    buttonBack.Visibility = Visibility.Collapsed;

}
private void client_openReadComplete(object sender, OpenReadCompletedEventArgs args)
{
    try
    {
        listBoxPosts.Items.Clear();
        // try to load the result in the XML reader.
        // Exception may occur if the request is failed
        XmlReader reader = XmlReader.Create(args.Result);

        // Syndication Feed is not supported by default.
        // It's safe to add this assembly.
        SyndicationFeed feed = SyndicationFeed.Load(reader);
        Items = feed.Items.ToArray();
        foreach (SyndicationItem e in Items)
            listBoxPosts.Items.Add(e.Title.Text);
    }
    catch
    {
        MessageBox.Show("Error downloading feed");
    }
}

Navigate to the Base URL on Tapping

private void OnTapItem(object sender, MouseButtonEventArgs e)
{
    if (listBoxPosts.SelectedIndex >= 0)
    {
        buttonRefresh.Visibility = Visibility.Collapsed;
        buttonBack.Visibility = Visibility.Visible;
        webBrowser.Visibility = Visibility.Visible;
        Uri address = new Uri(Items[listBoxPosts.SelectedIndex].Id);
        webBrowser.Navigate(address);
    }
}

The mobile theme I’ve enabled for this blog is not really working well. The WPTouch theme I installed seems to not really identify Windows Phone 7 user agent string. (Amazingly, it works well when I load using Intenet Explorer App in the Phone 7.)

Adding Support for Orientation

By default, the wizard creates the application with Portrait Orientation. The page content will not be arranged according to the current orientation if we don’t add support for it. Change the orientation to Portrait/Landscape/PortraitOrLandscape in the XAML file:

SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"

Handling Orientation Events

It’s possible to override OnOrientationChanged function do if we need to manage something else other than the default orientation change support implemented in the base class. For the simple applications using controls, we can use the standard alignment support provided by the grids and panels. The controls will be perfectly aligned according the vertical and horizontal settings of the controls.

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox Margin="0" x:Name="listBoxPosts" MouseLeftButtonUp="OnTapItem" />
    <phone:WebBrowser Margin="0" x:Name="webBrowser" />
</Grid>
<Button Content="Refresh" Height="71" HorizontalAlignment="Right" 
	Margin="320,145,0,0" x:Name="buttonRefresh" VerticalAlignment="Top" 
	Width="160" Click="buttonRefresh_Click" />
<Button Content="Back" Height="71" HorizontalAlignment="Left" Margin="0,145,0,0" 
	x:Name="buttonBack" VerticalAlignment="Top" Width="160" 
	Click="buttonBack_Click" />

Override the OnOrientationChanged if more flexibility is necessary:

protected override void OnOrientationChanged(OrientationChangedEventArgs args)
{
    // do your code. If base is not called, then the orientation will not be changed
    base.OnOrientationChanged(args);
}

Putting It All Together

OnClosing()

I feel Windows Phone 7 is a solid platform. It uses the modern programming languages and technologies. C# is one of the most popular languages and follows object painless oriented programming. The language, features and libraries are solid and vast. It's easy for programmers to learn and develop applications.

Apple’s iOS platform is truly a solid and great foundation, but the object C has a slight learning curve. It’s obvious because it’s my father’s programming language, not mine. But Apple made the platform truly solid and cash cow for the developers. So nobody really cared whether it’s his father’s or his. :)

Microsoft has chosen the right technologies for Windows Phone 7 is proven and popular. XNA, Silverlight with C# will surely give an edge for Microsoft to get a pace on their platform. Also the developer tools are easy to work with and available for free.

Charles Petzold is working for a free version of a book for Programming Windows Phone 7 which is expected to out soon. Apple has a definite documentation, video tutorials and lot more stuff to help the developers. The MSDN documentation is slightly noisy with too many languages technologies. Even if we check the documentation for Silverlight, some of the functionalities may not work under Windows Phone 7. Hopefully we can see lot of good stuffs in MSDN and Channel 9 for Windows Phone 7 (Channel 9 offers Windows Phone 7 Training Kit now). Now I’m exploring more about deploying the phone applications. See you soon.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sarath C
Technical Lead
India India
Software Developer
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 3 PinmemberHarii_M21-Oct-10 2:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 9 Oct 2010
Article Copyright 2010 by Sarath C
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid