Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Porting an iPhone Application to Windows Phone 7 : Page 3

There are many things with Windows Phone 7 that an iOS developer will find familiar and be comfortable with, but a few things have a learning curve.


advertisement

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.

<HyperlinkButton Content="Seattle" NavigateUri="/PersonListPage.xaml?office=Seattle" /> <HyperlinkButton Content="Los Angeles" NavigateUri="/PersonListPage.xaml?office=Los Angeles" />

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap