<?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/category/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>A Windows Phone 7 Slide View with Page Pips</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/12/a-windows-phone-7-slide-view-with-page-pips/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/12/a-windows-phone-7-slide-view-with-page-pips/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 11:51:09 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[Windows Phone 7]]></category>

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

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

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

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

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

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

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

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

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

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

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

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/12/a-windows-phone-7-slide-view-with-page-pips/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>Adding Error Bars to Visiblox Silverlight Charts</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/10/adding-error-bars-to-visiblox-silverlight-charts/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/10/adding-error-bars-to-visiblox-silverlight-charts/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 15:11:17 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[charts]]></category>
		<category><![CDATA[databinding]]></category>
		<category><![CDATA[visiblox]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1766</guid>
		<description><![CDATA[Having spent a number of years studying Physics at university, I have had the importance of error bars well and truly drummed into me! Within physics, or any experimental science, there are always going to be errors in the measurements you make. The more repeat measurements you make, the more confident you can be in [...]]]></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%252Fadding-error-bars-to-visiblox-silverlight-charts%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Adding%20Error%20Bars%20to%20Visiblox%20Silverlight%20Charts%20%23%22%20%7D);"></div>
<p>Having spent a number of years studying Physics at university, I have had the importance of error bars well and truly drummed into me! Within physics, or any experimental science, there are always going to be errors in the measurements you make. The more repeat measurements you make, the more confident you can be in the mean value, however you cannot remove the errors altogether. Error bars provide a way to represent the spread of experimental observations graphically, without them, it is hard to have any confidence in the conclusions drawn from the observations!</p>
<p>In this blog post I will show how to implement a custom <a href="http://www.visiblox.com/">Visiblox</a> chart series to render error bars:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/VisibloxErrorBars.png" alt="" title="VisibloxErrorBars" width="499" height="240" class="aligncenter size-full wp-image-1767" /></p>
<p>(The data in the above chart is from a page which details how to calculate the <a href="http://www.ncsu.edu/labwrite/res/gt/gt-stat-home.html">standard error from experimental results</a>).</p>
<h2>Creating a Custom Series</h2>
<p>As described in my previous blog post on <a href="http://www.scottlogic.co.uk/blog/colin/2010/11/adding-a-smoothed-line-series-bezier-curve-to-a-visiblox-chart/" title="Adding a Smoothed Line Series (Bézier curve) to a Visiblox Chart">creating a spline series</a>, to create a new series type, you sub-class one of the Visiblox base-classes, in this case <code>MultiValueSeriesBase </code>is a suitable starting point:</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> ErrorBarSeries <span style="color: #008000;">:</span> MultiValueSeriesBase
<span style="color: #008000;">&#123;</span> 
  <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> FrameworkElement CreatePoint<span style="color: #008000;">&#40;</span>IDataPoint dataPoint<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> NotImplementedException<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;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> RenderDataLabels<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> NotImplementedException<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>I don&#8217;t want data labels, so the only method I need to implement is <code>CreatePoint</code>, which takes the (multi-valued) point to be rendered as its only argument. The lifecycle of point creating and destruction is taken care of by the base-class.</p>
<p>The <code>IDataPoint </code>has a string indexer which is used to retrieve multiple Y values for multi-valued series. It is a good idea to define these in a single place, here we define the three y-values required for an error-bar series:</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;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #6666cc; font-weight: bold;">string</span> ErrorUp <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;ErrorUp&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<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> <span style="color: #6666cc; font-weight: bold;">string</span> ErrorDown <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;ErrorDown&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<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> <span style="color: #6666cc; font-weight: bold;">string</span> Value <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Value&quot;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The <code>CreatePoint</code> implementation for this series creates a Path as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> FrameworkElement CreatePoint<span style="color: #008000;">&#40;</span>IDataPoint dataPoint<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  var lineGeometry <span style="color: #008000;">=</span> BuildGeometry<span style="color: #008000;">&#40;</span>dataPoint<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  Path line <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Path<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  line<span style="color: #008000;">.</span><span style="color: #0000FF;">Stroke</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span>Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Black</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  line<span style="color: #008000;">.</span><span style="color: #0000FF;">Fill</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span>Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Gray</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  line<span style="color: #008000;">.</span><span style="color: #0000FF;">StrokeThickness</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">1.0</span><span style="color: #008000;">;</span>
  line<span style="color: #008000;">.</span><span style="color: #0000FF;">StrokeLineJoin</span> <span style="color: #008000;">=</span> PenLineJoin<span style="color: #008000;">.</span><span style="color: #0000FF;">Bevel</span><span style="color: #008000;">;</span>
  line<span style="color: #008000;">.</span><span style="color: #0000FF;">Data</span> <span style="color: #008000;">=</span> lineGeometry<span style="color: #008000;">;</span>
  line<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>ZoomCanvas<span style="color: #008000;">.</span><span style="color: #0000FF;">IsScaledPathProperty</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">return</span> line<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The <code>BuildGeometry</code> method does most of the work, extracting the values from the IDataPoint, transforming them (via the axis) to the required coordinate system, then creating a suitable geometry:</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;">/// Creates the geometry for the given datapoint</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> PathGeometry BuildGeometry<span style="color: #008000;">&#40;</span>IDataPoint dataPoint<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  var halfWidth <span style="color: #008000;">=</span> SuggestedPointWidth <span style="color: #008000;">*</span> WidthFactor<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// obtain the data values</span>
  var topDataValue <span style="color: #008000;">=</span> dataPoint<span style="color: #008000;">&#91;</span>ErrorUp<span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> IComparable<span style="color: #008000;">;</span>
  var middleDataValue <span style="color: #008000;">=</span> dataPoint<span style="color: #008000;">&#91;</span>Value<span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> IComparable<span style="color: #008000;">;</span>
  var bottomDataValue <span style="color: #008000;">=</span> dataPoint<span style="color: #008000;">&#91;</span>ErrorDown<span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">as</span> IComparable<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// convert to a the required render coordinates</span>
  <span style="color: #6666cc; font-weight: bold;">double</span> topRenderPos <span style="color: #008000;">=</span> YAxis<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDataValueAsRenderPositionWithoutZoom</span><span style="color: #008000;">&#40;</span>topDataValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">double</span> middleRenderPos <span style="color: #008000;">=</span> YAxis<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDataValueAsRenderPositionWithoutZoom</span><span style="color: #008000;">&#40;</span>middleDataValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">double</span> bottomRenderPos <span style="color: #008000;">=</span> YAxis<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDataValueAsRenderPositionWithoutZoom</span><span style="color: #008000;">&#40;</span>bottomDataValue<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #6666cc; font-weight: bold;">double</span> xMiddleRenderPos <span style="color: #008000;">=</span> XAxis<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDataValueAsRenderPositionWithoutZoom</span><span style="color: #008000;">&#40;</span>dataPoint<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">double</span> xRightRenderPos <span style="color: #008000;">=</span> xMiddleRenderPos <span style="color: #008000;">-</span> halfWidth<span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">double</span> xLeftRenderPos <span style="color: #008000;">=</span> xMiddleRenderPos <span style="color: #008000;">+</span> halfWidth<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// build a suitable gemoetry</span>
  PathGeometry lineGeometry <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PathGeometry<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  PathFigure upperVerticalLine <span style="color: #008000;">=</span> CreateLineFigure<span style="color: #008000;">&#40;</span>
    <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>xMiddleRenderPos, middleRenderPos <span style="color: #008000;">-</span> halfWidth<span style="color: #008000;">&#41;</span>,
    <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>xMiddleRenderPos, topRenderPos<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  lineGeometry<span style="color: #008000;">.</span><span style="color: #0000FF;">Figures</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>upperVerticalLine<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  PathFigure lowerVerticalLine <span style="color: #008000;">=</span> CreateLineFigure<span style="color: #008000;">&#40;</span>
    <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>xMiddleRenderPos, bottomRenderPos<span style="color: #008000;">&#41;</span>,
    <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>xMiddleRenderPos, middleRenderPos <span style="color: #008000;">+</span> halfWidth<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  lineGeometry<span style="color: #008000;">.</span><span style="color: #0000FF;">Figures</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>lowerVerticalLine<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  PathFigure upperBar <span style="color: #008000;">=</span> CreateLineFigure<span style="color: #008000;">&#40;</span>
    <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>xLeftRenderPos, topRenderPos<span style="color: #008000;">&#41;</span>,
    <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>xRightRenderPos, topRenderPos<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  lineGeometry<span style="color: #008000;">.</span><span style="color: #0000FF;">Figures</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>upperBar<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  PathFigure lowerBar <span style="color: #008000;">=</span> CreateLineFigure<span style="color: #008000;">&#40;</span>
    <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>xLeftRenderPos, bottomRenderPos<span style="color: #008000;">&#41;</span>,
    <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>xRightRenderPos, bottomRenderPos<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  lineGeometry<span style="color: #008000;">.</span><span style="color: #0000FF;">Figures</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>lowerBar<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  PathFigure center <span style="color: #008000;">=</span> CreateLineFigure<span style="color: #008000;">&#40;</span>
    <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>xMiddleRenderPos <span style="color: #008000;">-</span> halfWidth, middleRenderPos<span style="color: #008000;">&#41;</span>,
    <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>xMiddleRenderPos, middleRenderPos <span style="color: #008000;">+</span> halfWidth<span style="color: #008000;">&#41;</span>,
    <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>xMiddleRenderPos <span style="color: #008000;">+</span> halfWidth, middleRenderPos<span style="color: #008000;">&#41;</span>,
    <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>xMiddleRenderPos, middleRenderPos <span style="color: #008000;">-</span> halfWidth<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  lineGeometry<span style="color: #008000;">.</span><span style="color: #0000FF;">Figures</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>center<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">return</span> lineGeometry<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Create a line figure that connects the given points</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> PathFigure CreateLineFigure<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">params</span> Point<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> points<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// add all the points (except the first)</span>
  var pointCollection <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PointCollection<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var point <span style="color: #0600FF; font-weight: bold;">in</span> points<span style="color: #008000;">.</span><span style="color: #0000FF;">Skip</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    pointCollection<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>point<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;">// create a figure, using the first point as the StartPoint.</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> PathFigure<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    IsClosed <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>,
    StartPoint <span style="color: #008000;">=</span> points<span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
    Segments <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PathSegmentCollection<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #008000;">new</span> PolyLineSegment
      <span style="color: #008000;">&#123;</span>
        Points <span style="color: #008000;">=</span> pointCollection
      <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>We can now create an instance of this series in XAML:</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;vis:Chart</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;chart&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;vis:Chart.Series<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:ErrorBarSeries</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/vis:Chart.Series<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/vis:Chart<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Supplying data to the chart via <code>MultiValuedDataPoint</code> as follows:</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 data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataSeries<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">double</span>, <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  data<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>CreatePoint<span style="color: #008000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">195</span>, <span style="color: #FF0000;">1.4</span>, <span style="color: #FF0000;">0.2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  data<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>CreatePoint<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">62.2</span>, <span style="color: #FF0000;">9.3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  data<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>CreatePoint<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">20</span>, <span style="color: #FF0000;">70.4</span>, <span style="color: #FF0000;">6.5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  data<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>CreatePoint<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">100</span>, <span style="color: #FF0000;">77.4</span>, <span style="color: #FF0000;">1.9</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  chart<span style="color: #008000;">.</span><span style="color: #0000FF;">Series</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DataSeries</span> <span style="color: #008000;">=</span> data<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> MultiValuedDataPoint<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">double</span>, <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&gt;</span> CreatePoint<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span> x, <span style="color: #6666cc; font-weight: bold;">double</span> y, <span style="color: #6666cc; font-weight: bold;">double</span> error<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  var point <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MultiValuedDataPoint<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">double</span>, <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>x,
    <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">object</span>, <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #008000;">&#123;</span> ErrorBarSeries<span style="color: #008000;">.</span><span style="color: #0000FF;">ErrorUp</span>, y <span style="color: #008000;">+</span> error <span style="color: #008000;">&#125;</span>,
      <span style="color: #008000;">&#123;</span> ErrorBarSeries<span style="color: #008000;">.</span><span style="color: #0000FF;">ErrorDown</span>, y <span style="color: #008000;">-</span> error <span style="color: #008000;">&#125;</span>,
      <span style="color: #008000;">&#123;</span> ErrorBarSeries<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span>, y <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> point<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This results in the following chart:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/VisibloxErrorBars2.png" alt="" title="VisibloxErrorBars2" width="476" height="293" class="aligncenter size-full wp-image-1772" /></p>
<h2>Binding to a Multi-valued Series</h2>
<p>In the previous example we created instances of <code>MultiValuedDataPoint</code>, a Visiblox type for representing multi-valued points. As an alternative, we can create model objects to represent each point, rendering them in the chart via databinding.</p>
<p>We first modify the code to create a collection of <code>Measurement</code> instances (a simple model object that implements <code>INotifyPropertyChanged</code>):</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 data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ObservableCollection<span style="color: #008000;">&lt;</span>Measurement<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  data<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>CreateMeasurement<span style="color: #008000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">195</span>, <span style="color: #FF0000;">1.4</span>, <span style="color: #FF0000;">0.2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  data<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>CreateMeasurement<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">62.2</span>, <span style="color: #FF0000;">9.3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  data<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>CreateMeasurement<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">20</span>, <span style="color: #FF0000;">70.4</span>, <span style="color: #FF0000;">6.5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  data<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>CreateMeasurement<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">100</span>, <span style="color: #FF0000;">77.4</span>, <span style="color: #FF0000;">1.9</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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> data<span style="color: #008000;">;</span>    
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> Measurement CreateMeasurement<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span> x, <span style="color: #6666cc; font-weight: bold;">double</span> y, <span style="color: #6666cc; font-weight: bold;">double</span> error<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> Measurement<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      XValue <span style="color: #008000;">=</span> x,
      YValue <span style="color: #008000;">=</span> y,
      YValueErrorUp <span style="color: #008000;">=</span> y <span style="color: #008000;">+</span> error,
      YValueErrorDown <span style="color: #008000;">=</span> y <span style="color: #008000;">-</span> error
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The markup for the chart is modified to use a <code>BindableDataSeries</code>, with bindings specified for the various component of the error bar series. Also, the <code>ItemsSource </code>of the <code>BindableDataSeries</code> is bound to the inherited <code>DataContext</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>local<span style="color: #008000;">:</span>ErrorBarSeries<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>local<span style="color: #008000;">:</span>ErrorBarSeries<span style="color: #008000;">.</span><span style="color: #0000FF;">DataSeries</span><span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>vis<span style="color: #008000;">:</span>BindableDataSeries ItemsSource<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{Binding}&quot;</span>
                            XValueBinding<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{Binding XValue}&quot;</span><span style="color: #008000;">&gt;</span>
      <span style="color: #008000;">&lt;</span>vis<span style="color: #008000;">:</span>BindableDataSeries<span style="color: #008000;">.</span><span style="color: #0000FF;">YValueBindings</span><span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>vis<span style="color: #008000;">:</span>YValueBinding YValueKey<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Value&quot;</span> Binding<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{Binding YValue}&quot;</span><span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;</span>vis<span style="color: #008000;">:</span>YValueBinding YValueKey<span style="color: #008000;">=</span><span style="color: #666666;">&quot;ErrorUp&quot;</span> Binding<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{Binding YValueErrorUp}&quot;</span><span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;</span>vis<span style="color: #008000;">:</span>YValueBinding YValueKey<span style="color: #008000;">=</span><span style="color: #666666;">&quot;ErrorDown&quot;</span>  Binding<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{Binding YValueErrorDown}&quot;</span><span style="color: #008000;">/&gt;</span>
      <span style="color: #008000;">&lt;/</span>vis<span style="color: #008000;">:</span>BindableDataSeries<span style="color: #008000;">.</span><span style="color: #0000FF;">YValueBindings</span><span style="color: #008000;">&gt;</span>              
    <span style="color: #008000;">&lt;/</span>vis<span style="color: #008000;">:</span>BindableDataSeries<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;/</span>local<span style="color: #008000;">:</span>ErrorBarSeries<span style="color: #008000;">.</span><span style="color: #0000FF;">DataSeries</span><span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>local<span style="color: #008000;">:</span>ErrorBarSeries<span style="color: #008000;">&gt;</span></pre></div></div>

<p>We can also display our data in a DataGrid, allowing us to manipulate the values (not that I condone manipulation of scientific data!), with the changes being reflected in the chart:</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;sdk:DataGrid</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;grid&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>This gives us the following application:</p>
<div style="text-align: center;"><object width="500" height="400" data="data:application/x-silverlight," type="application/x-silverlight-2" ><param name="source" value="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/VisibloxErrorBarSeries.xap"/><a href="http://go.microsoft.com/fwlink/?LinkID=124807"  style="text-decoration: none;"> <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/></a></object></div>
<p>You can edit the values, with the changes reflected immediately in the chart above.</p>
<p>You can download the sourcecode for the above example here:  <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/VisibloxErrorBarSeries.zip'>VisibloxErrorBarSeries.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/10/adding-error-bars-to-visiblox-silverlight-charts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paging Data from the Server with Silverlight</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/10/paging-data-from-the-server-with-silverlight/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/10/paging-data-from-the-server-with-silverlight/#comments</comments>
		<pubDate>Wed, 19 Oct 2011 12:41:20 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[IPagedCollectionView]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1757</guid>
		<description><![CDATA[This blog post provides an implementation of IPagedCollectionView which allows paging of data from the server. An IPagedDataSource is introduced that allows any paged data source to be plugged in, with the standard controls such as DataPager making it easy to create paging applications. With web-based applications, bandwidth constraints often mean that when querying large [...]]]></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%252Fpaging-data-from-the-server-with-silverlight%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Paging%20Data%20from%20the%20Server%20with%20Silverlight%20%23%22%20%7D);"></div>
<p><em>This blog post provides an implementation of IPagedCollectionView which allows paging of data from the server. An IPagedDataSource is introduced that allows any paged data source to be plugged in, with the standard controls such as DataPager making it easy to create paging applications.</em></p>
<p>With web-based applications, bandwidth constraints often mean that when querying large datasets, the results must be paged, i.e. split into discrete pages each containing a small number of results, with an API available for moving forwards / backwards or to a specific page. There are numerous APIs available on the web that expose this type of interface, for example:</p>
<ul>
<li><a href="https://dev.twitter.com/docs/api/1/get/search">Twitter Search API</a></li>
<li><a href="http://www.nestoria.co.uk/help/api">Nestoria API</a> for searching houses for sale</li>
<li>Any of the <a href="http://www.odata.org/producers">OData producers</a></li>
</ul>
<p>With paging being a common application requirement, Silverlight has the functionality to deal with this scenario built-in. The System.Windows.Data assembly contains the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.ipagedcollectionview%28VS.95%29.aspx">IPagedCollectionView</a> interface, which defines the following methods, properties and events, allowing navigation of paged data:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/PagedCollectionView.png" alt="" title="PagedCollectionView" width="154" height="211" class="aligncenter size-full wp-image-1756" /></p>
<p>The <code>System.Windows.Controls</code> assembly contains a <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datapager.aspx">DataPager</a> control, that collaborates with this interface to provide a UI for paging the dataset. Simply set the Source property of this control to an instance of <code>IPagedCollectionView</code> and your user can page through the data:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/DataPager.png" alt="" title="DataPager" width="312" height="33" class="aligncenter size-full wp-image-1755" /></p>
<p>NOTE: The <code>Source</code> property of <code>DataPager</code> is of type <code>IEnumerable</code>, which is a little odd, because it doesn’t really need to enumerate the collection of items being displayed. Internally it probes the object you supply as the Source to see if it implements <code>IPagedCollectionView</code>, and if it does, the control becomes ‘active’.</p>
<p>This control gives you quite a bit of functionality for free, the buttons become enabled / disabled based on whether navigation forwards / backwards is possible. It respects the CanChangePage property, computes the total number of pages etc &#8230;</p>
<p>So, how do you make use of this interface and control?</p>
<p>The <code>System.Windows.Data</code> assembly also contains a concrete implementation of the paging interface, <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.pagedcollectionview%28VS.95%29.aspx">PagedCollectionView</a>. This class gives much more than just paging, it also implements grouping, sorting and filtering of a source collection (<code>IEnumerable</code>). To make use of this class simply construct an instance based on your collection of data and use that as your <code>DataContext</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span>MyDataObject<span style="color: #008000;">&gt;</span> sourceCollection <span style="color: #008000;">=</span> ParseData<span style="color: #008000;">&#40;</span>serverResponse<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var collectionView <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PagedCollectionView<span style="color: #008000;">&#40;</span>sourceCollection<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<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> collectionView<span style="color: #008000;">;</span></pre></div></div>

<p>The problem is <code>PagedCollectionView</code> allows you to sort, group and page a collection of data that is already held in memory. What I want to do, and what I think is a more common use-case, is page data that is supplied by the server. I don’t want to have to fetch all the data up-front then page it on the client, this would defeat the object of paging in the first place &#8230; to improve performance!</p>
<p>I discovered that there is an <code>IPagedCollectionView</code> implementation as part of RIA Services that provides this functionality, the <code>DomainCollectionView</code>. However, if I want to page data form a simple JSON formatted web service, adding a dependency to RIA Services seems like overkill. So I decided to create my own simple implementation of this interface.</p>
<p>In order to make a generic/ re-useable implementation of this paging interface, I first created an interface that would act as the source of the data:</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;">/// Defines a source of data that can be paged.</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;">interface</span> IPagedDataSource<span style="color: #008000;">&lt;</span>TDataType<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Asynchronously returns the data for the given page</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #6666cc; font-weight: bold;">void</span> FetchData<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> pageNumber, Action<span style="color: #008000;">&lt;</span>PagedDataResponse<span style="color: #008000;">&lt;</span>TDataType<span style="color: #008000;">&gt;&gt;</span> responseCallback<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// The items returned as a result of a paged data request.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> PagedDataResponse<span style="color: #008000;">&lt;</span>TDataType<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// The items contained within the requested page</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> List<span style="color: #008000;">&lt;</span>TDataType<span style="color: #008000;">&gt;</span> Items <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// The total count of all available items</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;">int</span> TotalItemCount <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This interface is very simple, having a single method that fetches data for the given page. The result is returned asynchronously, providing both the items within the given page and the total item count.</p>
<p>My paging collection view extends <code>ObservableCollection</code> and takes an instance of <code>IPagedDataSource</code> in its constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ServerSidePagedCollectionView<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> ObservableCollection<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>,
  IPagedCollectionView
<span style="color: #008000;">&#123;</span>
  IPagedDataSource<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> _pagedDataSource<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> ServerSidePagedCollectionView<span style="color: #008000;">&#40;</span>IPagedDataSource<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> pagedDataSource<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _pagedDataSource <span style="color: #008000;">=</span> pagedDataSource<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As an aside, aren’t generics great?</p>
<p>The various properties defined in <code>IPagedCollectionView</code> (<code>CanChangePage</code>, <code>TotalPages</code>, etc &#8230;) are implemented as standard field-backed properties that notify changes via <code>INotifyPropertyChanged</code> which is inherited via <code>ObservableCollection</code>, I am not going to show them here.</p>
<p>The various methods that permit navigation are all implemented using the same pattern:</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;">bool</span> MoveToFirstPage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  RefreshData<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">return</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;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> MoveToLastPage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  RefreshData<span style="color: #008000;">&#40;</span>TotalPages <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">return</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;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> MoveToNextPage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  RefreshData<span style="color: #008000;">&#40;</span>PageIndex <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">return</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;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> MoveToPage<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> pageIndex<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  RefreshData<span style="color: #008000;">&#40;</span>pageIndex<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">return</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;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> MoveToPreviousPage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  RefreshData<span style="color: #008000;">&#40;</span>PageIndex <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>With the <code>RefreshData</code> method doing all the work, it is the only method that uses the supplied <code>IPagedDataSource</code> interface and is shown below:</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;">/// Fetches the data for the given page</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> RefreshData<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> newPageIndex<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// set the pre-fetch state</span>
  CanChangePage <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
  OnPageChanging<span style="color: #008000;">&#40;</span>newPageIndex<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  _pagedDataSource<span style="color: #008000;">.</span><span style="color: #0000FF;">FetchData</span><span style="color: #008000;">&#40;</span>newPageIndex, response <span style="color: #008000;">=&gt;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">// process the received data</span>
      DataReceived<span style="color: #008000;">&#40;</span>response<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// set the post-fetch state</span>
      PageIndex <span style="color: #008000;">=</span> newPageIndex<span style="color: #008000;">;</span>
      OnPageChanged<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      CanChangePage <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>With the simple implementation of <code>IPagedCollectionView</code> given above I can now page data from a range of web services by simply implementing <code>IPagedDataSource</code>. For example, to page data from NetFlix you can use the following implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// A paged NetFlix movies search datasource.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NetFlixDataSource <span style="color: #008000;">:</span> IPagedDataSource<span style="color: #008000;">&lt;</span>SyndicationItem<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _searchString<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> _pageSize <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> NetFlixDataSource<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> searchString<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _searchString <span style="color: #008000;">=</span> searchString<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;">void</span> FetchData<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> pageNumber, Action<span style="color: #008000;">&lt;</span>PagedDataResponse<span style="color: #008000;">&lt;</span>SyndicationItem<span style="color: #008000;">&gt;&gt;</span> responseCallback<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    WebClient client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebClient<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 url for fetching movies with a name that contains the search string</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> url <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;http://odata.netflix.com/Catalog/Titles?&quot;</span>
      <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;$top={0}&amp;$orderby=Name&amp;$select=Name&amp;$inlinecount=allpages&amp;$skip={1}&amp;$filter=indexof(Name,'{2}') ne -1&quot;</span>,
      _pageSize, pageNumber <span style="color: #008000;">*</span> _pageSize, _searchString<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    client<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadStringCompleted</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: #008080; font-style: italic;">// parse the response using SyndaicationFeed</span>
      XmlReader reader <span style="color: #008000;">=</span> XmlReader<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> StringReader<span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      SyndicationFeed feed <span style="color: #008000;">=</span> SyndicationFeed<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span>reader<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// locate the item count </span>
      var itemCount <span style="color: #008000;">=</span> feed<span style="color: #008000;">.</span><span style="color: #0000FF;">ElementExtensions</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetObject</span><span style="color: #008000;">&lt;</span>XElement<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;">Value</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// invoke the response callback</span>
      responseCallback<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> PagedDataResponse<span style="color: #008000;">&lt;</span>SyndicationItem<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        Items <span style="color: #008000;">=</span> feed<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
        TotalItemCount <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>itemCount<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><span style="color: #008000;">;</span>
    client<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadStringAsync</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Uri<span style="color: #008000;">&#40;</span>url<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As you can see, this simple implementation of our paging data source constructs the required URL based on the search string and request page number. It also uses the ‘syndication’ APIs which are able to parse the response from the NetFlix OData APIs.</p>
<p>To test out this implementation of the paged search interface I have created a very simple movie search application. The view model for this application has a <code>SearchString</code> property, exposes the results as <code>SearchResults</code> property and has a single command which initiates a new search. The important parts of this class are shown below:</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 paged search results</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> ServerSidePagedCollectionView<span style="color: #008000;">&lt;</span>SyndicationItem<span style="color: #008000;">&gt;</span> SearchResults
<span style="color: #008000;">&#123;</span>
  get
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> _searchResults<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> set
  <span style="color: #008000;">&#123;</span>
    SetField<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> _searchResults, value, <span style="color: #666666;">&quot;SearchResults&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Gets a command which executes the search</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> ICommand ExecuteSearchCommand
<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;">new</span> DelegateCommand<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
    <span style="color: #008000;">&#123;</span>
      SearchResults <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ServerSidePagedCollectionView<span style="color: #008000;">&lt;</span>SyndicationItem<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>
        <span style="color: #008000;">new</span> NetFlixDataSource<span style="color: #008000;">&#40;</span>SearchText<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>When <code>ExecuteSearchCommand</code> is invoked, we create a new <code>ServerSidePagedCollectionView</code>, providing it with the NetFlix data source.  From here on, the <code>ServerSidePagedCollectionView</code> is responsible for the paging of data.</p>
<p>The view is 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;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;LightGray&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;400&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;360&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;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;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;/Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></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;">Margin</span>=<span style="color: #ff0000;">&quot;4&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 SearchText, Mode=TwoWay}&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;150&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;">Command</span>=<span style="color: #ff0000;">&quot;{Binding ExecuteSearchCommand}&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot; Search Movies ...&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;5,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;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding SearchResults}&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">ItemTemplate</span>=<span style="color: #ff0000;">&quot;{StaticResource MovieTemplate}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl.ItemsPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsPanelTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel</span> <span style="color: #000066;">Orientation</span>=<span style="color: #ff0000;">&quot;Vertical&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsPanelTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl.ItemsPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl<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> <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;White&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;">Opacity</span>=<span style="color: #ff0000;">&quot;0.5&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">Visibility</span>=<span style="color: #ff0000;">&quot;{Binding Path=SearchResults.CanChangePage, Converter={StaticResource BoolToVisibilityConverter}}&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;sdk:DataPager</span>  <span style="color: #000066;">PageSize</span>=<span style="color: #ff0000;">&quot;10&quot;</span> </span>
<span style="color: #009900;">                  <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;{Binding SearchResults}&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>
&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></pre></div></div>

<p>The DataPager simply binds to the <code>SearchResults</code> as does the ItemsControl. I have also added a <code>Rectangle</code> element which grays-out the current search results when a new page is being fetched.</p>
<p>You can see the application in action below:</p>
<div style="text-align: center;"><object width="500" height="400" data="data:application/x-silverlight," type="application/x-silverlight-2" ><param name="source" value="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/PagedCollectionViewExample.xap"/><a href="http://go.microsoft.com/fwlink/?LinkID=124807"  style="text-decoration: none;"> <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/></a></object></div>
<p>So in summary, by implementing <code>IPagedCollectionView</code> with a pluggable datasource, <code>IPagedDataSource</code>, it is possible to very quickly and easily create an application that pages data from the server.</p>
<p>You can download the full sourcecode here: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/PagedCollectionViewExample.zip'>PagedCollectionViewExample.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/10/paging-data-from-the-server-with-silverlight/feed/</wfw:commentRss>
		<slash:comments>9</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>A Windows Phone 7.1 (Mango) MVVM Tombstoning Example</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/10/a-windows-phone-7-1-mango-mvvm-tombstoning-example/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/10/a-windows-phone-7-1-mango-mvvm-tombstoning-example/#comments</comments>
		<pubDate>Mon, 03 Oct 2011 13:25:22 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[Mango]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[tombstoning]]></category>
		<category><![CDATA[Windows Phone 7]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1710</guid>
		<description><![CDATA[In this blog post I look at the new application lifecycle model that Windows Phone 7.1 (Mango) introduces, and show how to handle the various lifecycle events in a simple MVVM application. In a previous blog post I described the development of a simple Windows Phone 7 application using the Model-View-ViewModel (MVVM) pattern and how [...]]]></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%252Fa-windows-phone-7-1-mango-mvvm-tombstoning-example%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22A%20Windows%20Phone%207.1%20%28Mango%29%20MVVM%20Tombstoning%20Example%20%23%22%20%7D);"></div>
<p><i>In this blog post I look at the new application lifecycle model that Windows Phone 7.1 (Mango) introduces, and show how to handle the various lifecycle events in a simple MVVM application.</i></p>
<p>In a <a href="http://www.scottlogic.co.uk/blog/colin/2011/05/a-simple-windows-phone-7-mvvm-tombstoning-example/" title="A Simple Windows Phone 7 MVVM Tombstoning Example">previous blog post</a> I described the development of a simple Windows Phone 7 application using the Model-View-ViewModel (MVVM) pattern and how to handle application lifecycle events, specifically tombstoning. In this blog post I will look at updating this sample for Windows Phone 7.1 (Mango) to handle fast-app switching and the new Dormant state.</p>
<p>As an aside, I have bumped into a number of developers who were under the impression that <strong>Mango removes the need to tombstone</strong>. I&#8217;m afraid that <strong>this is not the case</strong>!</p>
<h2>Introduction and a Recap</h2>
<p>The WP7 application lifecycle model is designed to allow the user to move fluidly between applications whilst minimising the memory that these applications consume. Neither WP7 or WP7.1 (Mango) allow true multi-tasking, where more than one application is running at the same time. Instead, they employ a number of techniques to give the illusion of multi-tasking. As a developer you are exposed to this via the application lifecycle.</p>
<p>The WP7 (pre-Mango) application lifecycle, with the events that are fired for each transition, is illustrated below:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/WP7Lifecycle.png" alt="" title="WP7Lifecycle" width="600" height="476" class="aligncenter size-full wp-image-1711" /></p>
<p>You can see the tasks that you must perform under each state transition.</p>
<p>When the user pressed the Start button, your application stops running and becomes tombstoned. It is your responsibility to save your application state such that when the user pressed the back-button (navigating the back-stack) to return to your application, it is in the same state as when they left it. This involves saving both the state of your application logic, within the application <code>PhoneApplicationService.State</code> dictionary and the UI state (e.g list scroll position) within the <code>PhoneApplicationPage.State</code> dictionary for each page. I described how you can achieve this in my <a href="http://www.scottlogic.co.uk/blog/colin/2011/05/a-simple-windows-phone-7-mvvm-tombstoning-example/" title="A Simple Windows Phone 7 MVVM Tombstoning Example">previous blog post</a>. I also want to point out that the rather tricky task of saving UI state can be made much simpler by using things like Matt Lacey&#8217;s popular <a href="http://tombstonehelper.codeplex.com/">Tombstoning Helper</a> utilities.</p>
<p>One other thing that the <a href="http://msdn.microsoft.com/en-us/library/ff817008(VS.92).aspx">Execution Model Overview on MSDN</a> does not make terribly clear is that the back-stack has a limited size. What this means is that your tombstoned application state will not necessarily be re-activated. You can test this by running your application with the debugger attached, if you create a long enough back-stack (by repeatedly hitting the Start button and starting a new application), your debugging session will eventually terminate. This occurs when your application state has been pushed off the back-stack.</p>
<p>The upshot of this is that when your application is tombstoned you must save your state to isolated storage as well as the <code>State</code> dictionary (because your application might never get closed). Although, as it is recommended that your application tombstones swiftly, you might want to store application state to isolated storage at more regular intervals.</p>
<h2>Windows Phone 7.1 Lifecycle</h2>
<p>Windows Phone 7.1 (Mango) introduces a much more convincing illusion of multi-tasking, where the user can press and hold the back-button to rapidly switch between a small number of apparently concurrently running applications. However, again this is not true multitasking.</p>
<p>To achieve this, WP7.1 introduces a new state in the application lifecycle, the Dormant state:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/WP7MangoLifecycle.png" alt="" title="WP7MangoLifecycle" width="600" height="461" class="aligncenter size-full wp-image-1713" /></p>
<p>When you application is in a Dormant state it is still in memory, however it is not executing. This allows the phone to re-start your application much more rapidly than when it is in a tombstoned state.</p>
<p>The phone can only store a limited number of dormant applications, as a result it might choose to move your application from a dormant to a tombstoned state. You do not receive any notification that this has occurred. However, the tombstoning and re-activiation process is the same as that for WP7.0, where you re-activate your application from the data saved in the <code>State</code> dictionary.</p>
<p>Again, there is always the possibility that your tombstoned data will be pushed out of the back-stack and lost.</p>
<p>Notice that there have not been any new events added to indicate that your application has returned from a Dormant state, your application will always receive <code>Activated </code>event when it is restarted regardless of whether it was dormant or tombstoned. This is good news, because it is backwards compatible with WP7.0.</p>
<h2>Handling the Dormant State</h2>
<p>So how do you handle this new state in your application? In order to maintain backwards compatibility you don&#8217;t strictly have to, however it is actually very simple to do.</p>
<p>The event arguments passed to the <code>Activated</code> event now have a new <code>IsApplicationInstancePreserved</code> argument which is true if your application has been re-started from a dormant state and false if it has been restarted from a tombstoned state. If this argument returns true, you simply do nothing! This ensures that you do not perform any redundant actions and helps your application restart faster.</p>
<p>The code below is all of the lifecycle code (apart from per-page UI state) that my MVVM example application contains. You can see that the code for a re-activated dormant state does nothing.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Code to execute when the application is launching (eg, from Start)</span>
<span style="color: #008080; font-style: italic;">// This code will not execute when the application is reactivated</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Application_Launching<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, LaunchingEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  Debug<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Launching&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  LoadViewModelFromIsolatedStorage<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;">// if the view model is not loaded, create a new one</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>ViewModel <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>
    ViewModel <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FeedViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    ViewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">Update</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// set the frame DataContext</span>
  RootFrame<span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> ViewModel<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Code to execute when the application is activated (brought to foreground)</span>
<span style="color: #008080; font-style: italic;">// This code will not execute when the application is first launched</span>
<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>e<span style="color: #008000;">.</span><span style="color: #0000FF;">IsApplicationInstancePreserved</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Debug<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Activated From Dormant State&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">else</span>
  <span style="color: #008000;">&#123;</span>
    Debug<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Activated From Tombstoned State&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    LoadViewModelFromAppState<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    RootFrame<span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> ViewModel<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;">// Code to execute when the application is deactivated (sent to background)</span>
<span style="color: #008080; font-style: italic;">// This code will not execute when the application is closing</span>
<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>
  Debug<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Deactivated&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  SaveViewModelToAppState<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  SaveViewModelToIsolatedStorage<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: #008080; font-style: italic;">// Code to execute when the application is closing (eg, user hit Back)</span>
<span style="color: #008080; font-style: italic;">// This code will not execute when the application is deactivated</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Application_Closing<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, ClosingEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  Debug<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Closing&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  SaveViewModelToIsolatedStorage<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;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> SaveViewModelToAppState<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</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> ViewModel<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> LoadViewModelFromAppState<span style="color: #008000;">&#40;</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>
    ViewModel <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> FeedViewModel<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> LoadViewModelFromIsolatedStorage<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;">// load the view model from isolated storage</span>
  <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var store <span style="color: #008000;">=</span> IsolatedStorageFile<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserStoreForApplication</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var stream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IsolatedStorageFileStream<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;data.txt&quot;</span>, FileMode<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenOrCreate</span>, FileAccess<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span>, store<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var reader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</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><span style="color: #008000;">!</span>reader<span style="color: #008000;">.</span><span style="color: #0000FF;">EndOfStream</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      var serializer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlSerializer<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>FeedViewModel<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      ViewModel <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>FeedViewModel<span style="color: #008000;">&#41;</span>serializer<span style="color: #008000;">.</span><span style="color: #0000FF;">Deserialize</span><span style="color: #008000;">&#40;</span>reader<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> SaveViewModelToIsolatedStorage<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;">// persist the data using isolated storage</span>
  <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var store <span style="color: #008000;">=</span> IsolatedStorageFile<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserStoreForApplication</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var stream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IsolatedStorageFileStream<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;data.txt&quot;</span>,
                                                    FileMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span>,
                                                    FileAccess<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span>,
                                                    store<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    var serializer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlSerializer<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>FeedViewModel<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    serializer<span style="color: #008000;">.</span><span style="color: #0000FF;">Serialize</span><span style="color: #008000;">&#40;</span>stream, ViewModel<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>

<h2>Debugging Dormant and Tombstoned state</h2>
<p>Because the application now has a couple of states that it can be in when it is not running, you need to employ a few tricks to be able to force the application into these states while developing. Typically the emulator will always place your application into a dormant state when you hit the Start button.<br />
The fast-app switch UI is not enabled on the emulator by default, however you can enable it with a bit of hackery as described on <a href="http://forum.xda-developers.com/showthread.php?t=1121188">this forum thread</a>.</p>
<p>So, if your application always moves into a dormant state, how do you test your tombstoning code? Fortunately there is a build setting which you can enable which will ensure that your application is always tombstoned rather than placed in a dormant state:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/ForceTombstone.png" alt="" title="ForceTombstone" width="444" height="241" class="aligncenter size-full wp-image-1716" /></p>
<p>You should test your application with this setting enabled and disabled to ensure that it accommodates tombstoning and dormant state.</p>
<p>I hope this blog post has helps understand the slightly complex application lifecycle that Mango introduces. It is clear that this delivers the best performance to the user, however it <i>is</i> a little complex for the developer!</p>
<p>You can download my updated MVVM example here: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/10/WindowsPhoneMVVMExample.zip'>WindowsPhoneMVVMExample.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/10/a-windows-phone-7-1-mango-mvvm-tombstoning-example/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

