<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Colin Eberhardt&#039;s Adventures in .NET</title>
	<atom:link href="http://www.scottlogic.co.uk/blog/colin/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scottlogic.co.uk/blog/colin</link>
	<description>Colin Eberhardt&#039;s Adventures in .NET</description>
	<lastBuildDate>Thu, 19 Jan 2012 11:04:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>WP7 PhoneGap Backbutton Support Re-visited</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2012/01/wp7-phonegap-backbutton-support-re-visited/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2012/01/wp7-phonegap-backbutton-support-re-visited/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 11:03:11 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1950</guid>
		<description><![CDATA[About a month ago I published an article which demonstrated how to create a WP7 application using static HTML pages and PhoneGap. Whilst PhoneGap makes the packaging of HTML / JavaScript / CSS and images into a breeze, one thing it doesn&#8217;t do is provide correct back-button support. Correct back-button support is a mandatory requirement [...]]]></description>
			<content:encoded><![CDATA[
<p>About a month ago I published an article which demonstrated how to <a href="http://www.scottlogic.co.uk/blog/colin/2011/12/a-simple-multi-page-windows-phone-7-phonegap-example/" title="A Simple Multi-Page Windows Phone 7 PhoneGap Example">create a WP7 application using static HTML pages and PhoneGap</a>. Whilst PhoneGap makes the packaging of HTML / JavaScript / CSS and images into a breeze, one thing it doesn&#8217;t do is provide correct back-button support. Correct back-button support is a mandatory requirement for marketplace certification. Hitting the back button should navigate back through the various screens of an application. Hitting the back-button on the first screen should terminate the application. </p>
<p>The solution I published previously handles the WP7 back keypress in order to keep track of the back-stack depth. When the back-stack depth is just one, a back-button press exits the application. This works fine if backwards navigation is always controlled via the hardware back-button, however if your application has HTML anchor elements that navigate back to a previous page, or you use code such as <code>javascript:history.go(-1)</code>, this back-stack handling code will not detect that a backwards navigation has occurred.</p>
<p><em>As an aside, WP7 applications really should use the hardware back-button for navigation. If you are writing a cross-platform PhoneGap application consider adapting your UI for each platform. This means removing the back-button you woudl have in your iOS version when &#8216;skinning&#8217; for WP7.</em></p>
<p>In order to solve this issue I have updated the code to inspect the URL that the browser control is navigating to in order to detect backwards navigation. The class now maintains a list of URLs, which represent the navigation stack. This allows it to detect a backwards navigation.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Handles the back-button for a PhoneGap application. When the back-button</span>
<span style="color: #008080; font-style: italic;">/// is pressed, the browser history is navigated. If no history is present,</span>
<span style="color: #008080; font-style: italic;">/// the application will exit.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> BackButtonHandler
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> WebBrowser _browser<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> _backStack <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> BackButtonHandler<span style="color: #008000;">&#40;</span>PhoneApplicationPage page, PGView phoneGapView<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// subscribe to the hardware back-button</span>
    page<span style="color: #008000;">.</span><span style="color: #0000FF;">BackKeyPress</span> <span style="color: #008000;">+=</span> Page_BackKeyPress<span style="color: #008000;">;</span>
&nbsp;
    _browser <span style="color: #008000;">=</span> phoneGapView<span style="color: #008000;">.</span><span style="color: #0000FF;">Browser</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// handle navigation events</span>
    _browser<span style="color: #008000;">.</span><span style="color: #0000FF;">Navigated</span> <span style="color: #008000;">+=</span> Browser_Navigated<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Handle navigation in order to update our back-stack</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Browser_Navigated<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, NavigationEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> url <span style="color: #008000;">=</span> _browser<span style="color: #008000;">.</span><span style="color: #0000FF;">Source</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OriginalString</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// ensure all slashes are the same</span>
    <span style="color: #008080; font-style: italic;">// app\www/index.html</span>
    <span style="color: #008080; font-style: italic;">// see: https://issues.apache.org/jira/browse/CB-184</span>
    url <span style="color: #008000;">=</span> url<span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span>&quot;</span>, <span style="color: #666666;">&quot;/&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_backStack<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      _backStack<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>url<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">// check whether the URL represents a backwards navigation</span>
      <span style="color: #6666cc; font-weight: bold;">string</span> previousPage <span style="color: #008000;">=</span> _backStack<span style="color: #008000;">&#91;</span>_backStack<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>previousPage <span style="color: #008000;">==</span> url<span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        _backStack<span style="color: #008000;">.</span><span style="color: #0000FF;">RemoveAt</span><span style="color: #008000;">&#40;</span>_backStack<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Handle the hardware back-button</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Page_BackKeyPress<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, CancelEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// if we have items in the back-stack, route this event</span>
    <span style="color: #008080; font-style: italic;">// to the browser</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_backStack<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      _browser<span style="color: #008000;">.</span><span style="color: #0000FF;">InvokeScript</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;eval&quot;</span>, <span style="color: #666666;">&quot;history.go(-1)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      e<span style="color: #008000;">.</span><span style="color: #0000FF;">Cancel</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Note: There use of <code>url.Replace("\\", "/")</code>, this is due to a minor issue with the PhoneGap WP7 native code, which I have raised in the Callback JIRA (<a href="https://issues.apache.org/jira/browse/CB-184">CB-184</a>).</p>
<p>To use this code simply create an instance of the class, passing the PGView (the PhoneGap user control) into teh constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> MainPage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  InitializeComponent<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008000;">new</span> BackButtonHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, PGView<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Because back-button handling is a mandatory requirement for WP7 applications, hopefully Nitobi will incorporate this code (or similar) into the <a href="https://build.phonegap.com/apps">PhoneGap Build service</a> (which I tried earlier this week &#8211; it is pretty amazing!)</p>
<p>You can download a working example here: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/01/HTML5SandwichFlow1.zip'>HTML5SandwichFlow.zip</a></p>
<p>(The first three recipe pages have &#8216;back&#8217; anchor elements, two use the URL, one uses JavaScript)</p>
<p>Regards,<br />
Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2012/01/wp7-phonegap-backbutton-support-re-visited/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Windows Phone 7 &#8211; Browsing your Photos via Bing Maps</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2012/01/windows-phone-7-browsing-your-photos-via-bing-maps/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2012/01/windows-phone-7-browsing-your-photos-via-bing-maps/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 08:27:56 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[bing maps]]></category>
		<category><![CDATA[EXIF]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1925</guid>
		<description><![CDATA[The Windows Phone 7 camera gives you the option to record the location where a picture was taken (under Settings => applications => pictures+camera). With this feature turned on, each application has their latitude, longitude and altitude stored as part of the standard EXIF data. I thought it would be fun to combine the previous [...]]]></description>
			<content:encoded><![CDATA[
<p>The Windows Phone 7 camera gives you the option to record the location where a picture was taken (under Settings => applications => pictures+camera). With this feature turned on, each application has their latitude, longitude and altitude stored as part of the standard <a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format">EXIF data</a>. I thought it would be fun to combine the previous blog post I wrote on <a href="http://www.scottlogic.co.uk/blog/colin/2011/11/pushpin-clustering-with-the-windows-phone-7-bing-map-control/" title="Pushpin Clustering with the Windows Phone 7 Bing Map control">pushpin clustering</a> with the photos on my camera, to allow me to explore them via a Bing Maps control. With not much more than 100 lines of code I came up with an application which I think is a lot of fun to use. </p>
<table>
<tr>
<td width="300" >
<img style="margin:5px" src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/01/screenshot.jpg" alt="" title="screenshot4" width="300" height="188" class="aligncenter size-full wp-image-1931" />
</td>
<td width="300">
<img style="margin:5px" src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/01/screenshot2.jpg" alt="" title="screenshot4" width="300" height="188" class="aligncenter size-full wp-image-1931" />
</td>
</tr>
<tr>
<td>
<p  style="margin:5px">Here are all the photos on my phone, note the way the pushpins are clustered.</p>
</td>
<td>
<p  style="margin:5px">Here are a few pictures I took in New York, of the <a href="http://en.wikipedia.org/wiki/One_World_Trade_Center">One World Trade Centre</a> and the <a href="http://en.wikipedia.org/wiki/New_York_Stock_Exchange">Stock Exchange</a>.</p>
</td>
</tr>
<tr>
<td>
<img style="margin:5px" src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/01/screenshot3.jpg" alt="" title="screenshot4" width="300" height="188" class="aligncenter size-full wp-image-1931" />
</td>
<td>
<img style="margin:5px" src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/01/screenshot4.jpg" alt="" title="screenshot4" width="300" height="188" class="aligncenter size-full wp-image-1931" />
</td>
</tr>
<tr>
<td>
<p  style="margin:5px">Here are some pictures around Europe, including one of <a href="https://twitter.com/GergelyOrosz">Gergely Orosz</a> waiting for his turn in the Edinburgh Marathon Relay.</p>
</td>
<td>
<p  style="margin:5px">And finally, some pictures I took whilst running around <a href="http://en.wikipedia.org/wiki/Kielder_Water">Kielder Water</a> during Kielder marathon.</p>
</td>
</tr>
</table>
<h2>Accessing the EXIF data</h2>
<p>You can access the photos on a WP7 device via the XNA <a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.media.medialibrary.aspx">MediaLibrary</a> class. The interface that this class provides gives you access to <a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.media.picture.aspx">Picture</a> instances which have properties that allow you to access the width / height and a few other basic attributes. They also have methods that return streams which can be used to read the thumbnail and image data, however, they do not expose the picture location. This is &#8216;hidden&#8217; within the EXIF data.</p>
<p>Fortunately there is a C# implementation of an <a href="http://www.codeproject.com/KB/silverlight/Exif_Data.aspx">EXIF decoder available on codeproject</a>, which, <a href="http://timheuer.com/blog/archive/2010/09/23/working-with-pictures-in-camera-tasks-in-windows-phone-7-orientation-rotation.aspx">with a few tweaks by Tim Heuer</a> works just fine within Silverlight for Windows Phone 7.</p>
<p>With this library, accessing the EXIF data is a one-liner:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">JpegInfo info <span style="color: #008000;">=</span> ExifReader<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadJpeg</span><span style="color: #008000;">&#40;</span>picture<span style="color: #008000;">.</span><span style="color: #0000FF;">GetImage</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, picture<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The JpegInfo class exposes the raw EXIF geolocation data, which is detailed in the <a href="http://www.exif.org/Exif2-2.PDF">EXIF specification</a> as being expressed as separate components of degrees, minutes and seconds together with a reference direction (North / South, East / West). We can convert from the <a href="http://en.wikipedia.org/wiki/Sexagesimal">sexagesimal</a> numeric system used in EXIF, to the decimal system as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">double</span> DecodeLatitude<span style="color: #008000;">&#40;</span>JpegInfo info<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">double</span> degrees <span style="color: #008000;">=</span> ToDegrees<span style="color: #008000;">&#40;</span>info<span style="color: #008000;">.</span><span style="color: #0000FF;">GpsLatitude</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> info<span style="color: #008000;">.</span><span style="color: #0000FF;">GpsLatitudeRef</span> <span style="color: #008000;">==</span> ExifGpsLatitudeRef<span style="color: #008000;">.</span><span style="color: #0000FF;">North</span> <span style="color: #008000;">?</span> degrees <span style="color: #008000;">:</span> <span style="color: #008000;">-</span>degrees<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">double</span> DecodeLongitude<span style="color: #008000;">&#40;</span>JpegInfo info<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">double</span> degrees <span style="color: #008000;">=</span> ToDegrees<span style="color: #008000;">&#40;</span>info<span style="color: #008000;">.</span><span style="color: #0000FF;">GpsLongitude</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> info<span style="color: #008000;">.</span><span style="color: #0000FF;">GpsLongitudeRef</span> <span style="color: #008000;">==</span> ExifGpsLongitudeRef<span style="color: #008000;">.</span><span style="color: #0000FF;">East</span> <span style="color: #008000;">?</span> degrees <span style="color: #008000;">:</span> <span style="color: #008000;">-</span>degrees<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">double</span> ToDegrees<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> data<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> data<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> data<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">60.0</span> <span style="color: #008000;">+</span> data<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">/</span> <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">60.0</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">60.0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h2>Analysing the images</h2>
<p>When the application starts a <code>BackgroundWorker</code> is used to read the EXIF data for all of the pictures in the phone&#8217;s media library, with those that have geolocation data available being stored in a separate list:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">BackgroundWorker bw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BackgroundWorker<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
bw<span style="color: #008000;">.</span><span style="color: #0000FF;">WorkerReportsProgress</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// analyse the pictures that reside in the Media Library in a background thread</span>
bw<span style="color: #008000;">.</span><span style="color: #0000FF;">DoWork</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
<span style="color: #008000;">&#123;</span>
  var ml <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MediaLibrary<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var pics <span style="color: #008000;">=</span> ml<span style="color: #008000;">.</span><span style="color: #0000FF;">Pictures</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">int</span> total <span style="color: #008000;">=</span> pics<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">int</span> index <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Picture picture <span style="color: #0600FF; font-weight: bold;">in</span> pics<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">// read the EXIF data for this image</span>
      JpegInfo info <span style="color: #008000;">=</span> ExifReader<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadJpeg</span><span style="color: #008000;">&#40;</span>picture<span style="color: #008000;">.</span><span style="color: #0000FF;">GetImage</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, picture<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// check if we have co-ordinates</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>info<span style="color: #008000;">.</span><span style="color: #0000FF;">GpsLatitude</span><span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        _images<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> LocatedImage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
          Picture <span style="color: #008000;">=</span> picture,
          Lat <span style="color: #008000;">=</span> DecodeLatitude<span style="color: #008000;">&#40;</span>info<span style="color: #008000;">&#41;</span>,
          <span style="color: #6666cc; font-weight: bold;">Long</span> <span style="color: #008000;">=</span> DecodeLongitude<span style="color: #008000;">&#40;</span>info<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// report progress back to the UI thread</span>
      <span style="color: #6666cc; font-weight: bold;">string</span> progress <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;{0} / {1}&quot;</span>, index, total<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      bw<span style="color: #008000;">.</span><span style="color: #0000FF;">ReportProgress</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>index <span style="color: #008000;">*</span> <span style="color: #FF0000;">100</span> <span style="color: #008000;">/</span> total<span style="color: #008000;">&#41;</span>, progress<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      index<span style="color: #008000;">++;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// update progress on the UI thread</span>
bw<span style="color: #008000;">.</span><span style="color: #0000FF;">ProgressChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> title <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">UserState</span><span style="color: #008000;">;</span>
    ApplicationTitle<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> title<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
bw<span style="color: #008000;">.</span><span style="color: #0000FF;">RunWorkerAsync</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// when analysis is complete, add the pushpins</span>
bw<span style="color: #008000;">.</span><span style="color: #0000FF;">RunWorkerCompleted</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
  <span style="color: #008000;">&#123;</span>
    ApplicationTitle<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
    AddPushpins<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>When the pictures have all been analysed, a pushpin is created for each image which is then added to the <a href="http://www.scottlogic.co.uk/blog/colin/2011/11/pushpin-clustering-with-the-windows-phone-7-bing-map-control/" title="Pushpin Clustering with the Windows Phone 7 Bing Map control">clusterer described in my previous blog post</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> AddPushpins<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  List<span style="color: #008000;">&lt;</span>Pushpin<span style="color: #008000;">&gt;</span> pushPins <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Pushpin<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// create a pushpin for each picture</span>
  <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var image <span style="color: #0600FF; font-weight: bold;">in</span> _images<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Location location <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Location<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      Latitude <span style="color: #008000;">=</span> image<span style="color: #008000;">.</span><span style="color: #0000FF;">Lat</span>,
      Longitude <span style="color: #008000;">=</span> image<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Long</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    Pushpin myPushpin <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Pushpin<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      Location <span style="color: #008000;">=</span> location,
      DataContext <span style="color: #008000;">=</span> image,
      Content <span style="color: #008000;">=</span> image,
      ContentTemplate <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Resources</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;MarkerTemplate&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> DataTemplate
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    pushPins<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>myPushpin<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// add them to the map via a clusterer</span>
  var clusterer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PushpinClusterer<span style="color: #008000;">&#40;</span>map, pushPins, <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Resources</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;ClusterTemplate&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> DataTemplate<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The template used for the pushpins simply renders the image thumbnail:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;MarkerTemplate&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">BorderBrush</span>=<span style="color: #ff0000;">&quot;White&quot;</span> <span style="color: #000066;">BorderThickness</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Image</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;{Binding Picture, Converter={StaticResource PictureThumbnailConverter}}&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;80&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;80&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This makes use of a simple value converter which takes a <code>Picture</code> instance and converts it into a <code>BitmapImage </code>which is used as the <code>Source </code>for the image:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> PictureThumbnailConverter <span style="color: #008000;">:</span> IValueConverter
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Convert<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, Type targetType, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, <span style="color: #000000;">System.<span style="color: #0000FF;">Globalization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">CultureInfo</span> culture<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Picture picture <span style="color: #008000;">=</span> value <span style="color: #0600FF; font-weight: bold;">as</span> Picture<span style="color: #008000;">;</span>
    BitmapImage src <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BitmapImage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    src<span style="color: #008000;">.</span><span style="color: #0000FF;">SetSource</span><span style="color: #008000;">&#40;</span>picture<span style="color: #008000;">.</span><span style="color: #0000FF;">GetThumbnail</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> src<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> ConvertBack<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, Type targetType, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, <span style="color: #000000;">System.<span style="color: #0000FF;">Globalization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">CultureInfo</span> culture<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The puhspin clusterer allows you to specify a separate template for clustered pushpins. The <code>DataContext</code> for this template is a list of the <code>DataContexts</code> of the clustered pins that it represents. For this application I created a template which renders what looks like a &#8216;stack&#8217; of images. The number of pictures in the cluster is rendered as a <code>TextBlock</code> and the last image in the cluster rendered.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;ClusterTemplate&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;75&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;75&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Canvas<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">Style</span>=<span style="color: #ff0000;">&quot;{StaticResource FakePhoto}&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">Canvas.Left</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">Canvas.Top</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">Style</span>=<span style="color: #ff0000;">&quot;{StaticResource FakePhoto}&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">Canvas.Left</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">Canvas.Top</span>=<span style="color: #ff0000;">&quot;5&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">BorderBrush</span>=<span style="color: #ff0000;">&quot;White&quot;</span> <span style="color: #000066;">BorderThickness</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">Canvas.Left</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">Canvas.Top</span>=<span style="color: #ff0000;">&quot;10&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">DataContext</span>=<span style="color: #ff0000;">&quot;{Binding Path=., Converter={StaticResource LastConverter}}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Image</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;{Binding Picture, Converter={StaticResource PictureThumbnailConverter}}&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;60&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;60&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Count}&quot;</span></span>
<span style="color: #009900;">                  <span style="color: #000066;">Opacity</span>=<span style="color: #ff0000;">&quot;0.5&quot;</span></span>
<span style="color: #009900;">                  <span style="color: #000066;">Canvas.Left</span>=<span style="color: #ff0000;">&quot;25&quot;</span> <span style="color: #000066;">Canvas.Top</span>=<span style="color: #ff0000;">&quot;15&quot;</span></span>
<span style="color: #009900;">                  <span style="color: #000066;">FontSize</span>=<span style="color: #ff0000;">&quot;35&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Canvas<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>      
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;Border&quot;</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;FakePhoto&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Width&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;60&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Height&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;60&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;BorderBrush&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;White&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Background&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Black&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;BorderThickness&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The code that renders the last image is a bit cunning, it uses a value converter that performs a Linq style &#8216;last&#8217; operations, extracting the last items from a collection of objects:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> LastConverter <span style="color: #008000;">:</span> IValueConverter
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Convert<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, Type targetType, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, <span style="color: #000000;">System.<span style="color: #0000FF;">Globalization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">CultureInfo</span> culture<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    IList enumerable <span style="color: #008000;">=</span> value <span style="color: #0600FF; font-weight: bold;">as</span> IList<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> enumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">Cast</span><span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Last</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> ConvertBack<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, Type targetType, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, <span style="color: #000000;">System.<span style="color: #0000FF;">Globalization</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">CultureInfo</span> culture<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This feels quite neat to me <img src='http://www.scottlogic.co.uk/blog/colin/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>The clustered pins look like the following, which is a cluster of 5 images around Paris, with the stunning <a href="http://en.wikipedia.org/wiki/Grande_Arche">La Grande Arche de la Défense</a> as the image at the top of the cluster:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/01/cluster.jpg" alt="" title="cluster" width="229" height="157" class="aligncenter size-full wp-image-1943" /></p>
<p>Despite its simplicity, I have had a lot of fun playing with this application. It has certainly encouraged me to take as many photos as possible whenever I go travelling.</p>
<p>You can download the full sourcecode here: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/01/PhotoBrowser.zip'>PhotoBrowser.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2012/01/windows-phone-7-browsing-your-photos-via-bing-maps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Proud to be a CodeProject MVP 2012</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2012/01/proud-to-be-a-codeproject-mvp-2012/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2012/01/proud-to-be-a-codeproject-mvp-2012/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 20:48:46 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1919</guid>
		<description><![CDATA[I have just received an email from Chris Maunder, co-founder of CodeProject, informing me that I have been awarded CodeProject MVP status for 2012. I am very pleased to have received this award, which is given to a small handful of individuals each year. What I like about CodeProject is the fantastic quality of the [...]]]></description>
			<content:encoded><![CDATA[
<p>I have just received an email from Chris Maunder, co-founder of CodeProject, informing me that I have been awarded CodeProject MVP status for 2012. I am very pleased to have received this award, which is <a href="http://www.codeproject.com/script/Awards/MVPWinners.aspx">given to a small handful of individuals</a> each year.</p>
<p><a href="http://www.codeproject.com/Members/Colin-Eberhardt"><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/04/codeproject.gif" alt="" title="codeproject" width="225" height="72" class="aligncenter size-full wp-image-283" /></a></p>
<p>What I like about CodeProject is the fantastic quality of the articles submitted by the thousands of authors that participate in this site, with the only payment being their own please in the writing.</p>
<p>I have not written that many articles throughout the year, just four of them. However, I have put a lot of effort into each one of them &#8211; and thankfully they have been very well received. It is great that CodeProject favours quality over quantity. My articles for 2011 were:</p>
<ul>
<li><a href="http://www.codeproject.com/KB/codegen/CodeSnippetAutomation.aspx">Declarative Codesnippet Automation with T4 Templates</a></li>
<li><a href="http://www.codeproject.com/KB/windows-phone-7/WindowsPhone7JumpLIst.aspx">Developing a Windows Phone 7 JumpList Control</a></li>
<li><a href="http://www.codeproject.com/KB/scripting/SilverlightToHTML5.aspx">From Silverlight to HTML5</a></li>
<li><a href="http://www.codeproject.com/KB/windows-phone-7/XAMLFinance.aspx">XAMLFinance – A Cross-platform WPF, Silverlight &#038; WP7 Application</a></li>
</ul>
<p>I do also circulate some of my blog posts via the CodeProject Associate program..</p>
<p>On that note, it is almost three months since I last wrote an article &#8230; fortunately I do have an idea for the next article, so I had better get on with writting it &#8230;</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2012/01/proud-to-be-a-codeproject-mvp-2012/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>FastClick for WP7 &#8211; Improving Browser Responsiveness for PhoneGap Apps</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2012/01/fastclick-for-wp7-improving-browser-responsiveness-for-phonegap-apps/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2012/01/fastclick-for-wp7-improving-browser-responsiveness-for-phonegap-apps/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 10:23:50 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[phone]]></category>
		<category><![CDATA[WebBrowser]]></category>
		<category><![CDATA[Windows Phone 7]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1907</guid>
		<description><![CDATA[I recently released an update of the HTML5 / PhoneGap application I wrote a few months ago to the marketplace. This update fixed the location detection issue, where the marketplace certification process failed to detect that the application was using geolocation data via the browser (this issue has been fixed in PhoneGap 1.2), plus a [...]]]></description>
			<content:encoded><![CDATA[
<p>I recently released an update of the HTML5 / PhoneGap application I wrote a few months ago to the marketplace. This update fixed the location detection issue, where the marketplace certification process failed to detect that the application was using geolocation data via the browser (this issue <a href="https://issues.apache.org/jira/browse/CB-138">has been fixed in PhoneGap 1.2</a>), plus a few cosmetic improvements.</p>
<p>This application still exhibits a few UI quirks that make it fairly obvious that this is not a native application, the first is the <a href="http://stackoverflow.com/questions/6378008/windows-phone-7-browser-turn-off-the-gray-shading-when-links-are-clicked">gray box that appears whenever a link is clicked</a>, the second is the overall responsiveness &#8211; there is a short delay between clicking on links / buttons and the application responding. It is this responsiveness that I would like to tackle in this blog post. This delay unfortunately gives the impression that your application is less responsive than it actually is!</p>
<p><strong><em>So where does this delay come from?</em></strong></p>
<p>Despite the rise in popularity of touch interfaces, much of the web is still designed for interaction with a mouse and keyboard, with the DOM exposing events for mouse-up, mouse-down and click user interactions. The Windows Phone 7 browser control captures touch interactions and translates these into the required mouse events, firing them on the DOM element being interacted with. However, the browser itself also needs to support gestures such as pan, pinch-zoom and double-tap zoom.</p>
<p>If we consider for example the pinch-zoom feature, where the user places two fingers on the screen and moves them apart in order to zoom in. It is highly unlikely they will place both fingers on the screen simultaneously. If, when the first finger was placed, a mouse-down event was fired on the relevant DOM element, there would be no way to &#8216;cancel&#8217; this event when the user&#8217;s second finger made contact with the screen shortly afterwards. For this reason the WP7 mobile browser does not properly support mouse-down / mouse-up events. You will find that mousedown, mouseup then click are always fired immediately in that order regardless of how long you hold your finger on the screen for. Clearly the need to add gesture support to the browser interferes with the way in which native touch events are translated into JavaScript mouse events.</p>
<p>Now consider the double-tap zoom feature, where the user taps twice in quick succession to zoom the web page by some fixed amount. If the DOM click event were fired on the first tap, this would cause a navigation to occur if an anchor element were tapped. This is certainly not the desired behaviour! Again, there is no way to &#8216;cancel&#8217; an event once it is fired, so the only solution to this problem is, when the user first taps the screen, pause for a few hundred milliseconds to see if they tap a second time. Only after this pause is it safe to pass the interaction onto the DOM, firing a click event on the relevant DOM element.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/01/the_fence.jpg" alt="" title="the_fence" width="300" height="244" class="aligncenter size-full wp-image-1910" /></p>
<p><small>Stock image courtesy of <a href="http://www.sxc.hu/photo/313551">lovetheson</a></small></p>
<p>The need to handle gestures in the native layer causes numerous issues for the browser event handling. Thankfully these issues are relatively minor for most websites, but if your aim is to create a HTML-based application with a native look and feel, these issues become quite major.</p>
<p><strong>Improving responsiveness</strong></p>
<p>The problem I want to tackle in this blog post is that of the click DOM event firing delay to support the double-tap gesture. With HTML5-based applications you will typically want to disable pinch zoom, double-tap zoom and (optionally) scrolling <a href="http://www.scottlogic.co.uk/blog/colin/2011/11/suppressing-zoom-and-scroll-interactions-in-the-windows-phone-7-browser-control/" title="Suppressing Zoom and Scroll interactions in the Windows Phone 7 WebBrowser Control">as I described in a previous blog post</a>. As a result your application has no need for the double-tap and pinch gestures and we are safe to short-circuit this behaviour, immediately raising DOM click events when the user taps the screen.</p>
<p>My solution follows a similar approach to the utility class I wrote for suppressing pan / zoom, where event handlers are added to the <code>Border </code>that sits within the <code>WebBrowser </code>control visual tree:</p>
<pre>
\-WebBrowser
  \-Border
    \-Border
      \-PanZoomContainer
        \-Grid
          \-Border (*)
            \-ContentPresenter
              \-TileHost
</pre>
<p>In order to forward Tap gestures to the browser, we can add event handlers to the Border element, then use <code>InvokeScript</code> to pass this onto the DOM rendered within the <code>TileHost</code>.</p>
<p>The JavaScript code to achieve this is very simple, the DOM exposes an <code>elementFromPoint</code> function which allows you to hit test the DOM. The IE9 DOM interface also exposes a non-standard <code>click</code> function which invokes any click event handlers, this will cause an anchor element to navigate. Putting it together, if you have an anchor element at <code>(x=100, y=100)</code>, you can cause it to navigate using the following simple code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">elementFromPoint</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Calling this JavaScript from our native C# code in response to Tap events is very easy. I have wrapped it up into a simple utility class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Captures mouse left button up events, triggering an immediate</span>
<span style="color: #008080; font-style: italic;">/// click event on the clicked DOM element.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> BrowserFastClick
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> WebBrowser _browser<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> Border _border<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> BrowserFastClick<span style="color: #008000;">&#40;</span>WebBrowser browser<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _browser <span style="color: #008000;">=</span> browser<span style="color: #008000;">;</span>
    browser<span style="color: #008000;">.</span><span style="color: #0000FF;">Loaded</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span>Browser_Loaded<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Browser_Loaded<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// locate the element used to capture mouse events</span>
    _border <span style="color: #008000;">=</span> _browser<span style="color: #008000;">.</span><span style="color: #0000FF;">Descendants</span><span style="color: #008000;">&lt;</span>Border<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Last</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> Border<span style="color: #008000;">;</span>
    _border<span style="color: #008000;">.</span><span style="color: #0000FF;">Tap</span> <span style="color: #008000;">+=</span> Border_Tap<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Border_Tap<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, GestureEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//determine the click location</span>
    var pos <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">GetPosition</span><span style="color: #008000;">&#40;</span>_border<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// forward to the browser</span>
    _browser<span style="color: #008000;">.</span><span style="color: #0000FF;">InvokeScript</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;eval&quot;</span>,
      <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;document.elementFromPoint({0},{1}).click()&quot;</span>, pos<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span>, pos<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>      
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>To use it within a PhoneGap application, simply create an instance as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">new</span> BrowserFastClick<span style="color: #008000;">&#40;</span>PGView<span style="color: #008000;">.</span><span style="color: #0000FF;">Browser</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The net result is that your application becomes more responsive, with the browser immediately navigating links when they are first tapped.</p>
<p>I have updated the HTML5 SandwichFlow application I blogged about a few weeks ago to use this FastClick code. You can download the complete example here: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/01/HTML5SandwichFlow.zip'>HTML5SandwichFlow.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2012/01/fastclick-for-wp7-improving-browser-responsiveness-for-phonegap-apps/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>A Festive and Fun Windows Phone 7 Maze Game</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/12/a-festive-and-fun-windows-phone-7-maze-game/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/12/a-festive-and-fun-windows-phone-7-maze-game/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 10:01:19 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Windows Phone 7]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1899</guid>
		<description><![CDATA[Last night, with my Christmas presents all wrapped and a lack of any decent programmes (festive or otherwise) on television, I had a few hours to kill, so decided to create a festive-themed WP7 game &#8230; Ever since I first started writing code for Windows Phone 7 I have wanted to do something involving physics [...]]]></description>
			<content:encoded><![CDATA[
<p>Last night, with my Christmas presents all wrapped and a lack of any decent programmes (festive or otherwise) on television, I had a few hours to kill, so decided to create a festive-themed WP7 game &#8230;</p>
<p>Ever since I first started writing code for Windows Phone 7 I have wanted to do something involving physics and the accelerometer. I finally found a few hours to spare to give it a go. This blog post doesn’t go into too much detail about <a href="http://farseerphysics.codeplex.com/">Farseer</a>, the Physics Engine that I used, I didn’t really have time to learn it in any great detail. This post is more about how quickly you can put together something really quite cool in quite a short space of time using libraries and code grabbed from the internet.</p>
<p>The simple app I created renders a maze that you ‘roll’ a small ball around by tilting your phone:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/MazeGame.png" alt="" title="MazeGame" width="200" height="363" class="aligncenter size-full wp-image-1900" /></p>
<p>This works well in the new 7.1 emulator where you can tilt the emulator via the extended controls. However, it works best on a real device, where you can control tilt far more easily. See the video below:</p>
<p><iframe width="480" height="360" src="http://www.youtube.com/embed/Xkhh9P-Y45k" frameborder="0" allowfullscreen></iframe></p>
<h2>Farseer Physics Engine</h2>
<p>The game makes use of the popular <a href="http://farseerphysics.codeplex.com/">Farseer Physics Engine</a>. This library allows you to create a world populated with physics bodies, supporting features such as friction, joints, motors and much much more. As I am a Silverlight developer I made use of the <a href="http://physicshelper.codeplex.com/">Physics Helper</a> library which allows you to use the Farseer engine within a Silvelight application with very little effort. With the Physics Helper you simply make the Physics Engine ‘aware’ of the objects you wish it to control and it does the rest, animating them as they are subjected to gravity and other interactions.</p>
<p>The simplest way to use Physics Helper is via behaviours, this allows you to create a fully functioning ‘world’ without writing a single line of C# code. For example, you can create a ball and a surface that it will fall towards, eventually coming to rest, with just a few lines of XAML:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>Canvas x<span style="color: #008000;">:</span>Name<span style="color: #008000;">=</span><span style="color: #666666;">&quot;canvas&quot;</span><span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;!--</span> create our <span style="color: #666666;">'world'</span> <span style="color: #008000;">--&gt;</span>
  <span style="color: #008000;">&lt;</span>i<span style="color: #008000;">:</span>Interaction<span style="color: #008000;">.</span><span style="color: #0000FF;">Behaviors</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>pb<span style="color: #008000;">:</span>PhysicsControllerBehavior<span style="color: #008000;">/&gt;</span>
  <span style="color: #008000;">&lt;/</span>i<span style="color: #008000;">:</span>Interaction<span style="color: #008000;">.</span><span style="color: #0000FF;">Behaviors</span><span style="color: #008000;">&gt;</span>
&nbsp;
  <span style="color: #008000;">&lt;</span>Ellipse Width<span style="color: #008000;">=</span><span style="color: #666666;">&quot;100&quot;</span> Height<span style="color: #008000;">=</span><span style="color: #666666;">&quot;100&quot;</span> Fill<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Red&quot;</span>
            Canvas<span style="color: #008000;">.</span><span style="color: #0000FF;">Left</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;80&quot;</span> Canvas<span style="color: #008000;">.</span><span style="color: #0000FF;">Top</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;100&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;!--</span> make the engine aware of <span style="color: #0600FF; font-weight: bold;">this</span> element <span style="color: #008000;">--&gt;</span>
    <span style="color: #008000;">&lt;</span>i<span style="color: #008000;">:</span>Interaction<span style="color: #008000;">.</span><span style="color: #0000FF;">Behaviors</span><span style="color: #008000;">&gt;</span>
      <span style="color: #008000;">&lt;</span>pb<span style="color: #008000;">:</span>PhysicsObjectBehavior <span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;/</span>i<span style="color: #008000;">:</span>Interaction<span style="color: #008000;">.</span><span style="color: #0000FF;">Behaviors</span><span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;/</span>Ellipse<span style="color: #008000;">&gt;</span>
&nbsp;
&nbsp;
  <span style="color: #008000;">&lt;</span>Rectangle Width<span style="color: #008000;">=</span><span style="color: #666666;">&quot;500&quot;</span> Height<span style="color: #008000;">=</span><span style="color: #666666;">&quot;10&quot;</span> Fill<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Blue&quot;</span>
              Canvas<span style="color: #008000;">.</span><span style="color: #0000FF;">Top</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;500&quot;</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;!--</span> make the engine aware of <span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #0600FF; font-weight: bold;">STATIC</span> element <span style="color: #008000;">--&gt;</span>
    <span style="color: #008000;">&lt;</span>i<span style="color: #008000;">:</span>Interaction<span style="color: #008000;">.</span><span style="color: #0000FF;">Behaviors</span><span style="color: #008000;">&gt;</span>
      <span style="color: #008000;">&lt;</span>pb<span style="color: #008000;">:</span>PhysicsObjectBehavior IsStatic<span style="color: #008000;">=</span><span style="color: #666666;">&quot;True&quot;</span><span style="color: #008000;">/&gt;</span>
    <span style="color: #008000;">&lt;/</span>i<span style="color: #008000;">:</span>Interaction<span style="color: #008000;">.</span><span style="color: #0000FF;">Behaviors</span><span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;/</span>Rectangle<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>Canvas<span style="color: #008000;">&gt;</span></pre></div></div>

<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/SimpleExample.png" alt="" title="SimpleExample" width="600" height="256" class="aligncenter size-full wp-image-1901" /></p>
<p>I find this pretty amazing! </p>
<p>Physics Helper will do much more, it can handle complex paths and geometries, explosions and collisions with minimal effort. For a greater insight into just how much is possible, I would recommend <a href="http://channel9.msdn.com/coding4fun/articles/Creating-a-Pinball-Game-in-Silverlight-Using-the-Physics-Helper-Library--Farseer-Physics">Andy Beaulieu’s Channel 9 article</a>. Andy is the creator of Farseer, so really knows his stuff!</p>
<p>My maze is generated in C# code, so despite the elegance of using Physics Helper via behaviours, I need to add elements to my canvas programmatically. This means that I have to make the physics engine aware of their existence in code also. To achieve this, I add the behaviour in code as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Uses Physics Helper to create a physics body for the given element</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> AddPhysicsBody<span style="color: #008000;">&#40;</span>FrameworkElement element, <span style="color: #6666cc; font-weight: bold;">bool</span> isStatic<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  var behaviorCollection <span style="color: #008000;">=</span> Interaction<span style="color: #008000;">.</span><span style="color: #0000FF;">GetBehaviors</span><span style="color: #008000;">&#40;</span>element<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  behaviorCollection<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> PhysicsObjectBehavior<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    IsStatic <span style="color: #008000;">=</span> isStatic
  <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  var physicsObject <span style="color: #008000;">=</span> element<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>PhysicsObjectMain<span style="color: #008000;">.</span><span style="color: #0000FF;">PhysicsObjectProperty</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> PhysicsObjectMain<span style="color: #008000;">;</span>
  _physicsController<span style="color: #008000;">.</span><span style="color: #0000FF;">AddPhysicsBody</span><span style="color: #008000;">&#40;</span>physicsObject<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h2>Creating a Maze</h2>
<p>I found numerous C# maze algorithms on the internet, most of them based on the same random-walk concept. There is a great description of this approach, with <a href="http://weblog.jamisbuck.org/2011/1/20/maze-generation-wilson-s-algorithm">animated examples on this blog</a>. Unfortunately every C# implementation I came across was tightly coupled to a specific UI framework, often WinForms or XNA (tut-tut remember to separate your concerns!). So, I took a simple maze algorithm <a href="http://www.techpowerup.com/forums/showthread.php?t=109739">from a software forum</a> and ripped-out the WinForms / GDI rendering code. A new maze is constructed simply by creating an instance of the <code>Maze</code> class, passing it the number of rows and columns.</p>
<p>In order to render the maze using Silverlight elements, I simply took the original maze graphics code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> CreateMaze<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// compute the number of rows / cols</span>
  <span style="color: #6666cc; font-weight: bold;">double</span> ratio <span style="color: #008000;">=</span> mazeContainer<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualHeight</span> <span style="color: #008000;">/</span> mazeContainer<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualWidth</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">int</span> cols <span style="color: #008000;">=</span> _mazeSize<span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">int</span> rows <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>cols <span style="color: #008000;">*</span> ratio<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">double</span> cellWidth <span style="color: #008000;">=</span> mazeContainer<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualWidth</span> <span style="color: #008000;">/</span> cols<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// create the maze</span>
  Maze maze <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Maze<span style="color: #008000;">&#40;</span>rows, cols<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// render the maze</span>
  <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> cols<span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> rows<span style="color: #008000;">;</span> <span style="color: #008000;">++</span>j<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      var room <span style="color: #008000;">=</span> maze<span style="color: #008000;">.</span><span style="color: #0000FF;">cells</span><span style="color: #008000;">&#91;</span>i, j<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>room <span style="color: #008000;">&amp;</span> Maze<span style="color: #008000;">.</span><span style="color: #0000FF;">UP</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
        DrawLine<span style="color: #008000;">&#40;</span>i <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span>,
                j <span style="color: #008000;">*</span> cellWidth,
                i <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span>,
                j <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>room <span style="color: #008000;">&amp;</span> Maze<span style="color: #008000;">.</span><span style="color: #0000FF;">DOWN</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
        DrawLine<span style="color: #008000;">&#40;</span>i <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span>,
              j <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span>,
              i <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span>,
              j <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>room <span style="color: #008000;">&amp;</span> Maze<span style="color: #008000;">.</span><span style="color: #0000FF;">RIGHT</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
        DrawLine<span style="color: #008000;">&#40;</span>i <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span>,
              j <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span>,
              i <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span>,
              j <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>room <span style="color: #008000;">&amp;</span> Maze<span style="color: #008000;">.</span><span style="color: #0000FF;">LEFT</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
        DrawLine<span style="color: #008000;">&#40;</span>i <span style="color: #008000;">*</span> cellWidth,
              j <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span>,
              i <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span>,
              j <span style="color: #008000;">*</span> cellWidth <span style="color: #008000;">+</span> cellWidth <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And created a method that mimics the behaviour of the <code>System.Drawing.Graphics.DrawLine</code> method which the original maze code depended on:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> DrawLine<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span> x1, <span style="color: #6666cc; font-weight: bold;">double</span> y1, <span style="color: #6666cc; font-weight: bold;">double</span> x2, <span style="color: #6666cc; font-weight: bold;">double</span> y2<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">double</span> width <span style="color: #008000;">=</span> x2 <span style="color: #008000;">-</span> x1 <span style="color: #008000;">+</span> <span style="color: #FF0000;">2.0</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>width <span style="color: #008000;">==</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">&#41;</span> width <span style="color: #008000;">=</span> <span style="color: #FF0000;">2.0</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #6666cc; font-weight: bold;">double</span> height <span style="color: #008000;">=</span>  y2 <span style="color: #008000;">-</span> y1<span style="color: #008000;">+</span> <span style="color: #FF0000;">2.0</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>height <span style="color: #008000;">==</span> <span style="color: #FF0000;">0.0</span><span style="color: #008000;">&#41;</span> height <span style="color: #008000;">=</span> <span style="color: #FF0000;">2.0</span><span style="color: #008000;">;</span>
&nbsp;
  var rec <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Shapes</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Rectangle</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Fill <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span>Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Red</span><span style="color: #008000;">&#41;</span>,
    Width <span style="color: #008000;">=</span> width,
    Height <span style="color: #008000;">=</span> height
  <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
  Canvas<span style="color: #008000;">.</span><span style="color: #0000FF;">SetLeft</span><span style="color: #008000;">&#40;</span>rec, x1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  Canvas<span style="color: #008000;">.</span><span style="color: #0000FF;">SetTop</span><span style="color: #008000;">&#40;</span>rec, y1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  mazeContainer<span style="color: #008000;">.</span><span style="color: #0000FF;">Children</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>rec<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  AddPhysicsBody<span style="color: #008000;">&#40;</span>rec, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>A bit messy, but gets the job done!</p>
<p>The above code creates a maze comprised of static physics objects, where the<code> _mazeSize</code> field dictates the scale of the maze:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/Mazes.png" alt="" title="Mazes" width="500" height="454" class="aligncenter size-full wp-image-1902" /></p>
<h2>Adding a Rolling Ball</h2>
<p>Adding a ball to the maze is simply a matter of creating an ellipse and adding it at the maze entrance. Note, this physics-body is not static, so will move under the influence of gravity:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Ellipse ball <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Ellipse<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  Width <span style="color: #008000;">=</span> cellWidth <span style="color: #008000;">*</span> <span style="color: #FF0000;">0.7</span>,
  Height <span style="color: #008000;">=</span> cellWidth <span style="color: #008000;">*</span> <span style="color: #FF0000;">0.7</span>,
  Fill <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Resources</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;brush&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> Brush
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
Canvas<span style="color: #008000;">.</span><span style="color: #0000FF;">SetLeft</span><span style="color: #008000;">&#40;</span>ball, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>cols <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">0.7</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">*</span> cellWidth<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Canvas<span style="color: #008000;">.</span><span style="color: #0000FF;">SetTop</span><span style="color: #008000;">&#40;</span>ball, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
mazeContainer<span style="color: #008000;">.</span><span style="color: #0000FF;">Children</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>ball<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
AddPhysicsBody<span style="color: #008000;">&#40;</span>ball, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>By default, the ‘world’ has gravity with a fixed Y component and no X component. This causes all non-static bodies to fall downwards as you might expect. In order to guide the ball around the maze by tilting the phone, we need to adjust gravity based on accelerometer readings. This is achieved by the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> HandleAccelerometer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  _sensor <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Accelerometer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  _sensor<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// 10 times a second, update the gravity</span>
  var gtimer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DispatcherTimer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  gtimer<span style="color: #008000;">.</span><span style="color: #0000FF;">Interval</span> <span style="color: #008000;">=</span> TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">FromSeconds</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0.1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  gtimer<span style="color: #008000;">.</span><span style="color: #0000FF;">Tick</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_physicsController<span style="color: #008000;">.</span><span style="color: #0000FF;">Simulator</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      _physicsController<span style="color: #008000;">.</span><span style="color: #0000FF;">Simulator</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Gravity</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span> <span style="color: #008000;">*</span> <span style="color: #008000;">-</span>_sensor<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentValue</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Acceleration</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">;</span>
      _physicsController<span style="color: #008000;">.</span><span style="color: #0000FF;">Simulator</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Gravity</span><span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span> <span style="color: #008000;">*</span> _sensor<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentValue</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Acceleration</span><span style="color: #008000;">.</span><span style="color: #0000FF;">X</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
  gtimer<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Note</strong>: this uses a timer, rather than updating the simulator every time the accelerometer reading changes. I found that without the timer, I was repeatedly seeing the generic “Element is already a child of another element” exception. You will also notice in the code that the maze is built after a pause of a couple of seconds, again in order to avoid this issue. There’s something funny going on inside the Physics Helper!</p>
<p>Finally, we handle collisions in order to play a simple sounds effect a the ball bounces off each wall:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// locate the physics controller</span>
_physicsController <span style="color: #008000;">=</span> mazeContainer<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>PhysicsControllerMain<span style="color: #008000;">.</span><span style="color: #0000FF;">PhysicsControllerProperty</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> PhysicsControllerMain<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// load the WAV file</span>
Stream stream <span style="color: #008000;">=</span> TitleContainer<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenStream</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;boing1.wav&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
_effect <span style="color: #008000;">=</span> SoundEffect<span style="color: #008000;">.</span><span style="color: #0000FF;">FromStream</span><span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
FrameworkDispatcher<span style="color: #008000;">.</span><span style="color: #0000FF;">Update</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// play on each collision</span>
_physicsController<span style="color: #008000;">.</span><span style="color: #0000FF;">Collision</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s,e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> _effect<span style="color: #008000;">.</span><span style="color: #0000FF;">Play</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>And there we have it, a simple maze game powered by the Farseer Physics engine. The whole thing only took about an hour to create, with code grabbed from the internet &#8211; a bit rough and ready, but I was impressed with how easy it was to create this game. There is much more that could be done, for example detecting when the ball reaches the end of the maze, advancing to a more difficult level, however, my time has run out on this bit of fun &#8230; Also, if I were to take this idea further, I would probably use XNA rather than Silverlight. The Physics Helper is great for simple XAML-only physics models, but I feel that it wasn’t really built for programmatic creation of bodies.</p>
<p>You can download the code here: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/WP7MazeGame.zip'>WP7MazeGame.zip</a></p>
<p>Note, you will have to download and install Physics Helper to run this code.</p>
<p>On another note, Farseer is clearly the result of hundreds of hours of development effort, which makes it easy to create realistic physics-based applications and games. Farseer is free to use under the MS-PL licence. However, I would urge anyone who has used this great library to create a WP7 game to donate via the <a href="http://farseerphysics.codeplex.com/">Farseer codeplex site</a> (scroll to the bottom).</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/12/a-festive-and-fun-windows-phone-7-maze-game/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Metro Themed PowerPoint Template</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/12/a-metro-themed-powerpoint-template/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/12/a-metro-themed-powerpoint-template/#comments</comments>
		<pubDate>Mon, 19 Dec 2011 17:10:57 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[metro]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1889</guid>
		<description><![CDATA[Over the weekend I mentioned on Twitter that I had created a Metro-style PowerPoint template. There were a few requests to share, so hence this blog post. Scroll to the bottom to download the PowerPoint file. So why create a Metro-style PowerPoint presentation? A couple of reasons, firstly, the templates that come with PowerPoint are [...]]]></description>
			<content:encoded><![CDATA[
<p>Over the weekend I mentioned on Twitter that I had created a Metro-style PowerPoint template. There were a few requests to share, so hence this blog post. Scroll to the bottom to download the PowerPoint file.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/MetroPP.png" alt="" title="MetroPP" width="506" height="378" class="aligncenter size-full wp-image-1890" /></p>
<p>So why create a Metro-style PowerPoint presentation? A couple of reasons, firstly, the templates that come with PowerPoint are absolutely awful!</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/PPTemplates.png" alt="" title="PPTemplates" width="571" height="282" class="aligncenter size-full wp-image-1891" /></p>
<p>They look very dated (to put it politely). Secondly, with the Metro Design Language, which was introduced with Windows Phone 7, Microsoft have come up with something surprisingly stylish. Metro is about clarity, readability, simplicity &#8211; concepts that map very well to presentations. </p>
<p>The &#8216;classic&#8217; Metro look, which I have tried to re-produce with this template, includes highly legible san-serif fonts (from the Segoe family), a lack of chrome (i.e. shadows, gradient) and fluid motions. Many metro applications do include background imagery to add &#8216;warmth&#8217;, as I have done here also.</p>
<p>I have created a template with a master and a number of layouts. Each layout includes a number of sequenced animations, mimicking the &#8216;fast and fluid&#8217; Metro interface:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/Animations.png" alt="" title="Animations" width="600" height="347" class="aligncenter size-full wp-image-1892" /></p>
<p>PowerPoint can be used to create some rather neat sequenced animations, if you ignore the rather garish defaults.</p>
<p>To get a better feel for this template, have a look at it in motion (the quality here is rather poor, I haven&#8217;t managed to find a screen capture tool that works well for subtle colours):</p>
<p><iframe width="480" height="360" src="http://www.youtube.com/embed/Nmtu5vSYrn8" frameborder="0" allowfullscreen></iframe></p>
<p>If you want to use this template, you might want to change the background image. You can do this via the &#8216;slide master view&#8217;. The background I included is one I found on the free stock imagery site stock.xcnhg, by the user <a href="http://www.sxc.hu/photo/1246725">fangol</a>. There are numerous other free images on that site. Please remember to attribute your usage of free stock images.</p>
<p>You might also find that you need to add new layouts based on your content. Again, this is easy to do via the master view.</p>
<p>Anyhow, here is the template, enjoy: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/MetroStyle.pptx'>MetroStyle.pptx</a></p>
<p><b>NOTE:</b> Until I get the mime-types sorted out you might have to right click and select &#8220;save as&#8221; to save the above PowerPoint file.</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/12/a-metro-themed-powerpoint-template/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Simple Multi-Page Windows Phone 7 PhoneGap Example</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/12/a-simple-multi-page-windows-phone-7-phonegap-example/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/12/a-simple-multi-page-windows-phone-7-phonegap-example/#comments</comments>
		<pubDate>Thu, 15 Dec 2011 15:55:11 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PhoneGap]]></category>
		<category><![CDATA[Windows Phone 7]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1873</guid>
		<description><![CDATA[This blog post shows how you can use PhoneGap to create Windows Phone 7 applications that are comprised of multiple, simple HTML pages, whilst meeting the Marketplace certification requirements. Readers of my blog will know that I have been working on, and writing about, the use of PhoneGap to create HTML5-based Windows Phone 7 applications. [...]]]></description>
			<content:encoded><![CDATA[
<p><em>This blog post shows how you can use PhoneGap to create Windows Phone 7 applications that are comprised of multiple, simple HTML pages, whilst meeting the Marketplace certification requirements.</em></p>
<p>Readers of my blog will know that I have been working on, and writing about, the use of <a href="http://phonegap.com/">PhoneGap</a> to create HTML5-based Windows Phone 7 applications. Along the way I have solved problems such as <a href="http://www.scottlogic.co.uk/blog/colin/2011/11/handling-the-back-stack-in-windows-phone-7-phonegap-applications/">back-button support</a>, <a href="http://www.scottlogic.co.uk/blog/colin/2011/10/tombstoning-with-phonegap-for-windows-phone-7-and-knockoutjs/">tombstoning</a> and <a href="http://www.scottlogic.co.uk/blog/colin/2011/11/suppressing-zoom-and-scroll-interactions-in-the-windows-phone-7-browser-control/">suppressing browser pan and zoom</a>. So far the applications I have discussed have all been single-page JavaScript applications, i.e. the <code>WebBrowser </code>control loads a single HTML page which links to the application JavaScript. All subsequent navigation involves updating the DOM dynamically rather than navigating to a new URL. However, I have had numerous questions to my previous blog posts from people who are using PhoneGap to create simple navigation-based applications. In this context PhoneGap is being used as a mechanism for packaging up a collection of relatively static HTML pages.</p>
<p>So why would you want to do this? This is a perfectly valid question! If you have static HTML based content why not simply put it on the web? There are a few reasons, one is to ensure that the content is always available, whether the phone is connected to a network or not. Another is monetization, the app-store model provides an easy way to charge for this content. Regardless of the pros and cons, based on the feedback my previous articles have received, people are very interested in using PhoneGap as a model for packaging static content.</p>
<p>In this blog post I will show how to solve the problems of back-button support and pan / zoom for a simple navigation-based HTML application. As an example, I have chosen a subject which I have a lot of interest in … sandwiches! I have previously blogged about <a href="http://www.scottlogic.co.uk/blog/colin/2011/05/metro-in-motion-5-sandwichflow/">SandwichFlow</a>, an example application which showcased my metro-in-motion series, here I have transformed the sandwich recipe HTML into a collection of static HTML pages.</p>
<h2>Packaging the HTML</h2>
<p>My <a href="http://www.scottlogic.co.uk/blog/colin/2011/09/developing-windows-phone-7-html5-apps-with-phonegap/">first PhoneGap blog post</a> detailed how to get up-and-running with PhoneGap, if you have never used this framework before I would recommend that you read that first. Packaging my sandwich recipe content into an application was simply a matter of copying the files (HTML and CSS) into the ‘www’ directory, running the T4 template that auto-generates the <code>GapSourceDictionary.xml</code>, building and running. The resulting application is as follows:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/SandwichFlowThree.jpg" alt="" title="SandwichFlowThree" width="600" height="346" class="aligncenter size-full wp-image-1883" /></p>
<p>PhoneGap does a great job of making it very easy to package HTML content into an application, loading the files into isolates storage etc … However, there are a few Windows Phone 7 specific concepts that have to be supported in order to ensure certification on submission to the marketplace.</p>
<h2>Back-button Support</h2>
<p><strong>UPDATE:</strong> There are a few issues with the code below. See my <a href="http://www.scottlogic.co.uk/blog/colin/2012/01/wp7-phonegap-backbutton-support-re-visited/" title="WP7 PhoneGap Backbutton support re-visited">more recent blogpost for an improved solution</a>.</p>
<p>Correct back-button support is a mandatory requirement for marketplace certification. Hitting the back button should navigate back through the various screens of an application. Hitting the back-button on the first screen should terminate the application. The standard PhoneGap Visual Studio template does not support the back-button by default, this no-doubt reflects the fact that the other PhoneGap platforms (iPhone, Android, BlackBerry) do not share this same requirement, or lack a back-button altogether.</p>
<p>In my <a href="http://www.scottlogic.co.uk/blog/colin/2011/11/handling-the-back-stack-in-windows-phone-7-phonegap-applications/">previous blog post on back-button support</a> I described how a single-page JavaScript application can manage its own back-stack. This approach does not work for navingation-based HTML applications.</p>
<p>The <code>WebBrowser</code> control which PhoneGap uses to render its contents stores its navigation history in the same way that a desktop browser does. This history can be accessed via the JavaScript ‘history’ object. If the back-button is pressed after navigating to one of the packaged HTML pages, invoking <code>history.go(-1)</code> will cause the WebBrowser control to navigate backwards. All we need to do is to keep track of navigation events to determine whether to route the back-button too our HTML application or not. </p>
<p>The following utility class does just that:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Handles the back-button for a PhoneGap application. When the back-button</span>
<span style="color: #008080; font-style: italic;">/// is pressed, the browser history is navigated. If no history is present,</span>
<span style="color: #008080; font-style: italic;">/// the application will exit.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> BackButtonHandler
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> _browserHistoryLength <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> PGView _phoneGapView<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> BackButtonHandler<span style="color: #008000;">&#40;</span>PhoneApplicationPage page, PGView phoneGapView<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// subscribe to the hardware back-button</span>
    page<span style="color: #008000;">.</span><span style="color: #0000FF;">BackKeyPress</span> <span style="color: #008000;">+=</span> Page_BackKeyPress<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// handle navigation events</span>
    phoneGapView<span style="color: #008000;">.</span><span style="color: #0000FF;">Browser</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Navigated</span> <span style="color: #008000;">+=</span> Browser_Navigated<span style="color: #008000;">;</span>
&nbsp;
    _phoneGapView <span style="color: #008000;">=</span> phoneGapView<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Browser_Navigated<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, NavigationEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">NavigationMode</span> <span style="color: #008000;">==</span> NavigationMode<span style="color: #008000;">.</span><span style="color: #008000;">New</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      _browserHistoryLength<span style="color: #008000;">++;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Page_BackKeyPress<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, CancelEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_browserHistoryLength <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      _phoneGapView<span style="color: #008000;">.</span><span style="color: #0000FF;">Browser</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InvokeScript</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;eval&quot;</span>, <span style="color: #666666;">&quot;history.go(-1)&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      _browserHistoryLength <span style="color: #008000;">-=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
      e<span style="color: #008000;">.</span><span style="color: #0000FF;">Cancel</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Note:</strong> I have had a number of requests from readers who do not have any C# / Silverlight experience, so are unsure how to use code snippets like the one above. In this blog post I have included the full sourcecode. Just grab the code and replace the contents of the ‘www’ directory with your own content, and you are good to go!</p>
<p>With the above code in place the application now supports the back-button </p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/BackStack.jpg" alt="" title="BackStack" width="600" height="249" class="aligncenter size-full wp-image-1874" /></p>
<p>With this feature in place you should be able to successfully submit a PhoneGap based application to the Windows Phone 7 marketplace. The other bits of code that follow are all about improving the cosmetics.</p>
<h2>Suppressing Pan and Zoom</h2>
<p>Whilst the viewport metadata settings can be used to prevent scaling, the cosmetics of this feature are not terribly good with Windows Phone 7, the user can still ‘pinch’ your HTML page, however, it snaps back to its original scale when the manipulation ends. I previously published a <a href="http://www.scottlogic.co.uk/blog/colin/2011/11/suppressing-zoom-and-scroll-interactions-in-the-windows-phone-7-browser-control/">simple class which can intercept manipulation events</a> in order to suppress the pinch zoom behaviour.</p>
<p>This class can also ‘lock’ the browser windows entirely, supressing scroll as well as zoom. However, this is only appropriate for use on pages where you can be sure the content fits entirely within a single page. In order to ‘activate’ this behaviour we need to send a message from the HTML / JavaScript to the C# utility class. The ’about’ page of this application does just that. When it is loaded the following JavaScript is executed:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> charset<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;utf-8&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #006600; font-style: italic;">// ensure that this page does not scroll</span>
  window.<span style="color: #660066;">external</span>.<span style="color: #660066;">Notify</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;noScroll&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>The utility class (described in the earlier blog post) is instantiated in code-behind, with the above Notify being used to ‘lock’ the page:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> WebBrowserHelper _browserHelper<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Constructor</span>
<span style="color: #0600FF; font-weight: bold;">public</span> MainPage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  InitializeComponent<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008000;">new</span> BackButtonHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, PGView<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  _browserHelper <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebBrowserHelper<span style="color: #008000;">&#40;</span>PGView<span style="color: #008000;">.</span><span style="color: #0000FF;">Browser</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  PGView<span style="color: #008000;">.</span><span style="color: #0000FF;">Browser</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ScriptNotify</span> <span style="color: #008000;">+=</span> Browser_ScriptNotify<span style="color: #008000;">;</span>
  PGView<span style="color: #008000;">.</span><span style="color: #0000FF;">Browser</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Navigated</span> <span style="color: #008000;">+=</span> Browser_Navigated<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Browser_Navigated<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, NavigationEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// when we first navigate to a page, we assume that it can be scrolled</span>
  _browserHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">ScrollDisabled</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Browser_ScriptNotify<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, NotifyEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// if a page notifies that it should not be scrollable, disable</span>
  <span style="color: #008080; font-style: italic;">// scrolling.</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;noScroll&quot;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _browserHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">ScrollDisabled</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Again, the code above is included in the download at the end of this article. To &#8216;lock&#8217; the scroll on any of your HTML pages, simply add the JavaScript &#8216;Notify&#8217; above. </p>
<h2>Splashscreen</h2>
<p>One final cosmetic issue is the application startup. The application splashcreen is hidden automatically when your application starts up. This is fine for a native application, however a PhoneGap application isn’t quite ready at this point. After the native wrapper starts, the code is loaded into the WebBrwoser control and the HTML / JavaScript application starts. This results in a rather ugly white screen appearing for ~ 0.5 second when the application starts up.</p>
<p>The start screen duration is not configurable, however, there is a simple solution to this problem, when the application first starts, render the splashscreen as the application content, then hide this image when the PhoneGap application starts. </p>
<p>To do this, create an image and position it in front of the PhoneGap browser control:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LayoutRoot&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;Transparent&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;my:PGView</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span> </span>
<span style="color: #009900;">                  <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;0,0,0,0&quot;</span>  </span>
<span style="color: #009900;">                  <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;PGView&quot;</span> </span>
<span style="color: #009900;">                  <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Image</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;/SplashScreenImage.jpg&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;splashImage&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Image.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Storyboard</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;fadeOut&quot;</span></span>
<span style="color: #009900;">                  <span style="color: #000066;">BeginTime</span>=<span style="color: #ff0000;">&quot;0:0:0.5&quot;</span></span>
<span style="color: #009900;">                  <span style="color: #000066;">Completed</span>=<span style="color: #ff0000;">&quot;fadeOut_Completed&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DoubleAnimation</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Storyboard.TargetName</span>=<span style="color: #ff0000;">&quot;splashImage&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Storyboard.TargetProperty</span>=<span style="color: #ff0000;">&quot;Opacity&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">From</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">To</span>=<span style="color: #ff0000;">&quot;0.0&quot;</span> <span style="color: #000066;">Duration</span>=<span style="color: #ff0000;">&quot;0:0:0.3&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Storyboard<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Image.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Image<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This image has a storyboard that fades-out the image. </p>
<p>This animation is triggered when the browser navigates to the first age of the application:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">EventHandler<span style="color: #008000;">&lt;</span>NavigationEventArgs<span style="color: #008000;">&gt;</span> hideSplashScreen <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
hideSplashScreen <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>s, e <span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
  <span style="color: #008000;">&#123;</span>
    fadeOut<span style="color: #008000;">.</span><span style="color: #0000FF;">Begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    PGView<span style="color: #008000;">.</span><span style="color: #0000FF;">Browser</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Navigated</span> <span style="color: #008000;">-=</span> hideSplashScreen<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
PGView<span style="color: #008000;">.</span><span style="color: #0000FF;">Browser</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Navigated</span> <span style="color: #008000;">+=</span> hideSplashScreen<span style="color: #008000;">;</span></pre></div></div>

<p>Finally, the <code>Completed </code>event handler removes the image altogether when the animation ends:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> fadeOut_Completed<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  splashImage<span style="color: #008000;">.</span><span style="color: #0000FF;">Visibility</span> <span style="color: #008000;">=</span> Visibility<span style="color: #008000;">.</span><span style="color: #0000FF;">Collapsed</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h2>Conclusions</h2>
<p>Hopefully this blog post will help people who are new to WP7 and wish to release PhoneGap applications comprised of multiple HTML pages. </p>
<p>You can download the sourcecode: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/HTML5SandwichFlow.zip'>HTML5SandwichFlow.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/12/a-simple-multi-page-windows-phone-7-phonegap-example/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>A Windows Phone 7 Slide View with Page Pips</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/12/a-windows-phone-7-slide-view-with-page-pips/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/12/a-windows-phone-7-slide-view-with-page-pips/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 11:51:09 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[Windows Phone 7]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1866</guid>
		<description><![CDATA[A popular user-interface in the iOS world is the UIPageControl which renders a small set of dots to indicate the number of pages, with the current page highlighted in some way. This is often used in conjunction with a UIScrollView to navigate between pages in a multi-page layout. Windows Phone 7 has a Pivot control [...]]]></description>
			<content:encoded><![CDATA[
<p>A popular user-interface in the iOS world is the <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPageControl_Class/Reference/Reference.html">UIPageControl</a> which renders a small set of dots to indicate the number of pages, with the current page highlighted in some way. This is often used in conjunction with a <code>UIScrollView</code> to navigate between pages in a multi-page layout.</p>
<p>Windows Phone 7 has a Pivot control which allows you to swipe in order to navigate content across multiple screens. However, the Pivot control is most often used when each ‘page’ has some logic header – in some ways it is analogous to a desktop tab control. I decided to use this control, without specifying headers for each PivotItem, then add my own control to render the ‘pips’.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/SlideView.png" alt="" title="SlideView" width="630" height="222" class="aligncenter size-full wp-image-1868" /></p>
<p>The control that displays the location pips is defined in XAML, with the relationship between this control and the Pivot created via an <code>ElementName</code> binding:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:PivotLocationView</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;{Binding ElementName=pivot}&quot;</span></span>
<span style="color: #009900;">                          <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span></span>
<span style="color: #009900;">                          <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Bottom&quot;</span></span>
<span style="color: #009900;">                          <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;0,0,0,10&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;controls:Pivot</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;0,-30,0,40&quot;</span> </span>
<span style="color: #009900;">                <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;pivot&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;controls:PivotItem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/controls:PivotItem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;controls:PivotItem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/controls:PivotItem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;controls:PivotItem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/controls:PivotItem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/controls:Pivot<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Note the negative top margin for the Pivot, this control still allocates some header space, even when no headers are defined.</p>
<p>The <code>PivotLocationView</code> user control is backed by a simple view model, with model-items for each pip. When the view model is associated with a <code>Pivot</code> control, it creates a child view model for each Pivot Item, then handles the <code>SelectionChanged</code> event in order to keep the selection state synchronized:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> PivotLocationViewModel
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> Pivot _pivot<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> PivotLocationViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> PivotLocationViewModel<span style="color: #008000;">&#40;</span>Pivot pivot<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    PivotItems <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PivotItemViewModelCollection<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    SetPivot<span style="color: #008000;">&#40;</span>pivot<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Gets or sets the collection of child view-models</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> PivotItemViewModelCollection PivotItems <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetPivot<span style="color: #008000;">&#40;</span>Pivot pivot<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _pivot <span style="color: #008000;">=</span> pivot<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// handle selection changed</span>
    pivot<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectionChanged</span> <span style="color: #008000;">+=</span> Pivot_SelectionChanged<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// create a view model for each pivot item.</span>
    <span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i<span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>i<span style="color: #008000;">&lt;</span>pivot<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span>i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      PivotItems<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> PivotItemViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// set the initial selection</span>
    PivotItems<span style="color: #008000;">&#91;</span>_pivot<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedIndex</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsSelected</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Handle selection changes to update the view model</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span>  Pivot_SelectionChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, SelectionChangedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
 	  var selectedModel <span style="color: #008000;">=</span> PivotItems<span style="color: #008000;">.</span><span style="color: #0000FF;">SingleOrDefault</span><span style="color: #008000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p<span style="color: #008000;">.</span><span style="color: #0000FF;">IsSelected</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>selectedModel <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
      selectedModel<span style="color: #008000;">.</span><span style="color: #0000FF;">IsSelected</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
    PivotItems<span style="color: #008000;">&#91;</span>_pivot<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedIndex</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsSelected</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The view model for each pivot item is a simple model that implements <code>INotifyPropertyChanged</code>. It has an IsSelected property that reflects the selection state of the <code>Pivot</code>, it also exposes a <code>Color </code>property that indicates the color for each ‘pip’. This could have been implemented as a value converter, but there is little point as we already have a view model:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// A view model for each 'pip'</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> PivotItemViewModel <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// ... INotifyPropertyChanged implementation</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> _isSelected<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsSelected
  <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _isSelected<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    set
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_isSelected <span style="color: #008000;">==</span> value<span style="color: #008000;">&#41;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
&nbsp;
      _isSelected <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
      OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;IsSelected&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      Color <span style="color: #008000;">=</span> IsSelected <span style="color: #008000;">?</span> Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Black</span> <span style="color: #008000;">:</span> Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">White</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> Color _color<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> Color Color
  <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _color<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    set
    <span style="color: #008000;">&#123;</span>
      _color <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
      OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Color&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The XAML for this control is very simple, using an <code>ItemsControl</code> to render an ellipse for each ‘pip’:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl</span> ...</span>
<span style="color: #009900;">    <span style="color: #000066;">d:DataContext</span>=<span style="color: #ff0000;">&quot;{d:DesignData Source=PivotLocationViewModel.xml}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LayoutRoot&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding PivotItems}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl.ItemsPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsPanelTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel</span> <span style="color: #000066;">Orientation</span>=<span style="color: #ff0000;">&quot;Horizontal&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsPanelTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl.ItemsPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Ellipse</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;12&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;12&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;15,0,15,0&quot;</span></span>
<span style="color: #009900;">                   <span style="color: #000066;">Stroke</span>=<span style="color: #ff0000;">&quot;Black&quot;</span></span>
<span style="color: #009900;">                   <span style="color: #000066;">StrokeThickness</span>=<span style="color: #ff0000;">&quot;0.5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Ellipse.Fill<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SolidColorBrush</span> <span style="color: #000066;">Color</span>=<span style="color: #ff0000;">&quot;{Binding Color}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Ellipse.Fill<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Ellipse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Note, the use of design-time data. The following file defines a view model instance:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PivotLocationViewModel</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;clr-namespace:SlideView&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PivotLocationViewModel.PivotItems<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PivotItemViewModelCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PivotItemViewModel</span> <span style="color: #000066;">IsSelected</span>=<span style="color: #ff0000;">&quot;False&quot;</span> <span style="color: #000066;">Color</span>=<span style="color: #ff0000;">&quot;Red&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PivotItemViewModel</span> <span style="color: #000066;">IsSelected</span>=<span style="color: #ff0000;">&quot;True&quot;</span>  <span style="color: #000066;">Color</span>=<span style="color: #ff0000;">&quot;Green&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PivotItemViewModel</span> <span style="color: #000066;">IsSelected</span>=<span style="color: #ff0000;">&quot;False&quot;</span>  <span style="color: #000066;">Color</span>=<span style="color: #ff0000;">&quot;Red&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PivotItemViewModel</span> <span style="color: #000066;">IsSelected</span>=<span style="color: #ff0000;">&quot;False&quot;</span>  <span style="color: #000066;">Color</span>=<span style="color: #ff0000;">&quot;Red&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/PivotItemViewModelCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/PivotLocationViewModel.PivotItems<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/PivotLocationViewModel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This makes creating the above user control much easier, because it can be visualised in the designer:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/DesignTime.jpg" alt="" title="DesignTime" width="550" height="365" class="aligncenter size-full wp-image-1867" /></p>
<p>And there you have it, a simple user-control, which when used in conjunction with a <code>Pivot</code>, provides an interface where the user can swipe between pages.</p>
<p>You can download the full sourcecode: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/12/SlideView.zip'>SlideView.zip</a></p>
<p>Regards, Colin E. </p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/12/a-windows-phone-7-slide-view-with-page-pips/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

