devxlogo

Porting an iPhone Application to Windows Phone 7

Porting an iPhone Application to Windows Phone 7

By Rob Howard, Dan Maycock and Greg Martin

Developers of iPhone applications possess the fundamentals and key understanding for building applications for small form factor devices. When creating a mobile application for Windows Phone 7 however, the means for developing the application changes. This platform is a considerable departure from Microsoft’s previous mobile operating systems so this article will help describe what you’ll encounter porting an existing iPhone application to Windows Phone 7.

Whether it’s the difference between Objective-C vs .Net, or the iOS SDK vs the Windows Phone 7 SDK, a number of differences exist which will be explained throughout this article. We’re going to walk through our own process of porting an internal iPhone application, Slalom Cards, into a comparable version for Windows Phone 7. The goal of this article is not only provide you with the knowledge necessary to port an existing application to Windows Phone 7, but to also better understand the platform a whole for new development.

[login]

Resources

Windows Phone 7 applications are built and tested utilizing Visual Studio. All the tools to get started can be downloaded at http://developer.windowsphone.com as well as guides and tips to get up to speed with the Windows Phone 7 development environment.

As a note, the development environment DOES require the Windows Operating System (Windows Vista or Windows 7). Also, the emulator does not seem to run well in a VM (like Parallels or Fusion), so it will be necessary to boot directly into Windows if you want to debug.

You will need to download the developer tools on this website. They are a free download and it includes the following:

    a. Visual Studio 2010 Express for Windows Phone Beta

    b. Windows Phone Emulator Beta

    c. Silverlight for Windows Phone Beta

    d. Microsoft Expression Blend for Windows Phone Beta

    e. XNA Game Studio 4.0 Beta

    f. The UI Design and Interaction Guide for Windows Phone 7 can be found linked on the Windows Phone developer site at http://go.microsoft.com/?linkid=9713252

Construction of the Application: The Easy Things

Migration of the Data Model

Since the languages support similar features (both object-oriented c-like languages that support things like properties and methods) porting most of the model items will be pretty straightforward with a one to one mapping of fields and methods. Note: C# is currently the only supported programming language for Development for Windows Phone 7.

Networking

Windows Phone 7 allows the developer to use Windows Communication Foundation (WCF), HttpWebRequest, and WebClient.

WCF works very well with SOAP web services. It is very simple to set up a connection that would generate all of the networking code and object classes that it uses.

For RESTful services, WCF is also very useful but we will have to do a little bit more work deserializing the data into objects. There are many ways to do this; .Net provides some simple DataContract Serializers and Deserializers that work well with XML and JSON.

User Interface Elements

Windows Phone 7 provides a few ways to author and edit the user interface.

One can use the preview pane within the IDE for layout and addition to controls. This is very similar to the Interface Builder application that is part of the iOS SDK. Users of Interface Builder will become easily familiar with this method of UI authoring and manipulation.

Also in the IDE is a view of the XAML that can be very useful for a developer to see and modify. This code behind the presentation is an easy way to see the output of the UI layout engine, and gives a chance to add function handlers, custom properties on classes, and to fine tune your code.

There is also a version of Expression Studio for the Windows Phone. Like Expression for web based Silverlight applications or WPF implementations, this provides a more designer focused interface which allows you to creating templates, behaviors, animations, and many other custom UI.

Page Hierarchy

Like most mobile phone operating systems, Windows Phone 7 provides a stack based navigation system along with application buttons that can be used, like in the iPhone, as a tabbed based navigation system. The API provides NavigatedTo and NavigatedFrom methods that are similar to the ViewDidAppear, and ViewDidDisappear methods that the iPhone supports.

The Difficult or Not So Clear Things

This is a list of things that aren’t as simple as the first list. Differences in hardware, operating systems, and API are the contributing factor to getting an item in this list.

Data storage

Unlike previous versions of Windows Mobile operating systems, Windows Phone 7 currently does not have an API for a client-side database which developers can use.

What is provided is access to isolated data storage where your application can save settings or files. Here you can roll your own data storage mechanism or use a third party database with the caveat that an application can only access its own data. This is very similar to those familiar with iPhone development and is another break away from previous versions of Windows Mobile operating systems.

A resource video for developing occasionally connected applications can be found here: http://www.msteched.com/2010/NorthAmerica/WPH306

Page Transitions

While the navigation between pages in the Windows Phone 7 environment is similar to iPhone, the familiar animation transitions are not left up to the developer to implement.

Built-in screen transitions and animations are system-reserved and developers cannot access them but may mimic them. If developers want to implement transitions or animations within their application, they must use Silverlight or the XNA Framework to create them (see page 64 of the UI Design guide).

Transitions within your application must be implemented on your own.

Contrary to the iPhone, there currently isn’t a defined standard for the types of transitions one should use.

A Channel9 video of implementation can be seen here: http://channel9.msdn.com/posts/SlickThought/Simplify-Page-Transitions-in-Windows-Phone-7-Silverlight-Applications/

Windows Phone 7 Has Themes

There is a large selection of highlight colors including light and dark for background colors.

One must be aware that the user could have any one of those themes set for their phone. The application should be aware of the settings and use them throughout the user experience.

Background images and UI elements presented above them should take in to account the possibility of the different themes. For example, if you use a particularly dark image for a background, the light theme with black button borders may not show up very well against the image. Setting the opacity of the image to show some of the white background is one solution to this problem.

Digging Deeper

The following are some examples that we used in our Applications.  We chose these examples because we felt that they are things that aren’t obvious to most developers and take some research to get a solution.  We also feel that they are things that pretty much any application for the phone would need. In our samples, we try to find a simple solution to the problems while providing the best user experience.

Porting the Data Model

The following is simple port of a small class from objective-c to c#.  This would be part of a class that would represent the data behind user interface elements.  It could be created in several different ways, like being fetched from storage, an external api, or generated by the application at runtime.

Person.h

@interface Person : NSObject {}@property (nonatomic, retain) NSString *firstName;@property (nonatomic, retain) NSString *lastName;@property (nonatomic, retain) NSString *office;@end

Person.m

#import "Person.h"@implementation Person@synthesize firstName;@synthesize lastName;@synthesize office;-(void)dealloc{    [firstName release];    [lastName release];    [office release];    [super dealloc];}@end

Person.cs

public class Person{	public String FirstName { get; set; }	public String LastName { get; set; }	public String Office { get; set; }}

C# also provides an easy way to port Observable objects. The following code is an update of the person class to notify observers of updates to the data.

public class Person : INotifyPropertyChanged{    private String firstName;    private String lastName;    private String office;    public event PropertyChangedEventHandler PropertyChanged;    private void NotifyChanged(String changeProperty)    {        if (PropertyChanged != null)        {            PropertyChanged(this, new PropertyChangedEventArgs(changeProperty));        }    }    public String FirstName    {       get        {            return firstName;        }        set        {            if (value != firstName)            {               firstName = value;                NotifyChanged("FirstName");            }        }    }    ...}

Navigation

Moving from page to page in your application is different than iOS, but fairly simple.  Instead of pushing and popping view controllers to your scene, the navigation system behaves more like a browser where you navigate forward and back through the pages of the application.  Also, data is passed to the pages through the querystring where the navigating page can read and perform operations based on the input.

A typical navigation operation in iOS.

PersonListViewController * personList = [[PersonListViewController alloc] initWithNibName:@"PersonListView" bundle:nil];[[self navigationController] pushViewController: personList animated:YES];[personList release];

Doing the same in code on Windows Phone 7.

private void HyperlinkButton_Click(object sender, RoutedEventArgs e){    NavigationService.Navigate(new Uri("/PersonListPage.xaml?office=National", UriKind.Relative));}

A different way, with XAML.

Reading passed in values on the new page.

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e){    base.OnNavigatedTo(e);    PersonList people = CardsDatabase.Instance.PersonListTable;    String office;    if (NavigationContext.QueryString.TryGetValue("office", out office) == false)    {        office = "National"; // default value    }}

Navigating back to the previous page is either done with the user clicking on the back hardware button or you can programmatically perform this with the following code:

NavigationService.GoBack();

Data Storage

Our application persists data by taking advantage of data serialization, isolated storage, and LINQ to fetch, sort, and filter the data. A resource video for developing occasionally connected applications that was the inspiration for the example can be found at: http://www.msteched.com/2010/NorthAmerica/WPH306

First Create a Singleton for Database Management.

public class CardsDatabase{    private PersonList _personListTable = null;    private CardsDatabase()    { }    private static CardsDatabase staticDB = new CardsDatabase();    public static CardsDatabase Instance    {        get        {          return staticDB;        }    }

Then Create the Table members of the Database Class that will fetch the data, (in the same CardsDatabase class).

    private PersonList personListTable = null;    public PersonList PersonListTable    {        get        {            if (personListTable == null)            {                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())                {                    Stream stream = null;                    if (store.FileExists(personListTable))                    {                        stream = store.OpenFile(personListTable, FileMode.Open);                        using (stream)                        {                            // Read the personList object from the stream                            XmlSerializer ser = new XmlSerializer(typeof(PersonList));                            personListTable = (PersonList)ser.ReadObject(stream);                        }                    }                }            }            return personListTable;        }    }}

Finally, create a LINQ query to fetch the data as your application needs it:

PersonList people = CardsDatabase.Instance.PersonListTable;			ListBox.ItemsSource =			from p in people			where p.Office == "Seattle"    		orderby p.FirstName, p.LastName			select p;

Final Thoughts

There are many things with Windows Phone 7 that an iOS developer will find familiar and comfortable with, but a few things that have a bit of a learning curve.  Since the capabilities of the devices are so similar, much of the functionality of an application can be mirrored from iOS to WP7. With that, they are two different worlds, so there is still some work and design considerations associated with your first ported application. We hope this article will help clarify some of these differences, and will help you to develop windows phone 7 applications in a more efficient, streamlined, manner.

About the Team

Rob Howard is an experienced software engineer and engineering lead. He has ten years of industry experience with published mobile applications for several platforms including iPhone, WebOS, and Blackberry.

Dan Maycock is a technology solutions consultant, helping advise fortune 500 companies on how to work with new and emerging technologies, primarily focused around mobile.

Greg Martin has been a consultant at Slalom Consulting for over 7 years and is currently the mobility development lead.

Slalom Consulting

Slalom Consulting is a business and technology consulting firm that helps clients win by building local teams with a deep understanding of the art and science of business success. Slalom drives client ROI in areas such as cloud computing, business intelligence, portals, mobility, project management and process design. Slalom's consultants are handpicked experts who stay ahead of the curve by always finding the next innovative advantage. Headquartered in Seattle, Slalom employs more than 900 consultants across nine cities in the United States.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist