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

Tagged as

(untagged)

Developing Windows Phone 7 HTML5 apps with PhoneGap

, 30 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This article show the step-by-step development of a Windows Phone 7 HTML5 application using PhoneGap. It also looks at how viable this approach is for cross-platform mobile development. Introduction … and Why HTML5? Windows Phone 7 allows native application development in both Silverlight and

This article show the step-by-step development of a Windows Phone 7 HTML5 application using PhoneGap. It also looks at how viable this approach is for cross-platform mobile development.

Introduction … and Why HTML5?

Windows Phone 7 allows native application development in both Silverlight and XNA, both of which are mature framework with excellent tool support. So why would you want to develop an application with HTML5 / JavaScript instead? Personally I think the only viable reason for doing this (other than just for fun!) is to develop a cross-platform mobile application. HTML5 / JavaScript applications are platform agnostics, running on Android, iPhone, BlackBerry and now with the Mango WP7 supporting IE9, WP7.
In this blog post I will describe how I implemented the application shown below, which allows you to search properties for sale, using the GPS and various webservices to find and geocode your current location:

<iframe src="http://www.youtube.com/embed/bpCo4O69WPs" frameborder="0" width="560" height="315"></iframe>

The basic concept behind HTML5 applications is that your native application is simply a full-screen WebBrowser, which hosts your JavaScript application logic. In order to achieve this in practice, you need to package the HTML, JavaScript, CSS and other resources into a XAP file, then use the WebBrowser APIs to pass messages between your JavaScript code and the native APIs in order to access the phone features such as the camera, accelerometer etc …

A few people have developed frameworks to assist in development of HTML5 apps, for example the HTML App Host Framework. However, the one I would recommend is PhoneGap, as the name implies, this framework fills the gap between your JavaScript code and the native phone features. PhoneGap is an open source project that has been running for a couple of years now; currently a large range of phones are supported, meaning that you are guaranteed the same JavaScript APIs for accessing the phone features on Android, iPhone, BlackBerry and more.

PhoneGap support for Windows Phone 7 was initiated by Matt Lacey, who created an initial implementation of the PhoneGap APIs. More recently Nitobi (who run PhoneGap), announced an official beta release, which they have worked on in conjunction with Microsoft.

Getting Started With PhoneGap

PhoneGap has a project template which will help you create an initial “Hello World” style application. You can get started by following these steps:

  1.  Download the WP7 PhoneGap from https://github.com/phonegap/phonegap-wp7
  2.  Copy the file GapAppStarter.zip, which contains the project template, to the folder
    •  \My Documents\Visual Studio 2010\Templates\ProjectTemplates\
  3.  Create a new project using the GappAppStart project template. I could not locate this template within the tree of ‘Installed Templates’, so used the search function. See the image below.
  4.  Add the framework\WP7GapClassLib.csproj project to your solution and add a reference to this project.
  5. Build and run!

If you followed these steps correctly, you should see the following:

Dissecting the Example App

The example application is a bit more complex than an equivalent Silverlight ‘Hello World’ app, so we’ll look at it in some detail …

The www folder contains the HTML5 application sourcecode, here you place images, HTML, JavaScript, and CSS. These files are marked as ‘content’ and will be included within the XAP file with the same directory structure. The project contains a GapSourceDictionary.xml file, this XML file lists all the HTML application resources. When the application starts, this XML file is read, and each file is added to isolated storage so that it can be served by the WebBrowser control:

<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><GapSourceDictionary<span style="color: #000000; font-weight: bold;">></span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><FilePath</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">"www/index.html"</span> <span style="color: #000000; font-weight: bold;">/></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><FilePath</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">"www/phonegap.1.0.js"</span> <span style="color: #000000; font-weight: bold;">/></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><FilePath</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">"www/master.css"</span> <span style="color: #000000; font-weight: bold;">/></span></span> 
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></GapSourceDictionary<span style="color: #000000; font-weight: bold;">></span></span></span>

The MainPage.xaml, which is the Silverlight UI for the application contains an instance of PGView:

<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><phone:PhoneApplicationPage</span> </span>
<span style="color: #009900;">    ... <span style="color: #000066;">xmlns:my</span>=<span style="color: #ff0000;">"clr-namespace:WP7GapClassLib;assembly=WP7GapClassLib"</span><span style="color: #000000; font-weight: bold;">></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><Grid<span style="color: #000000; font-weight: bold;">></span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><my:PGView</span><span style="color: #000000; font-weight: bold;">/></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></Grid<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></phone:PhoneApplicationPage<span style="color: #000000; font-weight: bold;">></span></span></span>

PGView is defined in the WP7GapClassLib, it is a UserControl which hosts a WebBrowser, and contains the code that bridges between the PhoneGap JavaScript APIs and the WP7 .NET APIs. On startup it will load your files into isolated storage and navigate the browser control to “www/index.html” (although this will probably be configurable in future).

The index.html file from the template project is shown below (minus a few un-important parts):

<!doctype html>
<html>
  <head>
    <meta name="viewport" content="width=320; user-scalable=no" />    
      <script type="text/javascript" charset="utf-8" src="phonegap.1.0.js"></script>       
      <script type="text/javascript">
        function init()
        {
          document.addEventListener("deviceready",onDeviceReady,false);
        }
 
        // once the device ready event fires, you can safely do your thing! -jm
        function onDeviceReady()
        {
          document.getElementById("welcomeMsg").innerHTML += "PhoneGap is ready!";
        }
      </script>
 </head>
 <body onLoad="init();">
  <h1>Hello PhoneGap</h1>
  <div id="welcomeMsg"></div>
 </body>
</html>

The important parts to note here are, firstly the viewport metadata, which relates to the complications that small mobile screens present to layout, resulting in the need for a separate layout viewport and visual viewport, as described in the article “a tale of two viewports“. Here the width is set to 320px, so each pixel-width in our HTML is two pixels on screen. I prefer one-to-one correlation, so would change this to the physical phone dimensions. The “user-scalable=no” should prevent the user from being able to pinch-zoom the page, unfortunately on my (mango) phone, this doesn’t seem to be working. This is a shame, because it really spoils the illusion of this being an application rather than a web-page.

When the DOM is loaded, the init function is invoked, which adds a handler for the deviceready event. When this has been fired, it is safe to use the PhoneGap APIs. This example does not actually use any of the APIs, but you can make a simple change to get the phone name via device.name for example to verify that PhoneGap really is working.
So how does the JavaScript / Silverlight bridge work? The PhoneGap.js file communicates via window.external.Notify, which raises the ScriptNotify event within the WebBrowser control. Encoded strings are sent to the PGView control which uses the command pattern to identify the class which performs the required function, and executes it.

Creating the Property Search Application

Following the principles of Unobtrusive JavaScript, where presentation is separated from logic, the I have moved application logic into a file propertySearch.js (adding this to GapSourceDictionary.xml of course). The HTML has the following markup, a simple form that can be used to input the string to search for:

<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><h1</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"welcomeMsg"</span><span style="color: #000000; font-weight: bold;">></span></span>Find A Home<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></h1<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><form</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"form"</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">"#"</span><span style="color: #000000; font-weight: bold;">></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text"</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"searchText"</span><span style="color: #000000; font-weight: bold;">/></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"submit"</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"searchButton"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"Go"</span> <span style="color: #000000; font-weight: bold;">/></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"button"</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"getLocationButton"</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">"My Location"</span><span style="color: #000000; font-weight: bold;">/></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></form<span style="color: #000000; font-weight: bold;">></span></span></span>

The event handlers are wired up as follows:

$<span style="color: #009900;">(</span>document<span style="color: #009900;">)</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">(</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"deviceready"</span><span style="color: #339933;">,</span> onDeviceReady<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
 
<span style="color: #006600; font-style: italic;">// phonegap is initialised</span>
<span style="color: #003366; font-weight: bold;">function</span> onDeviceReady<span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  <span style="color: #006600; font-style: italic;">// verify that the phone is ready!</span>
  $<span style="color: #009900;">(</span><span style="color: #3366CC;">"#welcomeMsg"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">append</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"..."</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
 
  $<span style="color: #009900;">(</span><span style="color: #3366CC;">"#form"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">(</span>searchProperties<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">(</span><span style="color: #3366CC;">"#getLocationButton"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">click</span><span style="color: #009900;">(</span>getGeolocation<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>

Note, I have also added jQuery to the project. The code which appends an ellipsis (…) to the title is a subtle indication that the deviceready event has fired. I found when using the emulator that often the JavaScript code within the page was not being executed at all, this seems to have nothing to do with the PhoneGap JavaScript code. Fortunately the code is always executed when deployed to a real device.

The search properties function is a simple AJAX call to the Nestoria APIs, using the jQuery ajax() function, which manages the JSONP process of dynamically adding a script tag to the DOM which fetches the required data.

<span style="color: #003366; font-weight: bold;">var</span> propertyTemplate <span style="color: #339933;">=</span> $<span style="color: #009900;">(</span><span style="color: #3366CC;">"#propertyTemplate"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">template</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
 
<span style="color: #006600; font-style: italic;">// searches for properties based on the current search text</span>
<span style="color: #003366; font-weight: bold;">function</span> searchProperties<span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>      
  <span style="color: #003366; font-weight: bold;">var</span> query <span style="color: #339933;">=</span> <span style="color: #3366CC;">"http://api.nestoria.co.uk/api?"</span> <span style="color: #339933;">+</span>
                  <span style="color: #3366CC;">"country=uk&pretty=1&action=search_listings&encoding=json&listing_type=buy"</span> <span style="color: #339933;">+</span>
                  <span style="color: #3366CC;">"&place_name="</span> <span style="color: #339933;">+</span> $<span style="color: #009900;">(</span><span style="color: #3366CC;">"#searchText"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">val</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  $.<span style="color: #660066;">ajax</span><span style="color: #009900;">(</span><span style="color: #009900;">{</span>
    dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">"jsonp"</span><span style="color: #339933;">,</span>
    url<span style="color: #339933;">:</span> query<span style="color: #339933;">,</span>
    success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">(</span>result<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
 
      <span style="color: #003366; font-weight: bold;">var</span> resultsContainer <span style="color: #339933;">=</span> $<span style="color: #009900;">(</span><span style="color: #3366CC;">"#resultsContainer"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
      resultsContainer.<span style="color: #660066;">empty</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
      $.<span style="color: #660066;">each</span><span style="color: #009900;">(</span>result.<span style="color: #660066;">response</span>.<span style="color: #660066;">listings</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">(</span>index<span style="color: #339933;">,</span> property<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
        <span style="color: #003366; font-weight: bold;">var</span> $itemNode <span style="color: #339933;">=</span> $.<span style="color: #660066;">tmpl</span><span style="color: #009900;">(</span>propertyTemplate<span style="color: #339933;">,</span> property<span style="color: #009900;">)</span>.<span style="color: #660066;">data</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"propertyData"</span><span style="color: #339933;">,</span> property<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
        $itemNode.<span style="color: #660066;">appendTo</span><span style="color: #009900;">(</span>resultsContainer<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">}</span>
  <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
 
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>

NOTE: The application uses the Nestoria UK APIs, so if you are trying it out in your emulator, set your location to somewhere in the UK!

I am using a jQuery templates to render each item. You can render an array of items via the tmpl() function, however, here I render each one individually, so that I can associate the raw JSON data for each property with the generated element via the jQuery data() function. This is rather like defining the DataContext for a Silverlight FrameworkElement.

<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><script</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"propertyTemplate"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text/x-jquery-tmpl"</span><span style="color: #000000; font-weight: bold;">></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><li</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"property"</span> <span style="color: #000066;">onClick</span>=<span style="color: #ff0000;">"propertyClick(this); return false;"</span><span style="color: #000000; font-weight: bold;">></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"propertyContainer"</span><span style="color: #000000; font-weight: bold;">></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"thumbnailColumn"</span><span style="color: #000000; font-weight: bold;">></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">"${thumb_url}"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"thumbnail"</span><span style="color: #000000; font-weight: bold;">/></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"detailsColumn"</span><span style="color: #000000; font-weight: bold;">></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"title"</span><span style="color: #000000; font-weight: bold;">></span></span>${title}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"price"</span><span style="color: #000000; font-weight: bold;">></span></span>${price_formatted}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></li<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></script<span style="color: #000000; font-weight: bold;">></span></span></span>

When a property is clicked, the following function is invoked, which locates the clicked DOM element, then extract the JSON property details via the data() function described above. This is used in conjunction with another template to render the property details:

<span style="color: #006600; font-style: italic;">// handle clicks on properties</span>
<span style="color: #003366; font-weight: bold;">function</span> propertyClick<span style="color: #009900;">(</span>propertyNode<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
 
  <span style="color: #006600; font-style: italic;">// get the property</span>
  <span style="color: #003366; font-weight: bold;">var</span> property <span style="color: #339933;">=</span> $<span style="color: #009900;">(</span>propertyNode<span style="color: #009900;">)</span>.<span style="color: #660066;">data</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"propertyData"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
 
  <span style="color: #006600; font-style: italic;">// render the template</span>
  $<span style="color: #009900;">(</span><span style="color: #3366CC;">"#detailsContainer"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">empty</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  $.<span style="color: #660066;">tmpl</span><span style="color: #009900;">(</span>propertyDetailsTemplate<span style="color: #339933;">,</span> property<span style="color: #009900;">)</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"#detailsContainer"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
 
  <span style="color: #006600; font-style: italic;">// switch pages</span>
  showDetailsPage<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>

The template is shown here:

<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><script</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"propertyDetailsTemplate"</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">"text/x-jquery-tmpl"</span><span style="color: #000000; font-weight: bold;">></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div<span style="color: #000000; font-weight: bold;">></span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><h1<span style="color: #000000; font-weight: bold;">></span></span></span>${price_formatted}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></h1<span style="color: #000000; font-weight: bold;">></span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">"${img_url}"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"propertyImage"</span><span style="color: #000000; font-weight: bold;">/></span></span>			  
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><h2<span style="color: #000000; font-weight: bold;">></span></span></span>${title}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></h2<span style="color: #000000; font-weight: bold;">></span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"bedrooms"</span><span style="color: #000000; font-weight: bold;">></span></span>bedrooms: ${bedroom_number}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"bathrooms"</span><span style="color: #000000; font-weight: bold;">></span></span>bathrooms: ${bathroom_number}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"summary"</span><span style="color: #000000; font-weight: bold;">></span></span>${summary}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></script<span style="color: #000000; font-weight: bold;">></span></span></span>

The navigation between pages is achieved by having the markup for both pages present in the DOM:

<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><body<span style="color: #000000; font-weight: bold;">></span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"page searchPage"</span><span style="color: #000000; font-weight: bold;">></span></span>
     ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"page detailsPage"</span><span style="color: #000000; font-weight: bold;">></span></span>
    ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></body<span style="color: #000000; font-weight: bold;">></span></span></span>

The pages are styled to use absolute positioning:

div.<span style="color: #000000; font-weight: bold;">page</span> 
<span style="color: #00AA00;">{</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">480px</span><span style="color: #00AA00;">;</span>  
<span style="color: #00AA00;">}</span>

We can then show / hide these pages by simply animating their CSS left property:

<span style="color: #003366; font-weight: bold;">function</span> showSearchPage<span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  $<span style="color: #009900;">(</span><span style="color: #3366CC;">".detailsPage"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">(</span><span style="color: #009900;">{</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">480</span> <span style="color: #009900;">}</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
    $<span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">)</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">(</span><span style="color: #3366CC;">".searchPage"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">show</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">(</span><span style="color: #009900;">{</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span> <span style="color: #009900;">}</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span><span style="color: #339933;">;</span>
 
<span style="color: #003366; font-weight: bold;">function</span> showDetailsPage<span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  $<span style="color: #009900;">(</span><span style="color: #3366CC;">".detailsPage"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">show</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">(</span><span style="color: #009900;">{</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span> <span style="color: #009900;">}</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">(</span><span style="color: #3366CC;">".searchPage"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">(</span><span style="color: #009900;">{</span> left<span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">480</span> <span style="color: #009900;">}</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
    $<span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">)</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span><span style="color: #339933;">;</span>

It is worth noting that if the IE9 browser supported CSS3 transitions all of the above could have been done declaratively within CSS, unfortunately animations are not in the list of supported features. There is nothing wrong with jQuery animations, they have a very concise and simple syntax. However, CSS3 animation give the browser the option to use GPU acceleration, greatly improving performance. The webkit browsers on Android and iOS support this feature (using the –webkit prefix on the required CSS properties).

The current location is identified via navigator.geolocation.getCurrentPosition, using the Bing Maps REST APIs to geocode from a lat / long coordinate to a postcode:

<span style="color: #006600; font-style: italic;">// gets the current phone location</span>
<span style="color: #003366; font-weight: bold;">function</span> getGeolocation<span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  navigator.<span style="color: #660066;">geolocation</span>.<span style="color: #660066;">getCurrentPosition</span><span style="color: #009900;">(</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">(</span>position<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
    <span style="color: #006600; font-style: italic;">// geocode via Bing Maps</span>
    <span style="color: #003366; font-weight: bold;">var</span> apiKey <span style="color: #339933;">=</span> <span style="color: #3366CC;">"Ai9-KNy6Al-r_ueyLuLXFYB_GlPl-c-_iYtu16byW86qBx9uGbsdJpwvrP4ZUdgD"</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> query <span style="color: #339933;">=</span> <span style="color: #3366CC;">"http://dev.virtualearth.net/REST/v1/Locations/"</span> <span style="color: #339933;">+</span> position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">latitude</span> <span style="color: #339933;">+</span>
                    <span style="color: #3366CC;">","</span> <span style="color: #339933;">+</span> position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">longitude</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">"?jsonp=onGeocode&key="</span> <span style="color: #339933;">+</span> apiKey
 
    $.<span style="color: #660066;">ajax</span><span style="color: #009900;">(</span><span style="color: #009900;">{</span>
      dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">"jsonp"</span><span style="color: #339933;">,</span>
      url<span style="color: #339933;">:</span> query
    <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
 
<span style="color: #006600; font-style: italic;">// handle the geocode results</span>
<span style="color: #003366; font-weight: bold;">function</span> onGeocode<span style="color: #009900;">(</span>result<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  <span style="color: #006600; font-style: italic;">// extract the 'outward' part of the postcode</span>
  <span style="color: #003366; font-weight: bold;">var</span> postalCode <span style="color: #339933;">=</span> result.<span style="color: #660066;">resourceSets</span><span style="color: #009900;">[</span><span style="color: #CC0000;">0</span><span style="color: #009900;">]</span>.<span style="color: #660066;">resources</span><span style="color: #009900;">[</span><span style="color: #CC0000;">0</span><span style="color: #009900;">]</span>.<span style="color: #660066;">address</span>.<span style="color: #660066;">postalCode</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> codeSplit <span style="color: #339933;">=</span> postalCode.<span style="color: #660066;">split</span><span style="color: #009900;">(</span><span style="color: #3366CC;">" "</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">(</span>codeSplit.<span style="color: #660066;">length</span> <span style="color: #339933;">>

Metro-style

The standard style for HTML controls doesn’t look that great, however, with a simple bit of CSS it is possible to re-create something similar to the WP7 Metro styles. Here I have copied some of the properties that are present in the Silverlight resource dictionaries:

body<span style="color: #00AA00;">,</span> input<span style="color: #00AA00;">,</span> div
<span style="color: #00AA00;">{</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">22.667px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* PhoneFontSizeMedium */</span>
<span style="color: #00AA00;">}</span>
 
h2
<span style="color: #00AA00;">{</span>
  <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">normal</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">32px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* PhoneFontSizeLarge */</span>
<span style="color: #00AA00;">}</span>
 
h1
<span style="color: #00AA00;">{</span>
  <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">normal</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">42.667px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* PhoneFontSizeExtraLarge */</span>
<span style="color: #00AA00;">}</span>
 
input<span style="color: #00AA00;">[</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">"button"</span><span style="color: #00AA00;">]</span><span style="color: #00AA00;">,</span> input<span style="color: #00AA00;">[</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">"submit"</span><span style="color: #00AA00;">]</span>
<span style="color: #00AA00;">{</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">4px</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* PhoneBorderThickness */</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">25.333px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* PhoneFontSizeMediumLarge */</span>
<span style="color: #00AA00;">}</span>
 
input<span style="color: #00AA00;">[</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">"button"</span><span style="color: #00AA00;">]</span><span style="color: #3333ff;">:active</span><span style="color: #00AA00;">,</span> input<span style="color: #00AA00;">[</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">"submit"</span><span style="color: #00AA00;">]</span><span style="color: #3333ff;">:active
</span><span style="color: #00AA00;">{</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">}</span>
 
input<span style="color: #00AA00;">[</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">"text"</span><span style="color: #00AA00;">]</span>
<span style="color: #00AA00;">{</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">150px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">4px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">}</span>

ProgressBar

For a bit of fun I thought I’d try and create a HTML equivalent of the ‘trailing dots’ WP7 ProgressBar. With a simple bit of markup / CSS:

<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"progress"</span><span style="color: #000000; font-weight: bold;">></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"pip"</span><span style="color: #000000; font-weight: bold;">></span><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"pip"</span><span style="color: #000000; font-weight: bold;">></span><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"pip"</span><span style="color: #000000; font-weight: bold;">></span><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"pip"</span><span style="color: #000000; font-weight: bold;">></span><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;"><div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"pip"</span><span style="color: #000000; font-weight: bold;">></span><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;"></div<span style="color: #000000; font-weight: bold;">></span></span></span>
<span style="color: #6666ff;">.progress</span> div
<span style="color: #00AA00;">{</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>  
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">green</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">}</span>
 
<span style="color: #6666ff;">.progress</span>
<span style="color: #00AA00;">{</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">}</span>

And some further jQuery animations, this time making use of the easing plugin:

<span style="color: #003366; font-weight: bold;">function</span> onDeviceReady<span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  ...
 
  <span style="color: #006600; font-style: italic;">// create an animation loop for the progress bar;</span>
  startAnimation<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> tid <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">(</span>startAnimation<span style="color: #339933;">,</span> <span style="color: #CC0000;">3500</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
 
<span style="color: #003366; font-weight: bold;">function</span> startAnimation<span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  <span style="color: #003366; font-weight: bold;">var</span> delay <span style="color: #339933;">=</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">(</span><span style="color: #3366CC;">".pip"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">each</span><span style="color: #009900;">(</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
    animatePip<span style="color: #009900;">(</span>$<span style="color: #009900;">(</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">)</span><span style="color: #339933;">,</span> delay<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
    delay <span style="color: #339933;">+=</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
 
<span style="color: #003366; font-weight: bold;">function</span> animatePip<span style="color: #009900;">(</span>element<span style="color: #339933;">,</span> delay<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  element.<span style="color: #660066;">css</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"left"</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">)</span>
        .<span style="color: #660066;">hide</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
        .<span style="color: #660066;">delay</span><span style="color: #009900;">(</span>delay<span style="color: #009900;">)</span>
        .<span style="color: #660066;">show</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span>
        .<span style="color: #660066;">animate</span><span style="color: #009900;">(</span><span style="color: #009900;">{</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">240</span> <span style="color: #009900;">}</span><span style="color: #339933;">,</span> <span style="color: #009900;">{</span> duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> easing<span style="color: #339933;">:</span> <span style="color: #3366CC;">"easeOutSine"</span> <span style="color: #009900;">}</span><span style="color: #009900;">)</span>
        .<span style="color: #660066;">animate</span><span style="color: #009900;">(</span><span style="color: #009900;">{</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">480</span> <span style="color: #009900;">}</span><span style="color: #339933;">,</span> <span style="color: #009900;">{</span> duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> easing<span style="color: #339933;">:</span> <span style="color: #3366CC;">"easeInSine"</span> <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>

We get something which approximates the WP7 progress bar:

<iframe src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/progress.html" width="480" height="25" scrolling="no"></iframe>

(Note, sometimes the animation goes a bit crazy, try refreshing your browser to reset it!)

Again, my comments regarding CSS3 animations and GPU acceleration apply here also.

Tombstoning

As I have discussed in a previous blog post, tombstoning is a tricky task for WP7 developers. When your application is tombstoned you can save application state, page state – and the framework stores your back-stack URIs. It is your responsibility to re-start your application in the same state. Contrary to the belief of some developers I have talked to, Mango does not remove the need to tombstone, it just means that your application is likely to be tombstoned less often, with the suspended state being used to park your application while application switching.

So, how does tombstoning work with a PhoneGap HTML5 application? Good question! Implanting tombstoning would probably require some custom communication, outside of the PhoneGap APIs, that allows the JavaScript application to provide its current state to the Silverlight application that hosts this.

I would be very interested to hear from anyone who has solved this issue!

The Development Process

Attempting to develop JavaScript applications with the browser within the WP7 emulator is not a pleasant process. Any JavaScript errors, including simple parsing errors, typically result in the HTML being rendered but the scripts ignored. A much better approach is to run your HTML / JavaScript within a browser, this gives you access to the usual developer tools such as Firebug, or the built in Chrome / IE tools.

However, to do this, you need to mock the PhoneGap APIs. For my application, I found the following did the trick:

document.<span style="color: #660066;">addEventListener</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">(</span>evt<span style="color: #339933;">,</span> handler<span style="color: #339933;">,</span> capture<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  $<span style="color: #009900;">(</span><span style="color: #3366CC;">"body"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">(</span>evt<span style="color: #339933;">,</span> handler<span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span><span style="color: #339933;">;</span>
 
$<span style="color: #009900;">(</span>document<span style="color: #009900;">)</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">(</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  setTimeout<span style="color: #009900;">(</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
    $<span style="color: #009900;">(</span><span style="color: #3366CC;">"body"</span><span style="color: #009900;">)</span>.<span style="color: #660066;">trigger</span><span style="color: #009900;">(</span><span style="color: #3366CC;">"deviceready"</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">}</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>

OK, I know what you are thinking, what about the geolocation code? Where is the mock for these PhoneGap APIs? The answer is that a number of the PhoneGap APIs are designed to exactly match the corresponding HTML5 specification. So for example, the PhoneGap geolocation API is exactly the same as the HTML5 geolocation APIs. For phones such as WP7 where the browser supports this HTML5 feature, PhoneGap does nothing, for phones that do not, PhoneGap provides an implementation (using the native APIs).

If the PhoneGap mock is so simple, you might be wondering, why use PhoneGap at all? Well, PhoenGap is still giving us a mechanism for packaging files into a XAP file in such a way that they can be rendered by the browser.

How cross-platform is this approach?

I would say that this approach is probably about as cross-platform as any HTML5 / JavaScript browser based application. There are always going to be cross-browser differences to overcome. As a test, I ran this code on an iPod Touch without any modification. The results are pretty good, although there are some quirks visible:

<iframe width="480" height="360" src="http://www.youtube.com/embed/YGPw_0_5Fc0" frameborder="0" allowfullscreen></iframe>

How viable are HTML5 applications?

It is clear that there is a growing trend towards cross-platform HTML5 applications, and Microsoft seem to be in support of this concept (even though the JavaScript / HTML5 Metro applications within Win8 are not cross-platform!). Microsoft has worked with Nitobi to create the PhoneGap for WP7, it was also announced at the //build/ conference that Microsoft would be working with jQuery Mobile to create a Metro theme for their mobile controls.

Currently, PhoneGap for WP7 is in beta, and it is certainly a little rough around the edges. This will no doubt improve in time. The large list of applications written with PhoneGap, certainly indicate that this is a viable solution for application development.

A larger obstacle for WP7 HTML5 applications is the IE9 browser that runs in the Mango phone. Whilst it has a pretty impressive list of HTML5 features supported, there are a couple of browser features / issues that I cannot resolve. These make it obvious to any user that this is in fact a browser application, totally spoiling the ‘illusion’

  1. user-scalable=no – currently this setting seems to be ignored. This means that the user can pinch your application, which makes it feel like a web page.
  2. There doesn’t seem to be any way to turn of the gray shaded rectangle that appears over links when you click them. With applications that have dynamic content, this can look pretty ridiculous, with a gray rectangle lingering on screen while the page content changes underneath it. With the Android / iPhone browsers this can be turned off in CSS via -webkit-tap-highlight-color

Hopefully these limitations will be resolved. When they do, I am quite sure that HTML5 will be a viable technology for creating quality phone applications.

You can download the project sourcecode here: PhoneGapExample.zip

Regards, Colin E.

License

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

Share

About the Author

Colin Eberhardt
Architect Scott Logic
United Kingdom United Kingdom
I am CTO at ShinobiControls, a team of iOS developers who are carefully crafting iOS charts, grids and controls for making your applications awesome.
 
I am a Technical Architect for Visiblox which have developed the world's fastest WPF / Silverlight and WP7 charts.
 
I am also a Technical Evangelist at Scott Logic, a provider of bespoke financial software and consultancy for the retail and investment banking, stockbroking, asset management and hedge fund communities.
 
Visit my blog - Colin Eberhardt's Adventures in .NET.
 
Follow me on Twitter - @ColinEberhardt
 
-
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 5 PinmemberNemanja Trifunovic5-Jan-12 10:31 
QuestionIssue Pinmembermichelk5-Oct-11 21:23 
AnswerRe: Issue Pinmembernvthong3-Jul-12 21:52 
QuestionGood work Pinmembersumit_4palz30-Sep-11 21:43 

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
Web01 | 2.8.141015.1 | Last Updated 30 Sep 2011
Article Copyright 2011 by Colin Eberhardt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid