<?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 &#187; codeproject</title>
	<atom:link href="http://www.scottlogic.co.uk/blog/colin/tag/codeproject/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, 09 Feb 2012 10:21:18 +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>A Simple Pattern for Creating Re-useable UserControls in WPF / Silverlight</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2012/02/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2012/02/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 16:35:41 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[binding]]></category>
		<category><![CDATA[usercontrol]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1958</guid>
		<description><![CDATA[This blog post provides step-by-step instructions for creating a user control, which exposes bindable properties, in WPF and Silverlight. The post covers dependency properties, and how to manage DataContext inheritance. When building user interfaces you will often find yourself repeating the same UI patterns across your application. After all, users like to be presented with [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2012%252F02%252Fa-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22A%20Simple%20Pattern%20for%20Creating%20Re-useable%20UserControls%20in%20WPF%20%2F%20Silverlight%20%23%22%20%7D);"></div>
<p><em>This blog post provides step-by-step instructions for creating a user control, which exposes bindable properties, in WPF and Silverlight. The post covers dependency properties, and how to manage DataContext inheritance.</em></p>
<p>When building user interfaces you will often find yourself repeating the same UI patterns across your application. After all, users like to be presented with a consistent interface, so re-use makes sense. The WPF and Silverlight frameworks provide custom controls and user controls as a mechanism for re-using blocks of UI elements. Custom controls are rather special, with the logic being de-coupled from the XAML in order to support templating. For most needs, the simpler user control is more appropriate.</p>
<p>From participating in sites like StackOverflow I have noticed that whilst most people understand how to create a user control, which allows them to ‘stamp out’ the same XAML in multiple places, many struggle with how to make their user controls flexible by exposing properties that configure how it looks or behaves. This blog post will walk through a simple example, showing you how to create a user control, add dependency properties, wire them to the user control XAML and make a truly re-useable control.</p>
<p>We’ll start with a very simple example, an application that displays a simple form field which consists of a name and a value:</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 style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;UserControlExample.MainPage&quot;</span></span>
<span style="color: #009900;">    ...<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: #000066;">Background</span>=<span style="color: #ff0000;">&quot;White&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;StackPanel</span> <span style="color: #000066;">Orientation</span>=<span style="color: #ff0000;">&quot;Horizontal&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</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;Shoesize:&quot;</span></span>
<span style="color: #009900;">                 <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span></span>
<span style="color: #009900;">                 <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>   
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=Shoesize}&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<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>This UI is bound to a simple model object that implements <code>INotifyPropertyChanged</code> (not shown for the sake of brevity):</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> ModelObject <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Shoesize <span style="color: #008000;">&#123;</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;">double</span> Height <span style="color: #008000;">&#123;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The constructor instantiates the model object and sets it as the DataContext:</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;
  var model <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ModelObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Shoesize <span style="color: #008000;">=</span> <span style="color: #FF0000;">12</span>,
    Height <span style="color: #008000;">=</span> <span style="color: #FF0000;">34.5</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> model<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This produces the expected behaviour, a label and a text field that allows you to edit the Shoesize property:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/02/Userinterface.png" alt="" title="Userinterface" width="306" height="39" class="aligncenter size-full wp-image-1964" /></p>
<p>Let’s say we want to allow the user to edit the Height property as well. We could cut and paste our current XAML, but this will only cause maintenance issues in future. Instead, the preferred approach would be to move the XAML into a user control, allowing it to be re-used.</p>
<p>The first step is to create a new user control, <code>FieldUserControl</code>, and move our XAML into there:</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 style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;UserControlExample.FieldUserControl&quot;</span></span>
<span style="color: #009900;">    ...<span style="color: #000000; font-weight: bold;">&gt;</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;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Shoesize:&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=Shoesize}&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<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>We can now replace the XAML we have moved with an instance of this user 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;UserControl</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;UserControlExample.MainPage&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:local</span>=<span style="color: #ff0000;">&quot;clr-namespace:UserControlExample&quot;</span></span>
<span style="color: #009900;">    ...<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;">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;local:FieldUserControl</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&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 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>Compiling and running this code proves that this still works; we can see the model property and edit it:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/02/Userinterface.png" alt="" title="Userinterface" width="306" height="39" class="aligncenter size-full wp-image-1964" /></p>
<p>For trivial user controls this is all we need to do. However, in most cases, like this one, you will find that there are some elements of your user control that you wish to configure. In order to use this control for editing the <code>Height </code>property we need to make the label configurable. We do this by adding a Label property to our <code>FieldUserControl</code>.</p>
<p>This is where things get a bit tricky! Ideally this property should support binding, just like any other property of the framework UI controls. The WPF / Silverlight binding framework revolves around the concept of dependency properties, you can make any property the source of a binding, but the target must be a dependency property (DP). So let’s go ahead and add a <code>Label</code> dependency property to our user control:</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: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> FieldUserControl <span style="color: #008000;">:</span> UserControl
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080;">#region Label DP</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Gets or sets the Label which is displayed next to the field</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;">String</span> Label
  <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">&#41;</span>GetValue<span style="color: #008000;">&#40;</span>LabelProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    set <span style="color: #008000;">&#123;</span> SetValue<span style="color: #008000;">&#40;</span>LabelProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</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;">/// Identified the Label dependency property</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty LabelProperty <span style="color: #008000;">=</span>
      DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Label&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>,
        <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>FieldUserControl<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> PropertyMetadata<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080;">#endregion</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> FieldUserControl<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>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>A lot of code isn’t it? It’s all boiler-plate stuff, you just have to live with it (I’d recommend either using code-snippets, or <a href="http://www.scottlogic.co.uk/blog/colin/2009/08/declarative-dependency-property-definition-with-t4-dte/">code generation for DPs</a>) </p>
<p>We can now go ahead and bind the label text to this property:</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 style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;UserControlExample.FieldUserControl&quot;</span></span>
<span style="color: #009900;">    ...<span style="color: #000000; font-weight: bold;">&gt;</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: #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;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=Label}&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=Shoesize}&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<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>However, if you compile and run the above code, you’ll find that it doesn’t work. The model property value is still displayed but the label is not.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/02/NoLabel.png" alt="" title="NoLabel" width="281" height="30" class="aligncenter size-full wp-image-1962" /></p>
<p>So why is this?</p>
<p>Bindings have both a source and a target; where the binding framework is responsible for handling change notifications from the source and (optionally) the target, keeping the two synchronized. The bindings in our <code>FieldUserControl </code>have a value for the Path, which specifies the target, but what is the source?</p>
<p>If you create a binding in XAML but do not specify the source (which is probably the most common use case), the source will be set to the <code>DataContext </code>of the control the binding has been specified on. The DataContext is inherited down the visual tree, from each control’s parent to child. The <code>DataContext </code>is most often set to a view model or business / model object, as in our case where the top level control, the <code>MainPage</code>, has its <code>DataContext</code> set to an instance of <code>ModelObject</code>.</p>
<p>As a result, the <code>DataContext </code>for <code>FieldUserControl </code>and all of its child elements is also <code>ModelObject</code>. This is why our Value binding is failing. Value is a property of <code>FieldUserControl</code>, not our model object.</p>
<p>So how do we go about fixing this? Most people’s first reaction is to set the <code>DataContext </code>of the user control to itself (I distinctly recall doing this myself the first time I encountered this problem!). We’ll find out later that this is a mistake – but for now let’s just go with it!</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> FieldUserControl<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: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>With the <code>DataContext</code> of the control now set to itself, our label is now working:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/02/NoValue.png" alt="" title="NoValue" width="293" height="34" class="aligncenter size-full wp-image-1963" /></p>
<p>However, now our value has disappeared! Again, this is a <code>DataContext </code>issue, the binding in our user control is on a <code>Shoesize </code>property, whilst the <code>DataContext</code> is now the <code>FieldUserControl </code>instance.</p>
<p>This is not such a big problem, we were going to have to change that anyway, a hard-coded binding to the <code>Shoesize</code> property means that we cannot re-use this control to edit other properties of the model object.</p>
<p>So we add another dependency property to our user control. We already have the <code>Label </code>dependency property, we now add a <code>Value</code> property:</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: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> FieldUserControl <span style="color: #008000;">:</span> UserControl
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080;">#region Label DP</span>
&nbsp;
  <span style="color: #008000;">...</span>
&nbsp;
  <span style="color: #008080;">#endregion</span>
&nbsp;
  <span style="color: #008080;">#region Value DP</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Gets or sets the Value which is being displayed</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;">object</span> Value
  <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#41;</span>GetValue<span style="color: #008000;">&#40;</span>ValueProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    set <span style="color: #008000;">&#123;</span> SetValue<span style="color: #008000;">&#40;</span>ValueProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</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;">/// Identified the Label dependency property</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty ValueProperty <span style="color: #008000;">=</span>
      DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Value&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#41;</span>,
        <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>FieldUserControl<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> PropertyMetadata<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080;">#endregion</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> FieldUserControl<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: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This value property is bound to the user control UI as follows:</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 style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;UserControlExample.FieldUserControl&quot;</span></span>
<span style="color: #009900;">    ...<span style="color: #000000; font-weight: bold;">&gt;</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: #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;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=Label}&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=Value}&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<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>The idea here is that the exposed <code>Value</code> property ‘relays’ the value of the binding in our<code> MainPage.xaml</code>, which now has a binding which tells us which model object property is being displayed in our user 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 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;Vertical&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:FieldUserControl</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">Label</span>=<span style="color: #ff0000;">&quot;Height:&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;{Binding Height}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:FieldUserControl</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">Label</span>=<span style="color: #ff0000;">&quot;Shoesize:&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;{Binding Height}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<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>If you compile and run this code you will find that … it doesn’t work!</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/02/NoValue.png" alt="" title="NoValue" width="293" height="34" class="aligncenter size-full wp-image-1963" /></p>
<p>Remember earlier when I said that setting the user control’s <code>DataContext</code> to itself is a mistake? We have just found out why!</p>
<p>The source of a binding is the <code>DataContext </code>of the control it is defined upon. In our <code>MainPage.xaml</code> we have attempted to bind the <code>Value </code>property of the <code>FieldUserControl </code>to the <code>Height </code>property on our model object. However, the code within the <code>FieldUserControl</code> constructor means that it no longer inherits its parent’s <code>DataContext </code>(i.e. our model object), so this binding does not work.</p>
<p>This problem can be fixed by setting the <code>DataContext </code>of the <code>FieldUserControl</code>’s root element to itself.</p>
<p>Note that the user control has a StackPanel as its root element and that this is named <code>LayoutRoot</code>:</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 style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;UserControlExample.FieldUserControl&quot;</span></span>
<span style="color: #009900;">    ...<span style="color: #000000; font-weight: bold;">&gt;</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>
<span style="color: #009900;">              <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;/StackPanel<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>We change the constructor so that it sets the <code>LayoutRoot </code><code>DataContext </code>to itself.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> FieldUserControl<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: #008080; font-style: italic;">//this.DataContext = this;</span>
  LayoutRoot<span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This means that the <code>FieldUserControl </code>still inherits its parent’s <code>DataContext</code>, so bindings to our model object will work. Furthermore, the <code>FieldUserControl </code>and its children all have the <code>FieldUserControl </code>as their <code>DataContext</code>, so their bindings work also:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/02/ItWorks.png" alt="" title="ItWorks" width="269" height="36" class="aligncenter size-full wp-image-1961" /></p>
<p>Finally, we’re done!</p>
<p>If the technique of binding the layout root of the user control to itself is a bit confusing &#8211; the following diagram, which shows the visual tree of our simple application, might help:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/02/DataContext.png" alt="" title="DataContext" width="550" height="377" class="aligncenter size-full wp-image-1959" /></p>
<p>Again, notice that the <code>DataContext </code>of <code>FieldUserControl </code>is inherited from its parent. This means that any bindings we add to <code>FieldUserControl</code> have the <code>ModelObect </code>as their source.</p>
<p>We can now create multiple instances of <code>FieldUserControl </code>to edit different properties:</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 style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;UserControlExample.MainPage&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:local</span>=<span style="color: #ff0000;">&quot;clr-namespace:UserControlExample&quot;</span></span>
<span style="color: #009900;">    ...<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 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;Vertical&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:FieldUserControl</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">Label</span>=<span style="color: #ff0000;">&quot;Height:&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;{Binding Height}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:FieldUserControl</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">Label</span>=<span style="color: #ff0000;">&quot;Shoesize:&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;{Binding Shoesize}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<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>With an update of the <code>FieldUserControl</code> styling, the result looks like this:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/02/final.png" alt="" title="final" width="232" height="77" class="aligncenter size-full wp-image-1960" /></p>
<p>We now have a truly re-useable user control!</p>
<p>As an aside, for bonus points, you can bind the layout root <code>DataContext</code> without any code-behind by using an <code>ElementName</code> binding as follows:</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 style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;UserControlExample.FieldUserControl&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;parent&quot;</span> ...<span style="color: #000000; font-weight: bold;">&gt;</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>
<span style="color: #009900;">              <span style="color: #000066;">DataContext</span>=<span style="color: #ff0000;">&quot;{Binding ElementName=parent}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</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 Path=Label}&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=Shoesize}&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<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>Or, in WPF you could event use a<code> RelativeSource FindAncestor</code> binding, with <code>AncestorType </code>set to the type of <code>FieldUserControl </code>(but that would just be showing off!).</p>
<p>Hopefully this blog post will help anyone who is confused about how to create user controls which expose properties in WPF or Silverlight.</p>
<p>You can download the sourcecode for the example: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2012/02/UserControlExample.zip'>UserControlExample.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2012/02/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight/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[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2012%252F01%252Fwindows-phone-7-browsing-your-photos-via-bing-maps%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Windows%20Phone%207%20-%20Browsing%20your%20Photos%20via%20Bing%20Maps%20%23%22%20%7D);"></div>
<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>Tombstoning with PhoneGap for Windows Phone 7 (and KnockoutJS)</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/10/tombstoning-with-phonegap-for-windows-phone-7-and-knockoutjs/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/10/tombstoning-with-phonegap-for-windows-phone-7-and-knockoutjs/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 12:45:44 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[PhoneGap]]></category>
		<category><![CDATA[tombstoning]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1782</guid>
		<description><![CDATA[A few weeks back I wrote a blog post about how the recent announcement of PhoneGap support for Windows Phone 7 (WP7) which makes it possible to develop HTML5-based applications. In my previous blog post I showed the development of a simple HTML5 / JavaScript application which PhoneGap wraps up within a Silverlight application &#8216;shell&#8217; [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F10%252Ftombstoning-with-phonegap-for-windows-phone-7-and-knockoutjs%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Tombstoning%20with%20PhoneGap%20for%20Windows%20Phone%207%20%28and%20KnockoutJS%29%20%23%22%20%7D);"></div>
<p>A few weeks back I wrote <a href="http://www.scottlogic.co.uk/blog/colin/2011/09/developing-windows-phone-7-html5-apps-with-phonegap/" title="Developing Windows Phone 7 HTML5 apps with PhoneGap">a blog post</a> about how the recent announcement of PhoneGap support for Windows Phone 7 (WP7) which makes it possible to develop HTML5-based applications. In my previous blog post I showed the development of a simple HTML5 / JavaScript application which PhoneGap wraps up within a Silverlight application &#8216;shell&#8217; allowing it to be deployed to your phone and potentially submitted to the Marketplace.</p>
<p>However, in order to pass the various Marketplace requirements and gain certification, your application must correctly handle the application lifecycle. With the recent Mango release, the lifecycle has become a little more complicated (although better! in that it adds multi-tasking / fast-app switching). I have also covered the lifecycle in a previous blog post and demonstrated how you can  <a href="http://www.scottlogic.co.uk/blog/colin/2011/10/a-windows-phone-7-1-mango-mvvm-tombstoning-example/" title="A Windows Phone 7.1 (Mango) MVVM Tombstoning Example">handle the various lifecycle events within an MVVM application</a>. </p>
<p>The most tricky part of the application lifecycle that as a developer you need to handle is the tombstoned state, where your application is terminated (i.e. stopped and removed from memory). It is your responsibility to save enough state in order that when your tombstoned application is restarted, it looks to the user as if your application never stopped running, i.e. you restore your application UI to its original state. </p>
<p>The Mango application lifecycle is illustrated below:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/WP7Lifecycle1.png" alt="" title="WP7Lifecycle" width="600" height="475" class="aligncenter size-full wp-image-1785" /></p>
<p>The <a href="http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html">PhoneGap events</a> API includes pause and resume events, which can be used to detect when the application transitions to and from the dormant state, however, for WP7 these events do not give us enough information. When resuming, we need to know whether it has resumed from a dormant or a tombstoned state. Considering that the tombstoned state is peculiar to WP7 (Android, and iPhone simply have a suspend / resume model), I don&#8217;t think it makes sense for the PhoneGap APIs to change in order to accommodate this. In this blog post I will show how the WP7 PhoneGap application host can be modified in order to support tombstoning.</p>
<p>But before we get there, I want to digress a while and look at using the MVVM pattern with JavaScript &#8230;</p>
<h2>Using the MVVM pattern in JavaScript</h2>
<p>Handling tombstoning is much easier if you have a good separation between your view and your logic, with the MVVM pattern being a sensible choice for achieving this. When your application is tombstoned (and your application terminated), then re-activated, it is your responsibility to recreate the original state. Your view-model, is a model-of-a-view, so technically should provide all the information required to fulfil this requirement. See my <a href="http://www.scottlogic.co.uk/blog/colin/2011/10/a-windows-phone-7-1-mango-mvvm-tombstoning-example/">previous blog</a> post for details.</p>
<p>There are numerous JavaScript UI frameworks available (MVC, MVP, MVVM), however, because I feel tombstoning lends itself particularly well to the MVVM pattern, I decided to give <a href="http://knockoutjs.com/">KnockoutJS</a> a try. When reading about this framework you will find references to WPF and Silverlight, it is clear that it has been heavily inspired by the Microsoft XAML frameworks.</p>
<p>The application I have built to demonstrate tombstoning is a very simple, single page twitter search application.</p>
<p>The Knockout view model is a JavaScript object where the properties are defined as &#8216;observables&#8217;. These are JavaScript functions which provide change notification, much like CLR properties with <code>INotifyPropergtyChanged</code> within Silverlight / WPF.</p>
<h3>The View-Model</h3>
<p>The view model for my twitter search application is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #006600; font-style: italic;">/// A view model for searching twitter for a given term</span>
<span style="color: #006600; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #003366; font-weight: bold;">function</span> TwitterSearchViewModel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> that <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// --- properties</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">isSearching</span> <span style="color: #339933;">=</span> ko.<span style="color: #660066;">observable</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">searchTerm</span> <span style="color: #339933;">=</span> ko.<span style="color: #660066;">observable</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#wp7dev&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tweets</span> <span style="color: #339933;">=</span> ko.<span style="color: #660066;">observableArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// --- functions</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// search twitter for the given string</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">search</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>that.<span style="color: #660066;">searchTerm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      that.<span style="color: #660066;">isSearching</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;http://search.twitter.com/search.json?q=&quot;</span> <span style="color: #339933;">+</span>
            encodeURIComponent<span style="color: #009900;">&#40;</span>that.<span style="color: #660066;">searchTerm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jsonp&quot;</span><span style="color: #339933;">,</span>
        url<span style="color: #339933;">:</span> url<span style="color: #339933;">,</span>
        success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #006600; font-style: italic;">// clear the results</span>
          that.<span style="color: #660066;">tweets</span>.<span style="color: #660066;">removeAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #006600; font-style: italic;">// add the new items</span>
          $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>response.<span style="color: #660066;">results</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> tweet <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> TweetViewModel<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            that.<span style="color: #660066;">tweets</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>tweet<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          that.<span style="color: #660066;">isSearching</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It comprises a few simple properties and a search function. Note, the <code>items</code> property is an <code>observableArray</code>, this is analogous to the WPF / Silverlight <code>ObservableCollection</code>, which raises events when its contents are modified, allowing the UI to update automatically. The <code>search</code> function queries the twitter APIs to find matching tweets, updating the observable <code>items</code> array with the results.</p>
<p>The <code>TwitterSearchViewModel</code> <code>items</code> collection is populated with <code>TweetViewModel</code> instances:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #006600; font-style: italic;">/// A view model that represents a single tweet</span>
<span style="color: #006600; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #003366; font-weight: bold;">function</span> TweetViewModel<span style="color: #009900;">&#40;</span>tweet<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// --- properties</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">author</span> <span style="color: #339933;">=</span> tweet.<span style="color: #660066;">from_user</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">text</span> <span style="color: #339933;">=</span> tweet.<span style="color: #660066;">text</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">time</span> <span style="color: #339933;">=</span> parseDate<span style="color: #009900;">&#40;</span>tweet.<span style="color: #660066;">created_at</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">thumbnail</span> <span style="color: #339933;">=</span> tweet.<span style="color: #660066;">profile_image_url</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// --- functions</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// parses the tweet date to give a more readable format</span>
    <span style="color: #003366; font-weight: bold;">function</span> parseDate<span style="color: #009900;">&#40;</span>date<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> diff <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>date<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>diff <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> diff.<span style="color: #660066;">toFixed</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; seconds ago&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      diff <span style="color: #339933;">=</span> diff <span style="color: #339933;">/</span> <span style="color: #CC0000;">60</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>diff <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> diff.<span style="color: #660066;">toFixed</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; minutes ago&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      diff <span style="color: #339933;">=</span> diff <span style="color: #339933;">/</span> <span style="color: #CC0000;">60</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>diff <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> diff.<span style="color: #660066;">toFixed</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; hours ago&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      diff <span style="color: #339933;">=</span> diff <span style="color: #339933;">/</span> <span style="color: #CC0000;">24</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> diff.<span style="color: #660066;">toFixed</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; days ago&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note, here the properties are <em>not</em> observables, again much like WPF / Silverlight you can bind to a property that does not notify of changes if this is not required.</p>
<p>Also, the Knockout documentation typically defines view-models as literal objects. I prefer to use <a href="http://groups.google.com/group/knockoutjs/browse_thread/thread/78ec2119146f6571">constructor functions</a>, allowing the creation of multiple instances of the same view model.</p>
<h3>The View</h3>
<p>With Knockout the view is defined in HTML, you can create it directly, or via a template. I have created the following templates:</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;script</span> <span style="color: #000066;">type</span>=text/x-jquery-tmpl<span style="color: #ff0000;">&quot; charset=&quot;</span>utf-8<span style="color: #ff0000;">&quot; id=&quot;</span>twitterSearchView<span style="color: #ff0000;">&quot;&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">data-bind</span>=<span style="color: #ff0000;">&quot;submit: search&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">data-bind</span>=<span style="color: #ff0000;">&quot;value: searchTerm, valueUpdate: 'afterkeydown'&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;button</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">data-bind</span>=<span style="color: #ff0000;">&quot;enable: searchTerm().length &gt;</span></span> 0 <span style="color: #ddbb00;">&amp;amp;&amp;amp;</span> isSearching() == false&quot;&gt;Go<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">data-bind</span>=<span style="color: #ff0000;">&quot;template: {name: 'tweetView', foreach: tweets}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<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;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/x-jquery-tmpl&quot;</span> <span style="color: #000066;">charset</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;tweetView&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;tweet&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;thumbnailColumn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">data-bind</span>=<span style="color: #ff0000;">&quot;attr: { src: thumbnail }&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;thumbnail&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;detailsColumn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;author&quot;</span> <span style="color: #000066;">data-bind</span>=<span style="color: #ff0000;">&quot;text: author&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">data-bind</span>=<span style="color: #ff0000;">&quot;text: text&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;time&quot;</span> <span style="color: #000066;">data-bind</span>=<span style="color: #ff0000;">&quot;text: time&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The <code>data-bind</code> attribute is used by Knockout to set up the various bindings, connecting your view model properties and observables to the UI.It also defines functions to invoke when DOM events are raised, in much the same way as commands do in WPF / Silverlight.</p>
<h3>The application code</h3>
<p>Instantiating the view-model and the view is as simple as the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;deviceready&quot;</span><span style="color: #339933;">,</span> onDeviceReady<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// phonegap is initialised</span>
<span style="color: #003366; font-weight: bold;">function</span> onDeviceReady<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// create the view model</span>
  twitterSearchViewModel <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> TwitterSearchViewModel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// create an instance of the view</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#twitterSearchView&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">tmpl</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#app&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// wire-up</span>
  ko.<span style="color: #660066;">applyBindings</span><span style="color: #009900;">&#40;</span>twitterSearchViewModel<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Originally I wanted to have the HTML for each view within a separate file, loading them via jQuery as <a href="http://www.knockmeout.net/2011/03/using-external-jquery-template-files.html">described in this blog post</a>. However, I just couldn&#8217;t get this to work within the embedded WP7 browser.</p>
<p>The <code>tweetView</code> tempate is used via the <code>template </code>/ <code>foreach</code> Knockout binding, mimicking the Silverlight / WPF <code>ItemsControl.ItemTemplate</code> concept.</p>
<p>The application, after a bit of styling, looks like this:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/PhoneGapTwitterSearch.png" alt="" title="PhoneGapTwitterSearch" width="300" height="546" class="aligncenter size-full wp-image-1793" /></p>
<h2>Tombstoning</h2>
<p>Now that the application has a decent structure, we can tackle the application-lifecycle. We need a way to store the view model state when the application transitions into a dormant state. Fortunately Knockout makes this very easy by supplying a <code>toJSON</code> function, which can create a JSON representation of your view-model graph (minus the observables). I have added a <code>getState</code> function to the <code>TwitterSearchViewModel</code> as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// gets the view model state as a JSON string</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getState</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> ko.<span style="color: #660066;">toJSON</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we need a way to invoke this function when the application pauses. PhoneGap provides a <code>pause</code> lifecycle event, however, we need to store the output of this function in the WP7 <code>PhoneApplicationService.Current.State</code> dictionary. Because this is a very much WP7 specific feature, I decided to do this outside of the PhoneGap lifecycle events.</p>
<p>My handler for the <code>Deactivated </code>event simply invokes the above method, storing the state in the application state dictionary:</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> Application_Deactivated<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, DeactivatedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  var viewModelState <span style="color: #008000;">=</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;getState&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">;</span>
  PhoneApplicationService<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">State</span><span style="color: #008000;">&#91;</span>ModelKey<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> viewModelState<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Note, to do this I have had to modify the current PhoneGap WP7 library to provide access to the underlying WebBrowser control, I have <a href="https://github.com/phonegap/phonegap-wp7/issues/35">raised an issue requesting this change to the PhoenGap library</a>.</p>
<p>The application now stores it state when it becomes dormant, the next step is to use this state when an application is activated from a tombstoned state. Within the <code>Activated</code> handler we can read this state 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;">/// Gets the state that has been retrieved from isolated storage</span>
<span style="color: #008080; font-style: italic;">/// in order to re-activated after tombstoning.</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;">string</span> TombstoneState <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</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> Application_Activated<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, ActivatedEventArgs 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><span style="color: #008000;">!</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">IsApplicationInstancePreserved</span><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>PhoneApplicationService<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">State</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ContainsKey</span><span style="color: #008000;">&#40;</span>ModelKey<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      var viewModelState <span style="color: #008000;">=</span> PhoneApplicationService<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">State</span><span style="color: #008000;">&#91;</span>ModelKey<span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">;</span>
      TombstoneState <span style="color: #008000;">=</span> viewModelState<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, we check <code>IsApplicationInstancePreserved</code>, if this is true, we do not need to use the state that was saved during deactivation, this allows for fast-application switching. </p>
<p>Unfortunately as this point our UI has not been created and our JavaScript application code is not running, which is why the tombstoned state is simply stored in a public property of our application. To pick this state up, we add a new step to our JavaScript view model creation code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// phonegap is initialised</span>
<span style="color: #003366; font-weight: bold;">function</span> onDeviceReady<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// create the view model</span>
  twitterSearchViewModel <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> TwitterSearchViewModel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// ---&gt; check for tombstoned state</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;getState&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// instantiate the view an bind</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#twitterSearchView&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">tmpl</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#app&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  ko.<span style="color: #660066;">applyBindings</span><span style="color: #009900;">&#40;</span>twitterSearchViewModel<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When the PhoneGap view is created, we add a handler to the <code>ScriptNotify</code> event, allowing us to handle this <code>getState </code>notification:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><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;
  phoneGapView<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>
<span style="color: #008000;">&#125;</span>
&nbsp;
&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: #6666cc; font-weight: bold;">string</span> commandStr <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>commandStr <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;getState&quot;</span> <span style="color: #008000;">&amp;&amp;</span> App<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TombstoneState</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>
    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;setState&quot;</span>, <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> App<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TombstoneState</span> <span style="color: #008000;">&#125;</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>This checks for the presence of tombstone state, and if found, invokes setState back on our JavaScript view model:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// sets the view model state based on the given JSON string.</span>
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setState</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>stateString<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> state <span style="color: #339933;">=</span> $.<span style="color: #660066;">parseJSON</span><span style="color: #009900;">&#40;</span>stateString<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      that <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">isSearching</span><span style="color: #009900;">&#40;</span>state.<span style="color: #660066;">isSearching</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">searchTerm</span><span style="color: #009900;">&#40;</span>state.<span style="color: #660066;">searchTerm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">tweets</span>.<span style="color: #660066;">removeAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>state.<span style="color: #660066;">tweets</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    that.<span style="color: #660066;">tweets</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note, re-creating our view-model form JSON data is a little more involved than the opposite. I have also cheated a little here, rather than re-creating each <code>TweetViewModel</code> I am using the JSON representation, because this view model has no public functions (i.e. commands).</p>
<h2>Conclusions</h2>
<p>With the above code the PhoneGap application now successfully handles all of the WP7 lifecycle states and transitions. There are a couple of things to note if you try to run this code yourself:</p>
<ol>
<li>You can force tombstoning via the Debug properties, &#8220;Tombstone upon deactiviation while debugging&#8221;.</li>
<li>I have fund that the WebBrowser on the emulator does not tombstone correctly, when your application resumes the WebBrowser control fails to execute <strong>any</strong> JavaScript! Fortunately on  a real device it works just fine.</li>
</ol>
<p>Now that I can tombstone a PhoneGap application, I feel that it is one step closer to be a viable solution for application development. The final thing that I still haven&#8217;t quite worked out yet is navigation and back-button support. Fortunately Knockout has a lot to offer in this area as well, but more on that later &#8230;</p>
<p>You can download the full sourcecode (including PhoneGap library mods) here: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/PhoneGapExample.zip'>PhoneGapExample.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/10/tombstoning-with-phonegap-for-windows-phone-7-and-knockoutjs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WinRT Transitions &#8211; Creating Fast and Fluid Metro UIs</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/10/winrt-transitions-creating-fast-and-fluid-metro-uis/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/10/winrt-transitions-creating-fast-and-fluid-metro-uis/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 12:02:02 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[WinRT]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1735</guid>
		<description><![CDATA[This blog post looks at the new concept of &#8216;transitions&#8217; that WinRT, within Windows 8, introduces. This concept makes it very easy for you to create a fluid and interactive UI without going anywhere near storyboards! I have to admit it, I am a big fan of the Metro Design Language; the clean chrome-free graphics, [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F10%252Fwinrt-transitions-creating-fast-and-fluid-metro-uis%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22WinRT%20Transitions%20-%20Creating%20Fast%20and%20Fluid%20Metro%20UIs%20%23%22%20%7D);"></div>
<p><em>This blog post looks at the new concept of &#8216;transitions&#8217; that WinRT, within Windows 8, introduces. This concept makes it very easy for you to create a fluid and interactive UI without going anywhere near storyboards!</em></p>
<p>I have to admit it, I am a big fan of the <a href="http://en.wikipedia.org/wiki/Metro_(design_language)">Metro Design Language</a>; the clean chrome-free graphics, combined with typography inspired by signage and designed for maximum legibility, has given Windows Phone 7 an instantly recognisable style. However, whilst Silvelight applications developed for Windows Phone 7 look like the native metro phone apps, they do not move like them. Motion is an important part of metro, with one of the tenants being that it is &#8220;Alive In Motion&#8221;. This lead me to publish a multi-part blog series, called Metro In Motion, which provided implementations of the various native animations and transitions (<a href="http://www.scottlogic.co.uk/blog/colin/2011/03/metro-in-motion-fluid-list-animation/">1</a>, <a href="http://www.scottlogic.co.uk/blog/colin/2011/03/metro-in-motion-part-2-peel-animations/">2</a>, <a href="http://www.scottlogic.co.uk/blog/colin/2011/04/metro-in-motion-3-flying-titles/">3</a>, <a href="http://www.scottlogic.co.uk/blog/colin/2011/05/metro-in-motion-part-4-tilt-effect/">4</a>, <a href="http://www.scottlogic.co.uk/blog/colin/2011/05/metro-in-motion-5-sandwichflow/">5</a>, <a href="http://www.scottlogic.co.uk/blog/colin/2011/06/metro-in-motion-part-6-rolling-list-location-indicator/">6</a> &#038; <a href="http://www.scottlogic.co.uk/blog/colin/2011/06/metro-in-motion-part-7-panorama-prettiness-and-opacity/">7</a>).</p>
<p>Windows 8 again is built using the Metro Design Language, and again the tools make it easy for you to create an application which has the Metro look. However, where Windows 8 differs from Windows Phone 7 is that developer APIs make it very easy for you to create applications that move like a Metro application. WinRT introduces a new XAML concept, transitions, which is the focus of this blog post. </p>
<p>I was inspired to explore transitions after watching John Papa&#8217;s excellent //build/ session <a href="http://channel9.msdn.com/Events/BUILD/BUILD2011/APP-494T">&#8220;Stand out with styling and animation in your XAML app&#8221;</a>, I would thoroughly encourage you to watch it!</p>
<p>I have a (very shaky) video of the demo code included with this blog post below:</p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/6e-oP1QS23M" frameborder="0" allowfullscreen></iframe></p>
<p>With WinRT UIElement, the base class for any element that is added to the visual tree (UI), has a <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.transitions(v=VS.85).aspx">Transitions</a> property. You can use this to specify a collection of transitions for an individual element:</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;Rectangle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Rectangle.Transitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TransitionCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RepositionThemeTransition</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;EntranceThemeTransition</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TransitionCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Rectangle.Transitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Rectangle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Each transitions is an animation that the WinRT framework plays in response to certain &#8216;events&#8217;. We&#8217;ll take a look at the various transitions in turn&#8230;</p>
<h2>EntranceThemeTransition</h2>
<p>When elements are added to the UI they appear instantly (as you might expect), however, with the <code>EntranceThemeTransition </code>elements gracefully fade and slide into location. <img class="aligncenter size-full wp-image-1738" title="EntranceTheme" src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/EntranceTheme.png" alt="" width="134" height="274" />This animation will fire whenever your element is added to the UI, this could be as a result of:</p>
<ul>
<li>The application first loading, any element with this transitions specified will animate when the application starts.</li>
<li>The element being added programatically or as a result of binding (e.g. within a bound list)</li>
<li>The element being added as a child of another element</li>
</ul>
<p>Basically, you can rely on this animation being fired whene your element is first visible to the user.</p>
<h2>ChildrenTransitions</h2>
<p>If you add multiple elements to the UI at the same time, or a UserControl is loaded that contains a number of animated elements, their <code>EntranceThemeTransition </code>animations will fire at the same time. However, if you have a panel that contains a number of elements, you can make them appear in sequence by adding the <code>EntranceThemeTransition </code>to the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls(v=VS.85).aspx">ChildrenTransitions</a> property of the panel. For example, any element added to the WrapGrid defined below, will animate as it appears:</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;WrapGrid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;WrapGrid.ChildrenTransitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TransitionCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;EntranceThemeTransition</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TransitionCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/WrapGrid.ChildrenTransitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/WrapGrid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Here we can see nine rectangles being animated as they slide into location:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/ChildTransitions.png" alt="" title="ChildTransitions" width="500" height="231" class="aligncenter size-full wp-image-1743" /></p>
<p>The animations are fired in sequence, no matter how the elements are added to the panel. For example, if you add a couple of elements in code-behind, they will appear in sequence.</p>
<p>(As an aside, this is really quite cool, I can imagine how I might implement the animations on individual elements in WPF / Silverlight, however, the child-transitions concept would be vary hard to implement!)</p>
<p>One thing to note is that the framework fires the animations in the order that elements appear in the <code>Panel.Children</code> collection, this is not necissarily the order they appear in the UI. For example, the following example shows elements added in a Grid, where the elements are defined in a random order within XAML:</p>
<p><img class="aligncenter size-full wp-image-1737" title="ChildTransitions2" src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/ChildTransitions2.png" alt="" width="373" height="336" /></p>
<p>This doesn&#8217;t look quite so good!</p>
<h2>RepositionThemeTransition</h2>
<p>When you change the positions of an element, either directly or as a result of its parent layout changing, it will move into its new location instantly. By adding a <code>RepositionsThemeTransition</code>, the WinRT framework will animate the element as its moves location.</p>
<p>A few things I have observed:</p>
<ul>
<li>The <code>RepositionThemeTransition </code>is not fired when you apply a RenderTransform to an element, possibly because transitions are applied via render transforms</li>
<li>The <code>RepositionThemeTransition </code>is not fired when an elements position within a Canvas changes. This is probably because Canvas.Left and Canvas.Top do not have any impact on layout.</li>
<li>This transition, and none of the others discussed in this blog post, work when the elements are hosted within a <code>FlipView </code>(not sure why, this feels like a bug)</li>
</ul>
<p>Again, if you add a <code>RepositionThemeTransition </code>to the children of a panel via <code>ChildrenTransitions</code>, the animations are fired in sequence. In the below image an element has been removed from the top of a <code>WrapGrid</code>:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/Grid.png" alt="" title="Grid" width="500" height="305" class="aligncenter size-full wp-image-1740" /></p>
<p>(Trust me &#8211; this looks much better in action! try running the sample code with this blog-post)</p>
<p>One point of interest, you can simplify the XAML for adding <code>ChildrenTransitions </code>when using an <code>ItemsControl </code>(Or <code>ListBox</code>, <code>GridView </code>or other subclasses) from the following:</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;ItemsControl</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;itemsTwo&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;WrapGrid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;WrapGrid.ChildrenTransitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TransitionCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RepositionThemeTransition</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TransitionCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/WrapGrid.ChildrenTransitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/WrapGrid<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;/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<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>By using the <code>ItemContainerTransitions </code>property as follows:</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;ItemsControl</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;itemsTwo&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.ItemContainerTransitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TransitionCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RepositionThemeTransition</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TransitionCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl.ItemContainerTransitions<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;ItemsPanelTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;WrapGrid</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<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This property specifies a collection of transitions that will be set on the item-container.</p>
<h2>AddDeleteThemeTransition</h2>
<p>This transitions is intended for use with panels, it animates the process of adding / removing elements. This transitions is added to the <code>ChildrenTransitions </code>(or <code>ItemContainerTransitions</code>) just like the others. As items are added, they fade into place, and as they are removed they slide out and the other elements reposition themselves. One thing that is really cool about this transitions is that it &#8216;understand&#8217; the layout of the panel. </p>
<p>Here you can see elements being repositioned within a <code>WrapGrid</code> when the first element is being removed. You can see that the elements are moved in batches based on their row / column:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/WrapGrid.png" alt="" title="WrapGrid" width="613" height="270" class="aligncenter size-full wp-image-1739" /></p>
<p>Very cool!</p>
<h2>ContentThemeTransition</h2>
<p>The final transition I want to look at is the <code>ContentThemeTransition</code>. For those of you who have worked with Silverlight or WP7, this provides the same functionality as the popular <code>TransitioningContentControl</code> from the Slverlight toolkit &#8211; now this feature is delivered by the framework itself (at last!)</p>
<p>To enable this, add a <code>ContentThemeTransition </code>to the <code>ContentTransitions</code> property (note, not, the <code>Transitions </code>property!).</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;ContentControl</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;contentCtrl&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ContentControl.ContentTransitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TransitionCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ContentThemeTransition</span> <span style="color: #000066;">HorizontalOffset</span>=<span style="color: #ff0000;">&quot;200&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TransitionCollection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ContentControl.ContentTransitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ContentControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Whenever the content of this control is changed, either by setting the <code>Content </code>property directly, or as a result on the databound content changing, the new content gracefully slides into place.</p>
<h2>Conclusions</h2>
<p>Whilst WinRT borrows much from Silverlight and WPF, it brings some great new controls and features and transitions are probably one of my favourite new features. They really help you create fluid applications with minimum effort.</p>
<p>I have covered all the theme-transitions except one, ReorderThemeTransition, I have not yet found out how to use this transition, I think this might be because of <a href="http://social.msdn.microsoft.com/Forums/en-AU/winappswithcsharp/thread/fc3705c1-ae0c-483e-a209-8c3aeefd02c8">a lack of sorting / filtering of collections in WinRT</a>. This is probably functionality that will be added at a later date.</p>
<p>You can download the full sourcecode for this blog post: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/WinRTTransitions.zip'>WinRTTransitions.zip</a></p>
<p>Regards, Colin E</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/10/winrt-transitions-creating-fast-and-fluid-metro-uis/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Developing Windows Phone 7 HTML5 apps with PhoneGap</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/09/developing-windows-phone-7-html5-apps-with-phonegap/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/09/developing-windows-phone-7-html5-apps-with-phonegap/#comments</comments>
		<pubDate>Thu, 29 Sep 2011 15:37:44 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[cross-platform]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[PhoneGap]]></category>
		<category><![CDATA[Windows Phone 7]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1692</guid>
		<description><![CDATA[This article show the step-by-step development of a Windows Phone 7 HTML5 application using PhoneGap. It also looks at how viable this approach is for cross-platform mobile development. Introduction &#8230; and Why HTML5? Windows Phone 7 allows native application development in both Silverlight and XNA, both of which are mature framework with excellent tool support. [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F09%252Fdeveloping-windows-phone-7-html5-apps-with-phonegap%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Developing%20Windows%20Phone%207%20HTML5%20apps%20with%20PhoneGap%20%23%22%20%7D);"></div>
<p><em>This article show the step-by-step development of a Windows Phone 7 HTML5 application using PhoneGap. It also looks at how viable this approach is for cross-platform mobile development.</em></p>
<h2>Introduction &#8230; and Why HTML5?</h2>
<p>Windows Phone 7 allows native application development in both Silverlight and XNA, both of which are mature framework with excellent tool support. So why would you want to develop an application with HTML5 / JavaScript instead? Personally I think the only viable reason for doing this (other than just for fun!) is to develop a cross-platform mobile application. HTML5 / JavaScript applications are platform agnostics, running on Android, iPhone, BlackBerry and now with the Mango WP7 supporting IE9, WP7.<br />
In this blog post I will describe how I implemented the application shown below, which allows you to search properties for sale, using the GPS and various webservices to find and geocode your current location:</p>
<p><iframe src="http://www.youtube.com/embed/bpCo4O69WPs" frameborder="0" width="560" height="315"></iframe></p>
<p>The basic concept behind HTML5 applications is that your native application is simply a full-screen WebBrowser, which hosts your JavaScript application logic. In order to achieve this in practice, you need to package the HTML, JavaScript, CSS and other resources into a XAP file, then use the WebBrowser APIs to pass messages between your JavaScript code and the native APIs in order to access the phone features such as the camera, accelerometer etc &#8230;</p>
<p>A few people have developed frameworks to assist in development of HTML5 apps, for example the <a href="http://htmlappwp7.codeplex.com/">HTML App Host Framework</a>. However, the one I would recommend is <a href="http://www.phonegap.com/">PhoneGap</a>, as the name implies, this framework fills the gap between your JavaScript code and the native phone features. PhoneGap is an open source project that has been running for a couple of years now; currently a large range of phones are supported, meaning that you are guaranteed the same JavaScript APIs for accessing the phone features on Android, iPhone, BlackBerry and more.</p>
<p>PhoneGap support for Windows Phone 7 was initiated by Matt Lacey, who created an <a href="http://blog.mrlacey.co.uk/2010/08/wp7dev-phonegap-and-windows-phone-7.html">initial implementation of the PhoneGap APIs</a>. More recently Nitobi (who run PhoneGap), announced <a href="http://blogs.nitobi.com/jesse/2011/09/08/pg-wp7mango/">an official beta release</a>, which they have worked on in conjunction with Microsoft.</p>
<h2>Getting Started With PhoneGap</h2>
<p>PhoneGap has a project template which will help you create an initial “Hello World” style application. You can get started by following these steps:</p>
<ol>
<li> Download the WP7 PhoneGap from https://github.com/phonegap/phonegap-wp7</li>
<li> Copy the file GapAppStarter.zip, which contains the project template, to the folder</li>
<ul>
<li> <code>\My Documents\Visual Studio 2010\Templates\ProjectTemplates\</code></li>
</ul>
<li> Create a new project using the GappAppStart project template. I could not locate this template within the tree of ‘Installed Templates’, so used the search function. See the image below.</li>
<li> Add the framework\WP7GapClassLib.csproj project to your solution and add a reference to this project.</li>
<li>Build and run!</li>
</ol>
<p><a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/PhoneGapProject.png"><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/PhoneGapProject-300x211.png" alt="" title="PhoneGapProject" width="300" height="211" class="aligncenter size-medium wp-image-1694" /></a></p>
<p>If you followed these steps correctly, you should see the following:</p>
<p><a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/HelloWorld.png"><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/HelloWorld-165x300.png" alt="" title="HelloWorld" width="165" height="300" class="aligncenter size-medium wp-image-1696" /></a></p>
<h2>Dissecting the Example App</h2>
<p>The example application is a bit more complex than an equivalent Silverlight ‘Hello World’ app, so we’ll look at it in some detail &#8230;</p>
<p>The www folder contains the HTML5 application sourcecode, here you place images, HTML, JavaScript, and CSS. These files are marked as ‘content’ and will be included within the XAP file with the same directory structure. The project contains a <code>GapSourceDictionary.xml</code> file, this XML file lists all the HTML application resources. When the application starts, this XML file is read, and each file is added to isolated storage so that it can be served by the WebBrowser control:</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;GapSourceDictionary<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;FilePath</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;www/index.html&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;FilePath</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;www/phonegap.1.0.js&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;FilePath</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;www/master.css&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/GapSourceDictionary<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The MainPage.xaml, which is the Silverlight UI for the application contains an instance of PGView:</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;phone:PhoneApplicationPage</span> </span>
<span style="color: #009900;">    ... <span style="color: #000066;">xmlns:my</span>=<span style="color: #ff0000;">&quot;clr-namespace:WP7GapClassLib;assembly=WP7GapClassLib&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 style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;my:PGView</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 style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phone:PhoneApplicationPage<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>PGView is defined in the WP7GapClassLib, it is a UserControl which hosts a WebBrowser, and contains the code that bridges between the PhoneGap JavaScript APIs and the WP7 .NET APIs. On startup it will load your files into isolated storage and navigate the browser control to <code>“www/index.html”</code> (although this will probably be configurable in future).</p>
<p>The <code>index.html</code> file from the template project is shown below (minus a few un-important parts):</p>

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

<p>The important parts to note here are, firstly the viewport metadata, which relates to the complications that small mobile screens present to layout, resulting in the need for a separate layout viewport and visual viewport, as described in the article <a href="http://www.quirksmode.org/mobile/viewports2.html">“a tale of two viewports“</a>. Here the width is set to 320px, so each pixel-width in our HTML is two pixels on screen. I prefer one-to-one correlation, so would change this to the physical phone dimensions. The “user-scalable=no” should prevent the user from being able to pinch-zoom the page, unfortunately on my (mango) phone, this doesn’t seem to be working. This is a shame, because it really spoils the illusion of this being an application rather than a web-page.</p>
<p>When the DOM is loaded, the init function is invoked, which adds a handler for the deviceready event. When this has been fired, it is safe to use the PhoneGap APIs. This example does not actually use any of the APIs, but you can make a simple change to get the phone name via <a href="(http://docs.phonegap.com/phonegap_device_device.md.html#device.name">device.name</a> for example to verify that PhoneGap really is working.<br />
So how does the JavaScript / Silverlight bridge work?  The PhoneGap.js file communicates via window.external.Notify, which raises the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.scriptnotify(v=vs.95).aspx">ScriptNotify </a>event within the WebBrowser control. Encoded strings are sent to the PGView control which uses the command pattern to identify the class which performs the required function, and executes it.</p>
<h2>Creating the Property Search Application</h2>
<p>Following the principles of <a href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript">Unobtrusive JavaScript</a>, where presentation is separated from logic, the I have moved application logic into a file <code>propertySearch.js</code> (adding this to <code>GapSourceDictionary.xml</code> of course). The HTML has the following markup, a simple form that can be used to input the string to search for:</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;h1</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;welcomeMsg&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Find A Home<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;form&quot;</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;searchText&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;searchButton&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Go&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;getLocationButton&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;My Location&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The event handlers are wired up as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;deviceready&quot;</span><span style="color: #339933;">,</span> onDeviceReady<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// phonegap is initialised</span>
<span style="color: #003366; font-weight: bold;">function</span> onDeviceReady<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// verify that the phone is ready!</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#welcomeMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span>searchProperties<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#getLocationButton&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span>getGeolocation<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note, I have also added jQuery to the project. The code which appends an ellipsis (&#8230;) to the title is a subtle indication that the deviceready event has fired. I found when using the emulator that often the JavaScript code within the page was not being executed at all, this seems to have nothing to do with the PhoneGap JavaScript code. Fortunately the code is always executed when deployed to a real device.</p>
<p>The search properties function is a simple AJAX call to the <a href="http://www.nestoria.co.uk/help/api">Nestoria APIs</a>, using the jQuery ajax() function, which manages the <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> process of dynamically adding a script tag to the DOM which fetches the required data.</p>

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

<p>NOTE: The application uses the Nestoria UK APIs, so if you are trying it out in your emulator, set your location to somewhere in the UK!</p>
<p>I am using a <a href="http://api.jquery.com/category/plugins/templates/">jQuery templates</a> to render each item. You can render an array of items via the <code>tmpl()</code> function, however, here I render each one individually, so that I can associate the raw JSON data for each property with the generated element via the jQuery <code>data()</code> function. This is rather like defining the <code>DataContext </code>for a Silverlight FrameworkElement.</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;script</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;propertyTemplate&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/x-jquery-tmpl&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;property&quot;</span> <span style="color: #000066;">onClick</span>=<span style="color: #ff0000;">&quot;propertyClick(this); return false;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;propertyContainer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;thumbnailColumn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;${thumb_url}&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;thumbnail&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;detailsColumn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;title&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>${title}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;price&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>${price_formatted}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// handle clicks on properties</span>
<span style="color: #003366; font-weight: bold;">function</span> propertyClick<span style="color: #009900;">&#40;</span>propertyNode<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// get the property</span>
  <span style="color: #003366; font-weight: bold;">var</span> property <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>propertyNode<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;propertyData&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// render the template</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#detailsContainer&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $.<span style="color: #660066;">tmpl</span><span style="color: #009900;">&#40;</span>propertyDetailsTemplate<span style="color: #339933;">,</span> property<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#detailsContainer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// switch pages</span>
  showDetailsPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The template is shown here:</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;script</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;propertyDetailsTemplate&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/x-jquery-tmpl&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${price_formatted}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;${img_url}&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;propertyImage&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>			  
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${title}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;bedrooms&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>bedrooms: ${bedroom_number}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;bathrooms&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>bathrooms: ${bathroom_number}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;summary&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>${summary}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The navigation between pages is achieved by having the markup for both pages present in the DOM:</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;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;page searchPage&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
     ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;page detailsPage&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The pages are styled to use absolute positioning:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">div.<span style="color: #000000; font-weight: bold;">page</span> 
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">480px</span><span style="color: #00AA00;">;</span>  
<span style="color: #00AA00;">&#125;</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> showSearchPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.detailsPage&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">480</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.searchPage&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> showDetailsPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.detailsPage&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.searchPage&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">480</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It is worth noting that if the IE9 browser supported CSS3 transitions all of the above could have been done declaratively within CSS, unfortunately animations are not in <a href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2011/09/22/ie9-mobile-developer-overview.aspx">the list of supported features</a>. There is nothing wrong with jQuery animations, they have a very concise and simple syntax. However, CSS3 animation give the browser the option to use GPU acceleration, <a href="http://www.html5rocks.com/en/tutorials/speed/html5/">greatly improving performance</a>. The webkit browsers on Android and iOS support this feature (using the –webkit prefix on the required CSS properties).</p>
<p>The current location is identified via <a href="http://docs.phonegap.com/phonegap_geolocation_geolocation.md.html">navigator.geolocation.getCurrentPosition</a>, using the <a href="http://msdn.microsoft.com/en-us/library/ff701713.aspx">Bing Maps REST APIs</a> to geocode from a lat / long coordinate to a postcode:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// gets the current phone location</span>
<span style="color: #003366; font-weight: bold;">function</span> getGeolocation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  navigator.<span style="color: #660066;">geolocation</span>.<span style="color: #660066;">getCurrentPosition</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>position<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// geocode via Bing Maps</span>
    <span style="color: #003366; font-weight: bold;">var</span> apiKey <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Ai9-KNy6Al-r_ueyLuLXFYB_GlPl-c-_iYtu16byW86qBx9uGbsdJpwvrP4ZUdgD&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> query <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;http://dev.virtualearth.net/REST/v1/Locations/&quot;</span> <span style="color: #339933;">+</span> position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">latitude</span> <span style="color: #339933;">+</span>
                    <span style="color: #3366CC;">&quot;,&quot;</span> <span style="color: #339933;">+</span> position.<span style="color: #660066;">coords</span>.<span style="color: #660066;">longitude</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;?jsonp=onGeocode&amp;key=&quot;</span> <span style="color: #339933;">+</span> apiKey
&nbsp;
    $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      dataType<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jsonp&quot;</span><span style="color: #339933;">,</span>
      url<span style="color: #339933;">:</span> query
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// handle the geocode results</span>
<span style="color: #003366; font-weight: bold;">function</span> onGeocode<span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// extract the 'outward' part of the postcode</span>
  <span style="color: #003366; font-weight: bold;">var</span> postalCode <span style="color: #339933;">=</span> result.<span style="color: #660066;">resourceSets</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">resources</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">address</span>.<span style="color: #660066;">postalCode</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> codeSplit <span style="color: #339933;">=</span> postalCode.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>codeSplit.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#searchText&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>codeSplit<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">body<span style="color: #00AA00;">,</span> input<span style="color: #00AA00;">,</span> div
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">22.667px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* PhoneFontSizeMedium */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
h2
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">normal</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">32px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* PhoneFontSizeLarge */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
h1
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">normal</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">42.667px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* PhoneFontSizeExtraLarge */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;button&quot;</span><span style="color: #00AA00;">&#93;</span><span style="color: #00AA00;">,</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span><span style="color: #00AA00;">&#93;</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">4px</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* PhoneBorderThickness */</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">25.333px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* PhoneFontSizeMediumLarge */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;button&quot;</span><span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:active</span><span style="color: #00AA00;">,</span> input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span><span style="color: #00AA00;">&#93;</span><span style="color: #3333ff;">:active
</span><span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
input<span style="color: #00AA00;">&#91;</span>type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text&quot;</span><span style="color: #00AA00;">&#93;</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">150px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">4px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/MangoStyle.png" alt="" title="MangoStyle" width="600" height="193" class="aligncenter size-full wp-image-1697" /></p>
<h2>ProgressBar</h2>
<p>For a bit of fun I thought I’d try and create a HTML equivalent of the ‘trailing dots’ WP7 ProgressBar. With a simple bit of markup / CSS:</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;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;progress&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;pip&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;pip&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;pip&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;pip&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;pip&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.progress</span> div
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>  
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">green</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.progress</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>And some further jQuery animations, this time making use of the easing plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> onDeviceReady<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  ...
&nbsp;
  <span style="color: #006600; font-style: italic;">// create an animation loop for the progress bar;</span>
  startAnimation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> tid <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span>startAnimation<span style="color: #339933;">,</span> <span style="color: #CC0000;">3500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> startAnimation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> delay <span style="color: #339933;">=</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.pip&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    animatePip<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> delay<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    delay <span style="color: #339933;">+=</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> animatePip<span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> delay<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  element.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;left&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">delay</span><span style="color: #009900;">&#40;</span>delay<span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">240</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> easing<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;easeOutSine&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">480</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> easing<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;easeInSine&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We get something which approximates the WP7 progress bar:</p>
<p><iframe src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/progress.html" width="480" height="25" scrolling="no"></iframe></p>
<p>(Note, sometimes the animation goes a bit crazy, try refreshing your browser to reset it!)</p>
<p>Again, my comments regarding CSS3 animations and GPU acceleration apply here also.</p>
<h2>Tombstoning</h2>
<p>As I have discussed in <a href="http://www.scottlogic.co.uk/blog/colin/2011/05/a-simple-windows-phone-7-mvvm-tombstoning-example">a previous blog post</a>, tombstoning is a tricky task for WP7 developers. When your application is tombstoned you can save application state, page state – and the framework stores your back-stack URIs. It is your responsibility to re-start your application in the same state. Contrary to the belief of some developers I have talked to, Mango does not remove the need to tombstone, it just means that your application is likely to be tombstoned less often, with the suspended state being used to park your application while application switching.</p>
<p>So, how does tombstoning work with a PhoneGap HTML5 application? Good question! Implanting tombstoning would probably require some custom communication, outside of the PhoneGap APIs, that allows the JavaScript application to provide its current state to the Silverlight application that hosts this.</p>
<p>I would be very interested to hear from anyone who has solved this issue!</p>
<h2>The Development Process</h2>
<p>Attempting to develop JavaScript applications with the browser within the WP7 emulator is not a pleasant process. Any JavaScript errors, including simple parsing errors, typically result in the HTML being rendered but the scripts ignored. A much better approach is to run your HTML / JavaScript within a browser, this gives you access to the usual developer tools such as Firebug, or the built in Chrome / IE tools.</p>
<p>However, to do this, you need to mock the PhoneGap APIs. For my application, I found the following did the trick:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">addEventListener</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>evt<span style="color: #339933;">,</span> handler<span style="color: #339933;">,</span> capture<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;body&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>evt<span style="color: #339933;">,</span> handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;body&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">trigger</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;deviceready&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>OK, I know what you are thinking, what about the geolocation code? Where is the mock for these PhoneGap APIs? The answer is that a number of the PhoneGap APIs are designed to exactly match the corresponding HTML5 specification. So for example, the PhoneGap geolocation API is exactly the same as the HTML5 geolocation APIs. For phones such as WP7 where the browser supports this HTML5 feature, PhoneGap does nothing, for phones that do not, PhoneGap provides an implementation (using the native APIs).</p>
<p>If the PhoneGap mock is so simple, you might be wondering, why use PhoneGap at all? Well, PhoenGap is still giving us a mechanism for packaging files into a XAP file in such a way that they can be rendered by the browser.</p>
<h2>How cross-platform is this approach?</h2>
<p>I would say that this approach is probably about as cross-platform as any HTML5 / JavaScript browser based application. There are always going to be cross-browser differences to overcome. As a test, I ran this code on an iPod Touch without any modification. The results are pretty good, although there are some quirks visible:</p>
<p><iframe width="480" height="360" src="http://www.youtube.com/embed/YGPw_0_5Fc0" frameborder="0" allowfullscreen></iframe></p>
<h2>How viable are HTML5 applications?</h2>
<p>It is clear that there is a growing trend towards cross-platform HTML5 applications, and Microsoft seem to be in support of this concept (even though the JavaScript / HTML5 Metro applications within Win8 are not cross-platform!). Microsoft has worked with Nitobi to create the PhoneGap for WP7, it was also announced at the //build/ conference that Microsoft would be working with jQuery Mobile to create a Metro theme for their mobile controls. </p>
<p>Currently, PhoneGap for WP7 is in beta, and it is certainly a little rough around the edges. This will no doubt improve in time. The large list of <a href="http://www.phonegap.com/apps/">applications written with PhoneGap</a>, certainly indicate that this is a viable solution for application development.</p>
<p>A larger obstacle for WP7 HTML5 applications is the IE9 browser that runs in the Mango phone. Whilst it has a pretty impressive list of HTML5 features supported, there are a couple of browser features / issues that I cannot resolve. These make it obvious to any user that this is in fact a browser application, totally spoiling the ‘illusion’</p>
<ol>
<li><code>user-scalable=no</code> – currently this setting seems to be ignored. This means that the user can pinch your application, which makes it feel like a web page. <b>UPDATE</b> Roy, in a comment below pointed out that the viewport parameters should be comma-separated. This <i>almost</i> solves the issue. The user can still pinch the view, which causes it to zoom, but when the pinch finishes, the viewport returns to the original scale. Not perfect, but better than nothing!</li>
<li>There doesn’t seem to be any way to <a href="http://stackoverflow.com/questions/6378008/windows-phone-7-browser-turn-off-the-gray-shading-when-links-are-clicked">turn of the gray shaded rectangle that appears over links when you click them</a>. With applications that have dynamic content, this can look pretty ridiculous, with a gray rectangle lingering on screen while the page content changes underneath it. With the Android / iPhone browsers this can be turned off in CSS via <code>-webkit-tap-highlight-color</code></li>
</ol>
<p>Hopefully these limitations will be resolved. When they do, I am quite sure that HTML5 will be a viable technology for creating quality phone applications.</p>
<p>You can download the project sourcecode here: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/PhoneGapExample.zip'>PhoneGapExample.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/09/developing-windows-phone-7-html5-apps-with-phonegap/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>XAML Finance &#8211; a cross-platform WPF, Silverlight, WP7 application</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/09/xaml-finance-a-cross-platform-wpf-silverlight-wp7-application/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/09/xaml-finance-a-cross-platform-wpf-silverlight-wp7-application/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 14:03:13 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[codeproject]]></category>
		<category><![CDATA[cross-platform]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1682</guid>
		<description><![CDATA[I have just published a new article on codeproject which describes the development of XAMLFinance, a cross-platform application for the desktop (WPF), web (Silverlight) and phone (WP7). Head over to codeproject to read about the development of this application and download the sourcecode. Regards, Colin E.]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F09%252Fxaml-finance-a-cross-platform-wpf-silverlight-wp7-application%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22XAML%20Finance%20-%20a%20cross-platform%20WPF%2C%20Silverlight%2C%20WP7%20application%20%23%22%20%7D);"></div>
<p>I have just published a <a href="http://www.codeproject.com/KB/windows-phone-7/XAMLFinance.aspx">new article on codeproject</a> which describes the development of XAMLFinance, a cross-platform application for the desktop (WPF), web (Silverlight) and phone (WP7).</p>
<p><a href="http://www.codeproject.com/KB/windows-phone-7/XAMLFinance.aspx"><br />
<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><a href="http://www.codeproject.com/KB/windows-phone-7/XAMLFinance.aspx">Head over to codeproject</a> to read about the development of this application and download the sourcecode.</p>
<p><a href="http://www.codeproject.com/KB/windows-phone-7/XAMLFinance.aspx"><br />
<img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/07/XAMLFinancePanoramaSmall.png" alt="" title="XAMLFinancePanoramaSmall" width="650" height="338" class="aligncenter size-full wp-image-1610" /></a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/09/xaml-finance-a-cross-platform-wpf-silverlight-wp7-application/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Implementing a Windows Phone 7 Conversation View Part Two</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/08/implementing-a-windows-phone-7-conversation-view-part-two/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/08/implementing-a-windows-phone-7-conversation-view-part-two/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 08:28:50 +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=1617</guid>
		<description><![CDATA[This blog post describes how to implement a conversation / messaging style application with Windows Phone 7. It covers how to style the speech bubbles and the scrolling of the conversation list view when the phone keyboard is shown. A couple of weeks ago I wrote a blog post which described the creation of a [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F08%252Fimplementing-a-windows-phone-7-conversation-view-part-two%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Implementing%20a%20Windows%20Phone%207%20Conversation%20View%20Part%20Two%20%23%22%20%7D);"></div>
<p><em>This blog post describes how to implement a conversation / messaging style application with Windows Phone 7. It covers how to style the speech bubbles and the scrolling of the conversation list view when the phone keyboard is shown.</em></p>
<p>A couple of weeks ago I wrote a blog post which described the creation of a <a href="http://www.scottlogic.co.uk/blog/colin/2011/07/a-wp7-conversation-view/" title="Implementing a Windows Phone 7 Conversation View">Windows Phone 7 ConversationView</a>, a view which renders a list of messages so that they look like a conversation. In this blog post I am going to extend the concept further, by adding an input text field allowing you to have a conversation with ELIZA, an A.I. chatterbot. This blog post will look at some of the tricky issue regarding scrolling the list of messages so that the most recent is always visible when responding.</p>
<p>You can see a video of this code in action below:</p>
<p><iframe width="425" height="349" src="http://www.youtube.com/embed/cu2pw98tjZc" frameborder="0" allowfullscreen></iframe></p>
<p>I hoped that I could find a decent C# <a href="http://en.wikipedia.org/wiki/ELIZA">ELIZA</a> (a classic chatter application that takes on the role of a therapist) implementation on the internet, however, the only one <a href="http://www.codeproject.com/KB/library/ProjectEliza.aspx">I found was rather basic</a>. If you know of any alternatives, pleas let me know!</p>
<p>In my previous blog post I created a <code>UserControl</code> which renders each <code>Message</code> instance, where the template used to render the message is dependant on which side of the conversation it relates to. Refer to <a href="http://www.scottlogic.co.uk/blog/colin/2011/07/a-wp7-conversation-view/" title="Implementing a Windows Phone 7 Conversation View">my previous blog post</a> for implementation details.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/07/WP7ConversationDesignTime.png" alt="" title="WP7ConversationDesignTime" width="600" height="328" class="aligncenter size-full wp-image-1604" /></p>
<p>The layout for my simple chat application uses an instance of the <code>ConversationView</code> user control, with a text input located at the bottom of the screen:</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;ContentPanel&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;12,0,12,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;Grid</span> <span style="color: #000066;">contribControls:GridUtils.RowDefinitions</span>=<span style="color: #ff0000;">&quot;,Auto&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;ScrollViewer</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;ConversationScrollViewer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- conversation view --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:ConversationView</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;conversationView&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ScrollViewer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- the text input field --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">contribControls:GridUtils.RowDefinitions</span>=<span style="color: #ff0000;">&quot;,,&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;0,10,0,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;Rectangle</span> <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;White&quot;</span></span>
<span style="color: #009900;">                  <span style="color: #000066;">Grid.RowSpan</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;txt:WatermarkedTextBox</span> <span style="color: #000066;">Watermark</span>=<span style="color: #ff0000;">&quot;type a message&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">TextWrapping</span>=<span style="color: #ff0000;">&quot;Wrap&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">AcceptsReturn</span>=<span style="color: #ff0000;">&quot;True&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">Padding</span>=<span style="color: #ff0000;">&quot;0&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;TextInput&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">GotFocus</span>=<span style="color: #ff0000;">&quot;TextInput_GotFocus&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">LostFocus</span>=<span style="color: #ff0000;">&quot;TextInput_LostFocus&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>            
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Path</span> <span style="color: #000066;">Data</span>=<span style="color: #ff0000;">&quot;m 0,0 l 16,0 l 0,16 l -16,-16&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;White&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;0,0,5,0&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Right&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;2&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 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;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Note the simplified <code>Grid</code> markup from the <a href="http://wp7contrib.codeplex.com/">WP7Contrib codeplex project</a>, where the string <code>",Auto"</code> is used in place of the more verbose <code>RowDefinition</code> XAML elements.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/08/ConversationViewLayout.png" alt="" title="ConversationViewLayout" width="190" height="350" class="aligncenter size-full wp-image-1622" /></p>
<p>The <code>WatermarkedTextBox</code> is <a href="http://www.windowsphonegeek.com/articles/WP7-WatermarkedTextBox-custom-control">from an article by WindowsPhoneGeek</a> (and it works like a charm &#8211; thanks!). A simple <code>Path</code> and <code>Rectangle</code> are added to the layout so that the input field looks like a speech bubble.</p>
<p>When the user clicks on the input TextBlock the phone keyboard will be revealed, allowing them to enter their message. This is where we stumble upon our first major problem!</p>
<p>When the phone keyboard is displayed, your application content is &#8216;pushed&#8217; upwards to make space for the keyboard. Unfortunately, this results in the message which the user is replying to being pushed off the top of the screen &#8230;</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/08/KeyboardIssue.png" alt="" title="KeyboardIssue" width="450" height="386" class="aligncenter size-full wp-image-1620" /></p>
<p>Because our <code>ConversationView</code> sits within a <code>ScrollViewer</code> we can scroll to push the message further down the screen, however, this would require a negative scroll offset, which isn&#8217;t possible! </p>
<p>To circumnavigate this issue, we can add an element which is used to &#8216;push&#8217; the top message downwards so that it is located at the bottom of the <code>ScrollViewer</code>. The following markup adds a <code>Rectangle</code> which sits above the <code>ConversationView</code>, with two Storyboards that expand / collapse the <code>Rectangle</code> allowing us to push the messages down when we need them:</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;ScrollViewer</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;ConversationScrollViewer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</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;Vertical&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;ConversationContentContainer&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- padding element --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Rectangle</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;100&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;0&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;PaddingRectangle&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Rectangle.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;PaddingRectangleShowAnim&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 style="color: #000066;">Storyboard.TargetName</span>=<span style="color: #ff0000;">&quot;PaddingRectangle&quot;</span> </span>
<span style="color: #009900;">                <span style="color: #000066;">Storyboard.TargetProperty</span>=<span style="color: #ff0000;">&quot;(Height)&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">To</span>=<span style="color: #ff0000;">&quot;400&quot;</span>  <span style="color: #000066;">Duration</span>=<span style="color: #ff0000;">&quot;00:00:00.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;Storyboard</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;PaddingRectangleHideAnim&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 style="color: #000066;">Storyboard.TargetName</span>=<span style="color: #ff0000;">&quot;PaddingRectangle&quot;</span> </span>
<span style="color: #009900;">                <span style="color: #000066;">Storyboard.TargetProperty</span>=<span style="color: #ff0000;">&quot;(Height)&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">To</span>=<span style="color: #ff0000;">&quot;0&quot;</span>  <span style="color: #000066;">Duration</span>=<span style="color: #ff0000;">&quot;00:00:00.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;/Rectangle.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Rectangle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- conversation view --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:ConversationView</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;conversationView&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ScrollViewer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>We can detect when the input <code>TextBlock</code> receives focus to determine when the keyboard will be revealed. Using the <a href="http://www.scottlogic.co.uk/blog/colin/2009/09/helpful-extension-methods-for-show-hide-animations-in-silverlight/" title="Helpful extension methods for Show / Hide animations in Silverlight">Show / Hide extension methods I blogged about a while back</a>, we can fire the animations which make this padding rectangle grow or shrink when the keyboard is shown or hidden:</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> TextInput_GotFocus<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>
  PaddingRectangle<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  ApplicationBar<span style="color: #008000;">.</span><span style="color: #0000FF;">IsVisible</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: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> TextInput_LostFocus<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>
  PaddingRectangle<span style="color: #008000;">.</span><span style="color: #0000FF;">Hide</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  ApplicationBar<span style="color: #008000;">.</span><span style="color: #0000FF;">IsVisible</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></pre></div></div>

<p>If we fill the rectangle so that it is visible, you can see how it pushes down the content as shown below:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/08/KeyboardIssueFix.png" alt="" title="KeyboardIssueFix" width="450" height="388" class="aligncenter size-full wp-image-1623" /></p>
<p>The next step is to ensure that the <code>ConversationView</code> is always scrolled so that the most recent message is visible. The <code>ScrollViewer</code> has a <code>ScrollToVerticalOffset</code> method which can be used to programmatically scroll the content, however, because this is not a dependency property it cannot be animated via  <code>Storyboard</code>.</p>
<p>Here I am using the same trick I employed for the Windows Phone 7 Jump List control, where a private dependency property that sets the scroll offset value in its change handler is used as a target for the scrolling Storyboard:</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;">/// VerticalOffset, a private DP used to animate the scrollviewer</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> DependencyProperty VerticalOffsetProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;VerticalOffset&quot;</span>,
  <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>MainPage<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> PropertyMetadata<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0.0</span>, OnVerticalOffsetChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</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;">void</span> OnVerticalOffsetChanged<span style="color: #008000;">&#40;</span>DependencyObject d, DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  MainPage app <span style="color: #008000;">=</span> d <span style="color: #0600FF; font-weight: bold;">as</span> MainPage<span style="color: #008000;">;</span>
  app<span style="color: #008000;">.</span><span style="color: #0000FF;">OnVerticalOffsetChanged</span><span style="color: #008000;">&#40;</span>e<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> OnVerticalOffsetChanged<span style="color: #008000;">&#40;</span>DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  ConversationScrollViewer<span style="color: #008000;">.</span><span style="color: #0000FF;">ScrollToVerticalOffset</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>e<span style="color: #008000;">.</span><span style="color: #0000FF;">NewValue</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Using this dependency property we can create a simple <code>Storyboard</code> and <code>DoubleAnimation</code> that scrolls to reveal the latest message:</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> ScrollConvesationToEnd<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;">// start from the current position</span>
  scrollViewerScrollToEndAnim<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">From</span> <span style="color: #008000;">=</span> ConversationScrollViewer<span style="color: #008000;">.</span><span style="color: #0000FF;">VerticalOffset</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// set the scroll position to the the height of the contained content</span>
  scrollViewerScrollToEndAnim<span style="color: #008000;">.</span><span style="color: #0000FF;">To</span> <span style="color: #008000;">=</span> ConversationContentContainer<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualHeight</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// go!</span>
  scrollViewerStoryboard<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>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And there you have it, a fully functional conversation application. Enjoy!</p>
<p>You can download the complete sourcecode: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/08/WP7ConversationView.zip'>WP7ConversationView.zip</a> </p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/08/implementing-a-windows-phone-7-conversation-view-part-two/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Capturing Windows Phone 7 Panorama Images</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/07/capturing-windows-phone-7-panorama-images/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/07/capturing-windows-phone-7-panorama-images/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 16:00:52 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[codeproject]]></category>
		<category><![CDATA[Windows Phone 7]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1608</guid>
		<description><![CDATA[The Windows Phone 7 Panorama control is widely used in applications and to many has come to symbolise the Metro Design Language. Search for panorama images and you will find numerous promo-shots of Windows Phone 7 applications which display the panoramic contents of the application hub, with an image of a phone above it illustrating [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F07%252Fcapturing-windows-phone-7-panorama-images%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Capturing%20Windows%20Phone%207%20Panorama%20Images%20%23%22%20%7D);"></div>
<p>The Windows Phone 7 Panorama control is widely used in applications and to many has come to symbolise the Metro Design Language. Search for <a href="http://www.google.co.uk/search?tbm=isch&#038;q=windows+phone+7+panorama">panorama images</a> and you will find numerous promo-shots of Windows Phone 7 applications which display the panoramic contents of the application hub, with an image of a phone above it illustrating how the panorama slides through your viewport.</p>
<p>The image below shows a promo-shot for XAMLFinance, an application I have been working on:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/07/XAMLFinancePanoramaSmall.png" alt="" title="XAMLFinancePanoramaSmall" width="650" height="338" class="aligncenter size-full wp-image-1610" /> </p>
<p>Creating images like the one above takes a bit of work, fortunately you can turn off the parallax scrolling effect of the control (as described in <a href="http://www.cjcraft.com/blog/2011/01/19/WP7Tip002HowDoYouCreateWindowsPhone7PanoramaApplicationScreenshots.aspx">this blog post</a>), allowing for easier image stitching, however the process is still quite manual.</p>
<p>For a bit of fun I thought it would be nice to template the <code>Panorama</code> control itself in order to display the full panorama contents on the emulator within a single screen, so that I can create my promo-shot with a single screengrab.</p>
<p>Unfortunately, the <code>Panorama</code> control is a little too tightly coupled to its template to allow this kind of radical re-design, so the next best options is to create a new page that hosts my <code>PanoramaItems</code> within an <code>ItemsControl</code> and style that.</p>
<p>So, we&#8217;ll start with a page which hosts our items within an <code>ItemsControl</code>:</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;ItemsControl</span> <span style="color: #000066;">Style</span>=<span style="color: #ff0000;">&quot;{StaticResource ItemsControlStyle}&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.Background<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ImageBrush</span> <span style="color: #000066;">ImageSource</span>=<span style="color: #ff0000;">&quot;PanoramaBackground.jpg&quot;</span> <span style="color: #000066;">Opacity</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;/ItemsControl.Background<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!--Panorama item one--&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;controls:PanoramaItem</span> <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;item1&quot;</span> <span style="color: #000066;">Style</span>=<span style="color: #ff0000;">&quot;{StaticResource PanoramaItemStyle}&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 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;/controls:PanoramaItem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!--Panorama item two--&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;controls:PanoramaItem</span> <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;item2&quot;</span>  <span style="color: #000066;">Style</span>=<span style="color: #ff0000;">&quot;{StaticResource PanoramaItemStyle}&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 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;/controls:PanoramaItem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!--Panorama item three--&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;controls:PanoramaItem</span> <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;item3&quot;</span>  <span style="color: #000066;">Style</span>=<span style="color: #ff0000;">&quot;{StaticResource PanoramaItemStyle}&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 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;/controls:PanoramaItem<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></pre></div></div>

<p>We need to shrink each of those <code>PanoramaItem</code> instances so that they all fit on a single screen. We can easily shrink them with a <code>ScaleTransform</code>, however Silverlight only supports render transforms, what we really need here is a transform that affects layout also (for a brief overview of the difference see <a href="http://www.scottlogic.co.uk/blog/colin/2008/12/layouttransform-vs-rendertransform-whats-the-difference/">this blog post</a>). Fortunately the <a href="http://silverlight.codeplex.com/">Silverlight Toolkit</a> has a LayoutTransformer class which emulates this behaviour, and whilst it is not available in the WP7 release, copying the code across I found it to be entirely compatible with the Silverlight WP7 version.</p>
<p>We can shrink our PanoramaItems as follows:</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;system:Double</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;Scale&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>0.5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system:Double<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;">x:Key</span>=<span style="color: #ff0000;">&quot;PanoramaItemStyle&quot;</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;controls:PanoramaItem&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;260&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;290&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;Template&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.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;controls:PanoramaItem&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- apply a LayoutTransform to the content in order to scale it --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:LayoutTransformer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:LayoutTransformer.LayoutTransform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ScaleTransform</span> <span style="color: #000066;">ScaleX</span>=<span style="color: #ff0000;">&quot;{StaticResource Scale}&quot;</span></span>
<span style="color: #009900;">                                            <span style="color: #000066;">ScaleY</span>=<span style="color: #ff0000;">&quot;{StaticResource Scale}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:LayoutTransformer.LayoutTransform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
          <span style="color: #808080; font-style: italic;">&lt;!-- the original content of the template follows --&gt;</span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;{TemplateBinding Background}&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;12,0,0,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;Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;auto&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;*&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.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ContentControl</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;header&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">ContentTemplate</span>=<span style="color: #ff0000;">&quot;{TemplateBinding HeaderTemplate}&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;{TemplateBinding Header}&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">FontSize</span>=<span style="color: #ff0000;">&quot;{StaticResource PhoneFontSizeExtraExtraLarge}&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">FontFamily</span>=<span style="color: #ff0000;">&quot;{StaticResource PhoneFontFamilySemiLight}&quot;</span></span>
<span style="color: #009900;">                            <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;10,-2,0,26&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ContentControl.RenderTransform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TranslateTransform</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;headerTransform&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ContentControl.RenderTransform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ContentControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ContentPresenter</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;{TemplateBinding Content}&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;{TemplateBinding HorizontalContentAlignment}&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;{TemplateBinding Padding}&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;{TemplateBinding VerticalContentAlignment}&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 style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:LayoutTransformer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></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>Now we need to styles and template the <code>ItemsControl</code> so that it looks like a Panorama. This is done as follows:</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;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;ItemsControl&quot;</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;ItemsControlStyle&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;VerticalAlignment&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Center&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;HorizontalAlignment&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Center&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;ItemsPanel&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.Value<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;tk:WrapPanel</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;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></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;Template&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.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #808080; font-style: italic;">&lt;!-- create a template for the ItemControl that makes it look like a panorama--&gt;</span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- the grid which displays the  panorama background --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;{TemplateBinding Background}&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.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;auto&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;*&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.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
          <span style="color: #808080; font-style: italic;">&lt;!-- the title, scaled to match the PanoramatItems--&gt;</span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:LayoutTransformer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:LayoutTransformer.LayoutTransform<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ScaleTransform</span> <span style="color: #000066;">ScaleX</span>=<span style="color: #ff0000;">&quot;{StaticResource Scale}&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">ScaleY</span>=<span style="color: #ff0000;">&quot;{StaticResource Scale}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:LayoutTransformer.LayoutTransform<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;{TemplateBinding Tag}&quot;</span></span>
<span style="color: #009900;">                       <span style="color: #000066;">FontSize</span>=<span style="color: #ff0000;">&quot;180&quot;</span></span>
<span style="color: #009900;">                       <span style="color: #000066;">FontFamily</span>=<span style="color: #ff0000;">&quot;{StaticResource PhoneFontFamilySemiLight}&quot;</span></span>
<span style="color: #009900;">                       <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;10,-2,0,26&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:LayoutTransformer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
          <span style="color: #808080; font-style: italic;">&lt;!-- the rendered items --&gt;</span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsPresenter</span> <span style="color: #000066;">Grid.Row</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;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></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 end result is that we can see the entire panorama contents in a screen-shot ready state:</p>
<p><a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/07/Panorama.png"><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/07/PanoramaSmall.png" alt="" title="PanoramaSmall" width="600" height="330" class="aligncenter size-full wp-image-1612" /></a></p>
<p>The big advantage of this approach is that any changes to the application can be reflected in the promo-shot very swiftly. It is also quite good fun having a fully functioning version of your panoramic hub in miniature!</p>
<p>You can download the sourcecode here: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/07/PanoramaShot.zip'>PanoramaShot.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/07/capturing-windows-phone-7-panorama-images/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

