<?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; mvvm</title>
	<atom:link href="http://www.scottlogic.co.uk/blog/colin/tag/mvvm/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>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>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>
		<item>
		<title>A Simple Windows Phone 7 MVVM Tombstoning Example</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/05/a-simple-windows-phone-7-mvvm-tombstoning-example/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/05/a-simple-windows-phone-7-mvvm-tombstoning-example/#comments</comments>
		<pubDate>Mon, 23 May 2011 12:12:23 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[tombstoning]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1449</guid>
		<description><![CDATA[This blog post shows how to implement tombstoning within a Windows Phone 7 application that following the Model-View-ViewModel pattern. UPDATE: I have published a update to this blog post to handle the new dormant state in Windows Phone 7.1 (Mango). I have to admit Windows Phone 7 tombstoning had me in a bit of a [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F05%252Fa-simple-windows-phone-7-mvvm-tombstoning-example%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22A%20Simple%20Windows%20Phone%207%20MVVM%20Tombstoning%20Example%20%23%22%20%7D);"></div>
<p><em>This blog post shows how to implement tombstoning within a Windows Phone 7 application that following the Model-View-ViewModel pattern.</em></p>
<p><b>UPDATE:</b> I have published a <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">update to this blog post to handle the new dormant state in Windows Phone 7.1 (Mango)</a>.</p>
<p>I have to admit Windows Phone 7 tombstoning had me in a bit of a muddle for a while, so many places to store state, a confusing lifecycle and navigation model. Most of the blog posts I read either detailed tombstoning for non-MVVM applications, or described how to use or adapt an existing MVVM framework for the purposes of tombstoning. I only really understood the ins-and-outs of tombstoning after writing my own simple MVVM application. I thought I would share this application here in this blog in the hope that it might help other similarly confused developers!</p>
<h2>What is tombstoning?</h2>
<p>Mobile phones have limited resources compared to desktop computers, for that reason most smartphones OSs limit the number of applications that are currently loaded into memory and executing. For Windows Phone 7 this limit is one!</p>
<p>If the user hits the phone start button while you application is running, the screen lock engages, or you invoke a chooser / launcher from your application, then your application is terminated. However, when the user navigates back to your app, the screen unlocks or the chooser / launcher closed, the user expects to see your application again in its original state. </p>
<p>To support this, the WP7 OS maintains state information which allows you to &#8216;restore&#8217; your application by starting a new application instance and using this state information to start the application in the same state as the one which was terminated. For a full overview of this process I would recommend reading the <a href="http://msdn.microsoft.com/en-us/library/ff817008(v=vs.92).aspx">Execution Model Overview for Windows Phone</a> on MSDN, or the three part series on the Windows Phone Developer Blog <a href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2010/07/15/understanding-the-windows-phone-application-execution-model-tombstoning-launcher-and-choosers-and-few-more-things-that-are-on-the-way-part-1.aspx">(1)</a>, <a href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2010/07/16/understanding-the-windows-phone-application-execution-model-tombstoning-launcher-and-choosers-and-few-more-things-that-are-on-the-way-part-2.aspx">(2)</a>, <a href="http://windowsteamblog.com/windows_phone/b/wpdev/archive/2010/07/20/understanding-the-windows-phone-application-execution-model-tombstoning-launcher-and-more-part-3.aspx">(3)</a>.</p>
<p>This probably sounds like a lot of work, and to be honest, it is. You might be wondering if the new multi-tasking capabilities that the Mango update will bring (demoed at the<a href="http://channel9.msdn.com/Events/MIX/MIX11/KEY02"> MIX11 day 2 keynote</a> and to be released probably late 2012) will mean the that tombstoning will disappear. I have not seen any official confirmation one way or the other, however, personally I think you will still need to tombstone in Mango. It is most likely that the number of concurrent applications will be limited and applications will still need to tombstone as a result.</p>
<h2>The Example Application</h2>
<p>The example application I am using for this blog post is illustrated below. The application displays a list of tweets, clicking on a tweet displays it full screen. Twitter applications are the new Hello World!</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/05/MVVMExampleApplication.png" alt="" title="MVVMExampleApplication" width="600" height="456" class="aligncenter size-full wp-image-1453" /></p>
<h2>The ViewModel</h2>
<p>The view model is pretty trivial, each tweet is represented by a FeedItemViewModel:</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> FeedItemViewModel
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> FeedItemViewModel<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> <span style="color: #6666cc; font-weight: bold;">long</span> Id <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;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Title <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;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Author <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;">public</span> DateTime Timestamp <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>The above view model does not change state, so there is no need to implement <code>INotifyPropertyChanged</code>.</p>
<p>The top-level view model simply exposes a collection of the above feed items. It also has an <code>Update</code> method which populates this list by querying Twitter&#8217;s search API for tweets that contain the #wp7 hashtag:</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> FeedViewModel 
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// Twitter search for #WP7</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #6666cc; font-weight: bold;">string</span> _twitterUrl <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://search.twitter.com/search.atom?rpp=100&amp;&amp;q=%23wp7&quot;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> WebClient _webClient <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: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> ObservableCollection<span style="color: #008000;">&lt;</span>FeedItemViewModel<span style="color: #008000;">&gt;</span> _feedItems <span style="color: #008000;">=</span>
                                             <span style="color: #008000;">new</span> ObservableCollection<span style="color: #008000;">&lt;</span>FeedItemViewModel<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> FeedViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _webClient<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadStringCompleted</span> <span style="color: #008000;">+=</span> WebClient_DownloadStringCompleted<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;">/// Parses the response from our twitter request, creating a list of FeedItemViewModelinstances</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> WebClient_DownloadStringCompleted<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, DownloadStringCompletedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    var doc <span style="color: #008000;">=</span> XDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><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;">;</span>
    var items <span style="color: #008000;">=</span> doc<span style="color: #008000;">.</span><span style="color: #0000FF;">Descendants</span><span style="color: #008000;">&#40;</span>AtomConst<span style="color: #008000;">.</span><span style="color: #0000FF;">Entry</span><span style="color: #008000;">&#41;</span>
             <span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>entryElement <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">new</span> FeedItemViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
             <span style="color: #008000;">&#123;</span>
                Title <span style="color: #008000;">=</span> entryElement<span style="color: #008000;">.</span><span style="color: #0000FF;">Descendants</span><span style="color: #008000;">&#40;</span>AtomConst<span style="color: #008000;">.</span><span style="color: #0000FF;">Title</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Single</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span>,
                Id <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">long</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>entryElement<span style="color: #008000;">.</span><span style="color: #0000FF;">Descendants</span><span style="color: #008000;">&#40;</span>AtomConst<span style="color: #008000;">.</span><span style="color: #0000FF;">ID</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Single</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><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">':'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>,
               Timestamp <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>entryElement<span style="color: #008000;">.</span><span style="color: #0000FF;">Descendants</span><span style="color: #008000;">&#40;</span>AtomConst<span style="color: #008000;">.</span><span style="color: #0000FF;">Published</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Single</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;">&#41;</span>,
               Author <span style="color: #008000;">=</span> entryElement<span style="color: #008000;">.</span><span style="color: #0000FF;">Descendants</span><span style="color: #008000;">&#40;</span>AtomConst<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Single</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;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    _feedItems<span style="color: #008000;">.</span><span style="color: #0000FF;">Clear</span><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 item <span style="color: #0600FF; font-weight: bold;">in</span> items<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      _feedItems<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>item<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 the feed items</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> ObservableCollection<span style="color: #008000;">&lt;</span>FeedItemViewModel<span style="color: #008000;">&gt;</span> FeedItems
  <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _feedItems<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 feed item by its Id</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> FeedItemViewModel GetFeedItem<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">long</span> id<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> _feedItems<span style="color: #008000;">.</span><span style="color: #0000FF;">SingleOrDefault</span><span style="color: #008000;">&#40;</span>item <span style="color: #008000;">=&gt;</span> item<span style="color: #008000;">.</span><span style="color: #0000FF;">Id</span> <span style="color: #008000;">==</span> id<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;">/// Update the feed 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;">void</span> Update<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _webClient<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>_twitterUrl<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>

<h2>The View</h2>
<p>The FeedView page that is used to render the FeedViewModel (i.e the list of tweets) is simply a <a href="http://www.scottlogic.co.uk/blog/colin/2011/04/a-fast-loading-windows-phone-7-navigationlist-control/">NavigationList</a> control (An <code>ItemsControl</code> optimised for WP7 navigation scenarious) that has an <code>ItemTemplate</code> which renders each item:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;ContentPanel&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;12,0,12,0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;l:NavigationList</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;navigationControl&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding FeedItems}&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">Navigation</span>=<span style="color: #ff0000;">&quot;NavigationList_Navigation&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;l:NavigationList.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;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;">Height</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;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Author}&quot;</span></span>
<span style="color: #009900;">                      <span style="color: #000066;">Style</span>=<span style="color: #ff0000;">&quot;{StaticResource PhoneTextNormalStyle}&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 Title}&quot;</span></span>
<span style="color: #009900;">                      <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;20,0,0,0&quot;</span></span>
<span style="color: #009900;">                      <span style="color: #000066;">Style</span>=<span style="color: #ff0000;">&quot;{StaticResource PhoneTextSmallStyle}&quot;</span></span>
<span style="color: #009900;">                      <span style="color: #000066;">TextWrapping</span>=<span style="color: #ff0000;">&quot;Wrap&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;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/l:NavigationList.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/l:NavigationList<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>The FeedItemView that is used to render the FeedItemViewModel is even simpler:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;ContentPanel&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;12,0,12,0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;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;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Author}&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Style</span>=<span style="color: #ff0000;">&quot;{StaticResource PhoneTextLargeStyle}&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Foreground</span>=<span style="color: #ff0000;">&quot;{StaticResource PhoneAccentBrush}&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 Title}&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Style</span>=<span style="color: #ff0000;">&quot;{StaticResource PhoneTextLargeStyle}&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">TextWrapping</span>=<span style="color: #ff0000;">&quot;Wrap&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>Now that we have the ViewModels and their respective Views we need to bring them together by making the ViewModel the DataContext for each View. There are a number of different ways you can do this (Paul Stovell documents 8 different ways in his <a href="http://www.paulstovell.com/mvvm-instantiation-approaches">MVVM Instantiation Approaches</a> blogpost!), however, I find the simplest approach with WP7 applications is to associated the ViewModel with the application. Therefore, we add our view model as a property of the <code>App</code> class and instantiate it when the <code>Application_Launching</code> method (which handles the <code>Launching</code> lifecycle event) is invoked:</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> App <span style="color: #008000;">:</span> Application
<span style="color: #008000;">&#123;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Gets the ViewModel</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> FeedViewModel ViewModel <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: #008000;">...</span>
&nbsp;
  <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>
    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>
&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: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The <code>DataContext</code> of the RootFrame is set to our &#8216;top level&#8217; view model. The RootFrame is an instance of <code>PhoneApplicationFrame</code>, which contains the current <code>PhoneApplicationPage</code> instance, hence when you navigate from one page to the next the content of the <code>PhoneApplicationFrame</code> is replaced within the page that has been navigated to. As a result, each of your pages will inherit the <code>DataContext</code> from the application frame.</p>
<h2>Navigation</h2>
<p>The <code>DataContext</code> of the RootFrame is inherited by the FeedView page, so once the tweets are loaded they will be rendered in our <code>NavigationList</code>. In order to navigate to a tweet when a user clicks on it we need to handle the <code>Navigation</code> event:</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> FeedView <span style="color: #008000;">:</span> PhoneApplicationPage
<span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">...</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> NavigationList_Navigation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, NavigationEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    var selectedItem <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Item</span> <span style="color: #0600FF; font-weight: bold;">as</span> FeedItemViewModel<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// navigate to the feed items page</span>
    NavigationService<span style="color: #008000;">.</span><span style="color: #0000FF;">Navigate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Uri<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;/FeedItemView.xaml?id=&quot;</span> <span style="color: #008000;">+</span> selectedItem<span style="color: #008000;">.</span><span style="color: #0000FF;">Id</span>, UriKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Relative</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: #008000;">&#125;</span></pre></div></div>

<p>The above code uses the <code>NavigationService</code> to navigate to the <code>FeedItemView</code> page. We use the querystring to pass the id of the selected tweet. We could have passed this information from View the ViewModel by adding a <code>SelectedItemId</code> property, however, we will find out later that there are some advantages to using the querystring.</p>
<p>When the <code>FeedItemView</code> page is loaded we need to obtain this id from the querystring and use it to locate the correct <code>FeedItemViewModel</code> instance. This is done 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> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> FeedItemView <span style="color: #008000;">:</span> PhoneApplicationPage
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> FeedItemView<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>
&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> OnNavigatedTo<span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Navigation</span><span style="color: #008000;">.</span><span style="color: #0000FF;">NavigationEventArgs</span> e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnNavigatedTo</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// parse the querystring to extract the id</span>
    <span style="color: #6666cc; font-weight: bold;">long</span> feedItemId <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">long</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>NavigationContext<span style="color: #008000;">.</span><span style="color: #0000FF;">QueryString</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;id&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// obtain this item from the view model.</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> App<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ViewModel</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetFeedItem</span><span style="color: #008000;">&#40;</span>feedItemId<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>Tombstoning</h2>
<p>With the above code our simple application works just fine, you can navigate the list of tweets, click on one to see it in the <code>FeedItemView</code> page, then use the back button to return to the original list of tweets. It would be great if our job was complete at this point, however, the application fails miserably if it is tombstoned. To test this, load the application, then hit the start button, followed by the back button to return to the application. This is what you are met with:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/05/TombstoningFail.png" alt="" title="TombstoningFail" width="700" height="343" class="aligncenter size-full wp-image-1465" /></p>
<p>Tombstoning terminates the application, therefore the ViewModel and all the state it contains is lost. Hence we return to an empty list.</p>
<p>So how do we fix this?</p>
<p>When your application gets tombstoned a <code>Deactivated</code> event is fired before your application terminates, and when the user navigates back to your application, a new instance is created an the <code>Activated</code> event is fired. The Visual Studio WP7 application template helpfully adds event handlers for these events on your <code>App</code> class.</p>
<p>Note that when an application is re-activeated the <code>Launching</code> event is not fired, therefore the code we added to construct a new view model is not executed in this case.</p>
<p>The fix to this problem is rather simple, the framework provides a <code>PhoneApplicationService </code>class which has a <code>State</code> property which allows you to store your application state within a dictionary. The WP7 OS will persist this dictionary of state on your behalf when your application is tombstoned. You can place anything within this dictionary as long as it is serializable. Therefore a simple solution to this problem is to simply place our view model into this dictionary, retrieving it when the application is re-activated:</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;">readonly</span> <span style="color: #6666cc; font-weight: bold;">string</span> ModelKey <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Key&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<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>
  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> 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>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>
    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></pre></div></div>

<p>With this simple change the application now returns to its previous state when the user hits the back button. Note that this also works when the application is tombstoned on the <code>FeedItemView</code> page:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/05/TombstoningWin.png" alt="" title="TombstoningWin" width="700" height="340" class="aligncenter size-full wp-image-1468" /></p>
<p>This is because when the application is re-activated, the <code>NavigationService</code> Journal which records the pages that have been visited is also restored. The &#8216;back stack&#8217; contains the URI of each page which in our case contains the querystring which holds the Id of the tweet which is currently being viewed.</p>
<h2>Persisting State Between Sessions</h2>
<p>In the above example, the application state was saved as tomstoned state. But what if the user simply hits the back button until they navigate back out of our application? In this context your application is closed, rather than tombstoned. The <code>PhoneApplicationService.Current.State</code> dictionary which we used to store our view model is not persisted in this context. For long-term persistence you should save your data to the phones isolated storage. </p>
<p>We can serialize our view model to isolated storage when the application exits by adding code to the <code>Application_Closing</code> methods which handles the <code>Closing</code> event. In the example below I am using the framework <code>XmlSerializer</code>, which is the same serializer which is used to persist data in the application state dictionary. However, as you have full control over serialization, you might choose to use <a href="http://www.identitymine.com/forward/2010/11/loading-quickly-on-windows-phone-7-twitter-example/">a serializer with better performanc</a>e.</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 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>
    <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>

<p>The code for launching the application now needs to be modified to first try and load the data from isolated storage. If no data is found a new view model is created:</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>
  <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>
&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></pre></div></div>

<p>NOTE: This is no a fully functional twitter application, in a real application you would probably want to update the data that is loaded from isolated storage to add the latest tweets, however this blog post is about tomstoning, not twitter application development!</p>
<h2>Tombstoning UI state</h2>
<p>So, now our application saves state during tomstoning and to isolated storage when it exits. Surely we&#8217;re all done now?</p>
<p>Well &#8230; almost.</p>
<p>If you start the application and scroll down the list of tweets, then hit the start button, tomstoning the application, then hit the back button to re-activate it our tweets are all there, but our list is scrolled back to the top again:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/05/ScrollFail.png" alt="" title="ScrollFail" width="600" height="264" class="aligncenter size-full wp-image-1473" /></p>
<p>Why is this? Well, when you simply navigate back to from one page within your application to another, the original page is still present in memory and as a result, any state held by the controls within the application UI is maintained. </p>
<p>However, as we have seen already, when an application is tomstoned it is terminated. The only state that remains is that which you manually place into the <code>State</code> dictionary. Therefore, in order to maintain the scroll location, which we should do, we need to determine its location and store it in the tombstone state.</p>
<p>A while back I blogged about <a href="http://www.scottlogic.co.uk/blog/colin/2010/07/exposing-and-binding-to-a-silverlight-scrollviewer%E2%80%99s-scrollbars/">exposing a <code>ScrollViewers</code> scroll location as an attached property</a> in order to permit databinding. This approach could be used here to bind the scroll location to a property on the view model. However, in this case I think something a bit more lightweight is more appropriate.</p>
<p>In the previous sections we have used the application-level code>PhoneApplicationService.Current.State</code> for storing tombstone state. You can also store state on a page-level via the <code>PhoneApplicationPage.State</code> property. This is a much more appropriate place for storing state relating to UI controls within pages, rather than state which is more closely related with your applications business logic.</p>
<p>It is still a little tricky to extract the required state information from list controls. The code below uses <a href="http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/">Linq-to-VisualTree</a> to locate the <code>VirtualizingStackPanel</code> that can be used to get / set the scroll position. The scroll location is placed in the <code>State</code> dictionary when the user navigates away from the page.</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 NavigationList ItemsPanel</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> VirtualizingStackPanel ItemsPanel
<span style="color: #008000;">&#123;</span>
  get
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> navigationControl<span style="color: #008000;">.</span><span style="color: #0000FF;">Descendants</span><span style="color: #008000;">&lt;</span>VirtualizingStackPanel<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;">Cast</span><span style="color: #008000;">&lt;</span>VirtualizingStackPanel<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;">SingleOrDefault</span><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>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</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> ScrollOffsetKey <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;ScrollOffsetKey&quot;</span><span style="color: #008000;">;</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> OnNavigatedFrom<span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Navigation</span><span style="color: #008000;">.</span><span style="color: #0000FF;">NavigationEventArgs</span> e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnNavigatedFrom</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// persist the scroll position within the page state</span>
  var scroll <span style="color: #008000;">=</span> ItemsPanel<span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>scroll <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>
    State<span style="color: #008000;">&#91;</span>ScrollOffsetKey<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> ItemsPanel<span style="color: #008000;">.</span><span style="color: #0000FF;">ScrollOwner</span><span style="color: #008000;">.</span><span style="color: #0000FF;">VerticalOffset</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Restoring this state is simply a matter of checking for the existence of this key in the <code>State</code> dictionary when the page is navigated to:</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> <span style="color: #6666cc; font-weight: bold;">void</span> OnNavigatedTo<span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Navigation</span><span style="color: #008000;">.</span><span style="color: #0000FF;">NavigationEventArgs</span> e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnNavigatedTo</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// restore the scroll position</span>
  <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Loaded</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e2<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>State<span style="color: #008000;">.</span><span style="color: #0000FF;">ContainsKey</span><span style="color: #008000;">&#40;</span>ScrollOffsetKey<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
          var scroll <span style="color: #008000;">=</span> ItemsPanel<span style="color: #008000;">;</span>
          <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>scroll <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>
            ItemsPanel<span style="color: #008000;">.</span><span style="color: #0000FF;">SetVerticalOffset</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span>State<span style="color: #008000;">&#91;</span>ScrollOffsetKey<span style="color: #008000;">&#93;</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><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Note that the state is restored after the Loaded event is fired because the <code>VirtualizingStackPanel</code> which manages the scroll location is defined within the <code>NavigationList</code> template and hence will not be present in the visual tree when the page is not initially constructed (the same is true if you use a <code>ListBox</code> as well).</p>
<p>So ... finally, we are done!</p>
<h2>Conclusions</h2>
<p>This blog post feels like it has turned into something of an epic! as I mentioned in the introduction tomstoning within WP7 applications is complex and it is easy to get confused with all the various places where state can be stored (It even seems to have <a href="http://jesseliberty.com/2011/04/08/tombstoning-and-mvvm/">Silverlight guru Jesse Liberty in a bit of a muddle!</a>).</p>
<p>It is worth noting that this example tombstones the entire ViewModel state, and in more complex applications it might be more appropriate to tombstone an abstraction of the ViewModel, especially if it is not serializable. However, I think the simple example that this blog presents is still a useful starting point for understanding the tombstoning process.</p>
<p>You can download the sourcecode for this example application: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/05/WP7MVVMTombstone.zip'>WP7MVVMTombstone.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/05/a-simple-windows-phone-7-mvvm-tombstoning-example/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ViewModel INotifyPropertyChanged Code Generation</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/04/viewmodel-inotifypropertychanged-code-generation/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/04/viewmodel-inotifypropertychanged-code-generation/#comments</comments>
		<pubDate>Thu, 21 Apr 2011 12:34:16 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code generation]]></category>
		<category><![CDATA[codeproject]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[t4]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1399</guid>
		<description><![CDATA[This blog post describes a novel method of generating boiler-plate MVVM code using codesnippet automation. You simply add attributes to your view model classes and the code is generated for you! Model-View-ViewModel (MVVM) has become the de facto pattern for Silverlight, WPF and WP7 applications, providing code that is easily tested and couples cleanly to [...]]]></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%252F04%252Fviewmodel-inotifypropertychanged-code-generation%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22ViewModel%20INotifyPropertyChanged%20Code%20Generation%20%23%22%20%7D);"></div>
<p><i>This blog post describes a novel method of generating boiler-plate MVVM code using codesnippet automation. You simply add attributes to your view model classes and the code is generated for you!</i></p>
<p>Model-View-ViewModel (MVVM) has become the de facto pattern for Silverlight, WPF and WP7 applications, providing code that is easily tested and couples cleanly to the view via databinding. However, one small problem with MVVM is that it relies on the <code>INotifyPropertyChanged</code> (INPC) interface and the boiler-plate code which this entails.</p>
<p>This blog post describes a technique for implementing INPC and adding properties to your view model as easily as this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>SnippetINotifyPropertyChanged<span style="color: #008000;">&#93;</span>
<span style="color: #008000;">&#91;</span>SnippetPropertyINPC<span style="color: #008000;">&#40;</span>field <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;_surname&quot;</span>, type <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;string&quot;</span>, property <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Surname&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #008000;">&#91;</span>SnippetPropertyINPC<span style="color: #008000;">&#40;</span>field <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;_forename&quot;</span>, type <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;string&quot;</span>, property <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Forename&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<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> PersonViewModel <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>There are hundreds of blog posts that describe solutions to the problem of implementing INPC including simple options like a <a href="http://joshsmithonwpf.wordpress.com/2007/08/29/a-base-class-which-implements-inotifypropertychanged/">base-class that implements the INPC interface</a>, the popular approach of using <a href=" http://10rem.net/blog/2010/12/16/strategies-for-improving-inotifypropertychanged-in-wpf-and-silverlight">lambda expressions</a> and more complex solutions involving <a href="http://justinangel.net/AutomagicallyImplementingINotifyPropertyChanged">Intermediate Language Weaving</a> (AOP), or <a href="http://www.deanchalk.me.uk/post/WPF-e28093-Easy-INotifyPropertyChanged-Via-DynamicObject-Proxy.aspx">dynamic proxies</a>.  However, for the sake of simplicity, most of the projects I have worked on have opted for a manual approach &#8211; with individual developers using <a href="http://geekswithblogs.net/brians/archive/2010/07/27/inotifypropertychanged-with-less-typing-using-a-code-snippet.aspx">codesnippets</a> if they so wish.</p>
<p>There are a couple of problems with codesnippets, firstly they are not refactor friendly, secondly they do not reduce boiler-plate code, they simply provide a method for adding this code more quickly!</p>
<p>Yesterday I published an article on codeproject which <a href="http://www.codeproject.com/KB/codegen/CodeSnippetAutomation.aspx">describes a technique for &#8216;automating&#8217; code snippets</a>, where you indicate the use of a codesnippet declaratively via an attribute, with the resultant code being generated in a partial class. Here I am going to show how it can be used to streamline the creation of ViewModels and results in the removal of boiler-plate code.</p>
<p>The first step to using this technique is to add the CodeGen folder to your project. This folder includes a number of T4 templates (Visual Studio&#8217;s built in code-generation framework):</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/04/AddCodeGen.png" alt="" title="AddCodeGen" width="344" height="379" class="aligncenter size-full wp-image-1417" /></p>
<p>With these templates added to your project, if you add a codesnippet to the project (i.e. a <code>.snippet</code> file), a corresponding attribute will be generated when the T4 templates are run. You can see in the above screenshot that a couple of snippets have been added and attributes generated. </p>
<p>The T4 templates are run when the project is built, or can be run on-demand by clicking the button indicated below:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/04/TransformAllTemplates.png" alt="" title="TransformAllTemplates" width="302" height="154" class="aligncenter size-full wp-image-1414" /></p>
<p>So let&#8217;s have a go at generating our view model code &#8230;</p>
<p>We&#8217;ll create a simple Person view model, which has properties of Surname, Forename and a dependent property of FullName which concatenates the two together. We&#8217;ll start by adding the INPC implementation to our class via an automated snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>SnippetINotifyPropertyChanged<span style="color: #008000;">&#93;</span>
<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> PersonViewModel <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>By adding the attribute above and compiling the code, you will find that a generated counterpart to our view model is created:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/04/GeneratedViewModel.png" alt="" title="GeneratedViewModel" width="346" height="227" class="aligncenter size-full wp-image-1416" /></p>
<p>This file contains the code defined in our INPC codesnippet:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> ViewModelCodeGeneration
<span style="color: #008000;">&#123;</span>
  <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> PersonViewModel
  <span style="color: #008000;">&#123;</span>
&nbsp;
    <span style="color: #008080;">#region INotifyPropertyChanged Members</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Occurs when a property changes</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;">event</span> PropertyChangedEventHandler PropertyChanged<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Raises a PropertyChanged event</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> property<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>PropertyChanged <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>
        PropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, <span style="color: #008000;">new</span> PropertyChangedEventArgs<span style="color: #008000;">&#40;</span>property<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>
&nbsp;
    <span style="color: #008080;">#endregion</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>We can now add a property to our view model:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>SnippetINotifyPropertyChanged<span style="color: #008000;">&#93;</span>
<span style="color: #008000;">&#91;</span>SnippetPropertyINPC<span style="color: #008000;">&#40;</span>field <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;_surname&quot;</span>, type <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;string&quot;</span>, property <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Surname&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<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> PersonViewModel <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Compiling the code and inspecting the generated file, we can see that the code has been generated based on the codesnippet that adds a property (with INPC <code>PropertyChanged</code>), based on the attribute parameters supplied above:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> ViewModelCodeGeneration
<span style="color: #008000;">&#123;</span>
  <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> PersonViewModel
  <span style="color: #008000;">&#123;</span>
&nbsp;
    <span style="color: #008080;">#region INotifyPropertyChanged Members</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Occurs when a property changes</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;">event</span> PropertyChangedEventHandler PropertyChanged<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Raises a PropertyChanged event</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> property<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>PropertyChanged <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>
        PropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, <span style="color: #008000;">new</span> PropertyChangedEventArgs<span style="color: #008000;">&#40;</span>property<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>
&nbsp;
    <span style="color: #008080;">#endregion</span>
&nbsp;
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Field which backs the Surname property</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;">string</span> _surname <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</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> SurnameProperty <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Surname&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Gets / sets the Surname value</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> Surname
    <span style="color: #008000;">&#123;</span>
      get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _surname<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>_surname <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;
        _surname <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
&nbsp;
        OnSurnameChanged<span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        OnPropertyChanged<span style="color: #008000;">&#40;</span>SurnameProperty<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;">/// Invoked when the value of Surname changes</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnSurnameChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> value<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>Finally, we add the second property, and add implementations for the partial methods that are invoked when either <code>Surname</code> or <code>Forename</code> changes, allowing us to implement <code>FullName</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>SnippetINotifyPropertyChanged<span style="color: #008000;">&#93;</span>
<span style="color: #008000;">&#91;</span>SnippetPropertyINPC<span style="color: #008000;">&#40;</span>field <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;_surname&quot;</span>, type <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;string&quot;</span>, property <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Surname&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #008000;">&#91;</span>SnippetPropertyINPC<span style="color: #008000;">&#40;</span>field <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;_forename&quot;</span>, type <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;string&quot;</span>, property <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Forename&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<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> PersonViewModel <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</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> <span style="color: #6666cc; font-weight: bold;">string</span> FullNameProperty <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;FullName&quot;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnForenameChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> value<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    OnPropertyChanged<span style="color: #008000;">&#40;</span>FullNameProperty<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;">partial</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnSurnameChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> value<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    OnPropertyChanged<span style="color: #008000;">&#40;</span>FullNameProperty<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;">string</span> FullName
  <span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> Surname <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;, &quot;</span> <span style="color: #008000;">+</span> Forename<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>All that&#8217;s left to do is bind our view model to the view:</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;Border</span> <span style="color: #000066;">BorderBrush</span>=<span style="color: #ff0000;">&quot;DarkGray&quot;</span> <span style="color: #000066;">BorderThickness</span>=<span style="color: #ff0000;">&quot;4&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">CornerRadius</span>=<span style="color: #ff0000;">&quot;3&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;Grid</span> <span style="color: #000066;">l:GridUtils.ColumnDefinitions</span>=<span style="color: #ff0000;">&quot;*,*&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">l:GridUtils.RowDefinitions</span>=<span style="color: #ff0000;">&quot;Auto,Auto,&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding FullName}&quot;</span> <span style="color: #000066;">FontWeight</span>=<span style="color: #ff0000;">&quot;Bold&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Grid.ColumnSpan</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;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Surname:&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Surname, Mode=TwoWay}&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>  
&nbsp;
    <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;Forename:&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Forename, Mode=TwoWay}&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <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>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>(Note the <a href="http://www.scottlogic.co.uk/blog/colin/2010/12/a-simplified-grid-markup-for-silverlight-and-wpf/">simplified grid markup</a>!)</p>
<p>Here&#8217;s the code for this very simple example in action:</p>
<div style="text-align: center;"><object width="250" height="100" 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/04/ViewModelCodeGeneration.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>This blog post has described a method for generating the boiler-plate code that the MVVM patterns requires. The use of T4 templates and partial classes mirrors the way that the Visual Studio designer generates code. Personally I think the end result is much easier to understand that the dynamic proxy or aspects / weaving approach described by others. The example given in this blog post is pretty trivial, but when you use it for bigger projects with complex view-models containing numerous properties, you really start to feel the benefit.</p>
<p>Furthermore, the technique described here can be used to automate any codesnippet! you can use it to generate dependency properties for example.</p>
<p>Enjoy!</p>
<p>You can download the sourcecode here: <a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/04/ViewModelGeneration.zip">ViewModelGeneration.zip</a></p>
<p>For more information regarding how T4 / Env.DTE / XSLT are used to automate your codesnippets I would <a href="http://www.codeproject.com/KB/codegen/CodeSnippetAutomation.aspx">encourage you to read my codeproject article</a>.</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/04/viewmodel-inotifypropertychanged-code-generation/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>MVVM Charting &#8211; Binding Multiple Series to a Visiblox Chart</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/03/mvvm-charting-binding-multiple-series-to-a-visiblox-chart/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/03/mvvm-charting-binding-multiple-series-to-a-visiblox-chart/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 21:53:52 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[visiblox]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1243</guid>
		<description><![CDATA[This post describes a method of using attached properties to bind a ViewModel which contains multiple data series to a Visiblox chart without any code-behind. The Visiblox chart supports databinding in both WPF and Silverlight, where the X and Y values for each datapoint are bound to properties on an underlying model. However, there is [...]]]></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%252F03%252Fmvvm-charting-binding-multiple-series-to-a-visiblox-chart%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22MVVM%20Charting%20-%20Binding%20Multiple%20Series%20to%20a%20Visiblox%20Chart%20%23%22%20%7D);"></div>
<p><em>This post describes a method of using attached properties to bind a ViewModel which contains multiple data series to a Visiblox chart without any code-behind.</em></p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/03/harmonic.png" alt="" title="harmonic" width="389" height="253" class="aligncenter size-full wp-image-1247" /><br />
<br/><br />
The <a href="http://www.visiblox.com">Visiblox chart</a> supports databinding in both WPF and Silverlight, where the X and Y values for each datapoint are bound to properties on an underlying model. However, there is no interface for binding a varying number of series (i.e a collection of collections). The solution provided here is similar to the one which Jeremiah Morrill published for <a href="http://jmorrill.hjtcentral.com/Home/tabid/428/EntryId/351/Silverlight-Charts-Binding-multiple-Series.aspx">binding multiple series to the Silverlight Toolkit charts</a>, but with a few added extras, like series title binding and series type selection.</p>
<p>The solution is surprisingly simple, so I am going to dive straight into the code (note, I have collapsed the verbose attached property definitions so that just the interesting bits are shown below!)</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: #6666cc; font-weight: bold;">class</span> MultiSeries
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080;">#region TitlePath attached property</span>
&nbsp;
  <span style="color: #008080;">#region ItemsSourcePath attached property</span>
&nbsp;
  <span style="color: #008080;">#region XValuePath attached property</span>
&nbsp;
  <span style="color: #008080;">#region YValuePath attached property</span>
&nbsp;
  <span style="color: #008080;">#region ChartTypeProvider attached property</span>
&nbsp;
  <span style="color: #008080;">#region Source attached property</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Handles property changed event for the Source property</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnSourcePropertyChanged<span style="color: #008000;">&#40;</span>DependencyObject d,
                                                      DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Chart targetChart <span style="color: #008000;">=</span> d <span style="color: #0600FF; font-weight: bold;">as</span> Chart<span style="color: #008000;">;</span>
&nbsp;
    SynchroniseChartWithSource<span style="color: #008000;">&#40;</span>targetChart<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    IEnumerable Source <span style="color: #008000;">=</span> GetSource<span style="color: #008000;">&#40;</span>targetChart<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    INotifyCollectionChanged incc <span style="color: #008000;">=</span> Source <span style="color: #0600FF; font-weight: bold;">as</span> INotifyCollectionChanged<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>incc <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>
      incc<span style="color: #008000;">.</span><span style="color: #0000FF;">CollectionChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e2<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> SynchroniseChartWithSource<span style="color: #008000;">&#40;</span>targetChart<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
  <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;">void</span> SynchroniseChartWithSource<span style="color: #008000;">&#40;</span>Chart chart<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    chart<span style="color: #008000;">.</span><span style="color: #0000FF;">Series</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    IEnumerable Source <span style="color: #008000;">=</span> GetSource<span style="color: #008000;">&#40;</span>chart<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>Source <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// iterate over each source series</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> seriesDataSource <span style="color: #0600FF; font-weight: bold;">in</span> Source<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">// create a visiblox chart series</span>
      IChartSeries chartSeries <span style="color: #008000;">=</span> GetChartTypeProvider<span style="color: #008000;">&#40;</span>chart<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetSeries</span><span style="color: #008000;">&#40;</span>seriesDataSource<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// resolve the ItemsSource path (if present).</span>
      var itemsSourcePath <span style="color: #008000;">=</span> GetItemsSourcePath<span style="color: #008000;">&#40;</span>chart<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      IEnumerable itemsSource <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>itemsSourcePath<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        itemsSource <span style="color: #008000;">=</span> seriesDataSource<span style="color: #008000;">.</span><span style="color: #0000FF;">GetPropertyValue</span><span style="color: #008000;">&lt;</span>IEnumerable<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>itemsSourcePath<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span>
      <span style="color: #0600FF; font-weight: bold;">else</span>
      <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// if not present, assume this is a collection of collections</span>
        itemsSource <span style="color: #008000;">=</span> seriesDataSource <span style="color: #0600FF; font-weight: bold;">as</span> IEnumerable<span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// resolve the title path</span>
      var titlePath <span style="color: #008000;">=</span> GetTitlePath<span style="color: #008000;">&#40;</span>chart<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      var seriesTitle <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
      <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>titlePath<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        seriesTitle <span style="color: #008000;">=</span> seriesDataSource<span style="color: #008000;">.</span><span style="color: #0000FF;">GetPropertyValue</span><span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>titlePath<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 the data series, and add to the chart.</span>
      chartSeries<span style="color: #008000;">.</span><span style="color: #0000FF;">DataSeries</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BindableDataSeries<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        XValueBinding <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Binding<span style="color: #008000;">&#40;</span>GetXValuePath<span style="color: #008000;">&#40;</span>chart<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,
        YValueBinding <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Binding<span style="color: #008000;">&#40;</span>GetYValuePath<span style="color: #008000;">&#40;</span>chart<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,
        ItemsSource <span style="color: #008000;">=</span> itemsSource,
        Title <span style="color: #008000;">=</span> seriesTitle
      <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
      chart<span style="color: #008000;">.</span><span style="color: #0000FF;">Series</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>chartSeries<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 the value of the named 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> T GetPropertyValue<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #6666cc; font-weight: bold;">object</span> source, <span style="color: #6666cc; font-weight: bold;">string</span> propertyName<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    var property <span style="color: #008000;">=</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetProperty</span><span style="color: #008000;">&#40;</span>propertyName<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>property <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>
      <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #008000;">&#40;</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;The property {0} does not exist on the type {1}&quot;</span>,
        propertyName, source<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>property<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>source, <span style="color: #0600FF; font-weight: bold;">null</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>The <code>MultiSeries</code> class defines a number of attached properties, <code>Source</code> is used to bind the collection of series, this property must be an <code>IEnumerable</code>, but if it also implements <code>INotifyCollectionChanged</code>, we handle the <code>CollectionChanged </code>events to update the chart (adding or removing series). The optional <code>ItemsSourcePath</code> is used to provide the path to the nested collection (more on this later) and the optional <code>TitlePath</code> binds the chart title. The <code>XValuePath</code> and <code>YValuePath</code> properties are used to bind the X &#038; Y values of the chart. Finally, <code>ChartTypeProvider</code> is used to determine the series type (Line, Bar, Column &#8230;) for each of the bound series. The provider must implement the following interface:</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;">/// An interface for providing Visiblox chart series.</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> IChartTypeProvider
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Creates a suitable chart series for the given data</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  IChartSeries GetSeries<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> boundObject<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In most MVVM chart binding applications, you will probably want all the series to have the same type. To achieve this, we can create a simple implementation of this interface which always returns the same chart type:</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 ChartTypeProvider that always returns the Visiblox series</span>
<span style="color: #008080; font-style: italic;">/// type that was supplied in the constructor.</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> DefaultChartTypeProvider <span style="color: #008000;">:</span> IChartTypeProvider
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> Type _seriesType<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> DefaultChartTypeProvider<span style="color: #008000;">&#40;</span>Type seriesType<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _seriesType <span style="color: #008000;">=</span> seriesType<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> IChartSeries GetSeries<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> boundObject<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    var ctr <span style="color: #008000;">=</span> _seriesType<span style="color: #008000;">.</span><span style="color: #0000FF;">GetConstructor</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Type<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</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> <span style="color: #008000;">&#40;</span>IChartSeries<span style="color: #008000;">&#41;</span>ctr<span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</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>In order to simplify the usage of this provider in XAML, we can provide a type converter which allows us to specify the required series type as a string, e.g. <code>ChartTypeProvider="LineSeries"</code>, this makes use of the same framework mechanisms that allow you to specify a <code>Fill</code> as a string, e.g. <code>Fill="Red"</code>, where the result will be to create the following, <code>new SolidColorBrush() { Color = Colors.Red }</code>, a suitable type converter is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>TypeConverter<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>StringToChartTypeProvider<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IChartTypeProvider
<span style="color: #008000;">&#123;</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;">/// A type converter that converts a string into a FixedChartTypeProvider. For example</span>
<span style="color: #008080; font-style: italic;">/// &quot;LineSeries&quot; is converted into a FixedChartTypeProvider which</span>
<span style="color: #008080; font-style: italic;">/// always returns Visblox.Chart.LineSeries instances</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> StringToChartTypeProvider <span style="color: #008000;">:</span> TypeConverter
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">object</span> ConvertFrom<span style="color: #008000;">&#40;</span>ITypeDescriptorContext context,
    CultureInfo culture, <span style="color: #6666cc; font-weight: bold;">object</span> value<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>value <span style="color: #008000;">is</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      var visibloxAssembly <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Chart<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Assembly</span><span style="color: #008000;">;</span>
      var seriesType <span style="color: #008000;">=</span> visibloxAssembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Visiblox.Charts.&quot;</span> <span style="color: #008000;">+</span> value<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</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> <span style="color: #008000;">new</span> DefaultChartTypeProvider<span style="color: #008000;">&#40;</span>seriesType<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ConvertFrom</span><span style="color: #008000;">&#40;</span>context, culture, value<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: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CanConvertFrom<span style="color: #008000;">&#40;</span>ITypeDescriptorContext context, Type sourceType<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>sourceType <span style="color: #008000;">==</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;">&#41;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CanConvertFrom</span><span style="color: #008000;">&#40;</span>context, sourceType<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>A Simple Example</h2>
<p>We&#8217;ll start with a simple example, binding a collection of collections, in this case the <a href="http://en.wikipedia.org/wiki/Harmonic">harmonic vibrations of a string</a>. The series are created 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;
    <span style="color: #008080; font-style: italic;">// a collection of collections</span>
    var harmonics <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>List<span style="color: #008000;">&lt;</span>Point<span style="color: #008000;">&gt;&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;">// plot each harmonic frequency</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> frequency <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> frequency <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">5</span> <span style="color: #008000;">;</span>frequency<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">// create the upper and lower component</span>
      var upperHarmonic <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Point<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      var lowerHarmonic <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Point<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: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span> phase <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> phase <span style="color: #008000;">&lt;</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">PI</span><span style="color: #008000;">;</span> phase<span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>Math<span style="color: #008000;">.</span><span style="color: #0000FF;">PI</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        upperHarmonic<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>phase, Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Sin</span><span style="color: #008000;">&#40;</span>phase <span style="color: #008000;">*</span> frequency<span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> frequency <span style="color: #008000;">*</span> <span style="color: #FF0000;">2.5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        lowerHarmonic<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>phase, Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Sin</span><span style="color: #008000;">&#40;</span>phase <span style="color: #008000;">*</span> frequency <span style="color: #008000;">+</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">PI</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> frequency <span style="color: #008000;">*</span> <span style="color: #FF0000;">2.5</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;">// add each to the collection</span>
      harmonics<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>upperHarmonic<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      harmonics<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>lowerHarmonic<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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> harmonics<span style="color: #008000;">;</span>      
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>We can then use the attached properties defined above to bind this data to the chart. Note, because the bound data is a collection of collections (rather than a collection of items which expose the child collection via a property), the optional <code>ItemsSourcePath</code> is not required:</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.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;vis:Palette</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;palette&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> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;vis:LineSeries&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;LineStroke&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;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;vis:LineSeries&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;LineStroke&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;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;vis:LineSeries&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;LineStroke&quot;</span> <span style="color: #000066;">Value</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;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;vis:LineSeries&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;LineStroke&quot;</span> <span style="color: #000066;">Value</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;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/vis:Palette<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></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>
  <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>
<span style="color: #009900;">              <span style="color: #000066;">Palette</span>=<span style="color: #ff0000;">&quot;{StaticResource palette}&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">LegendVisibility</span>=<span style="color: #ff0000;">&quot;Collapsed&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;Modes of a vibrating string&quot;</span>               </span>
<span style="color: #009900;">              <span style="color: #000066;">local:MultiSeries.ChartTypeProvider</span>=<span style="color: #ff0000;">&quot;LineSeries&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">local:MultiSeries.XValuePath</span>=<span style="color: #ff0000;">&quot;X&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">local:MultiSeries.YValuePath</span>=<span style="color: #ff0000;">&quot;Y&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">local:MultiSeries.Source</span>=<span style="color: #ff0000;">&quot;{Binding}&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></pre></div></div>

<p>Whilst the <code>MultiSeries</code> class allows you to specify the chart type via <code>ChartTypeProvider</code>, it does not provide a mechanism for styling the various series which it produces based on the supplied <code>Source</code>. However, styling can be achieved using the chart&#8217;s <code>Palette</code>, which is a collection of <code>Style</code> instances which are applied to the series in order.</p>
<p>The resulting chart is shown below:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/03/harmonic.png" alt="" title="harmonic" width="389" height="253" class="aligncenter size-full wp-image-1247" /></p>
<h2>A More Complex MVVM Example</h2>
<p>In this example, rather than binding a collection of collections, the following model is bound:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/03/MultiBindingExample.png" alt="" title="MultiBindingExample" width="600" height="208" class="aligncenter size-full wp-image-1254" /></p>
<p><code>CompanySalesViewModel</code> has a collection of <code>SalesTeamViewModel</code> instances, each of these has a name, <code>TeamName</code>, and a collection of <code>SalesInRegionViewModel</code> instances. Note, <code>SalesTeamViewModel</code> also has a string indexer property which will be used to bind this model to a <code>DataGrid</code>.</p>
<p>A custom <code>ChartTypeProvider</code> is used so that we can select chart types based on the bound <code>SalesTeamViewModel</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> <span style="color: #6666cc; font-weight: bold;">class</span> SalesTypeProvider <span style="color: #008000;">:</span> IChartTypeProvider
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> IChartSeries GetSeries<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> boundObject<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    var viewModel <span style="color: #008000;">=</span> boundObject <span style="color: #0600FF; font-weight: bold;">as</span> SalesTeamViewModel<span style="color: #008000;">;</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: #0000FF;">TeamName</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Target&quot;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> LineSeries<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> ColumnSeries<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>The view model is bound to a chart and is also bound to a <code>DataGrid</code> as follows. Note this time the <code>ItemsSourcePath</code> is used to bind the <code>TeamSales</code> property of each <code>SalesTeamViewModel</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.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DropShadowEffect</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;shadow&quot;</span> <span style="color: #000066;">Opacity</span>=<span style="color: #ff0000;">&quot;0.3&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;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;UIElement&quot;</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;collapsedStyle&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;Visibility&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Collapsed&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>
&nbsp;
  <span style="color: #808080; font-style: italic;">&lt;!-- a base style for column series --&gt;</span>
  <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;vis:ColumnSeries&quot;</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;columnBaseStyle&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;PointStroke&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;PointStrokeThickness&quot;</span> <span style="color: #000066;">Value</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;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Effect&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;{StaticResource shadow}&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>
&nbsp;
  <span style="color: #808080; font-style: italic;">&lt;!-- a Visiblox palette --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;vis:Palette</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;palette&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> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;vis:ColumnSeries&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">BasedOn</span>=<span style="color: #ff0000;">&quot;{StaticResource columnBaseStyle}&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;PointFill&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;#4f81bd&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>
    <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;vis:ColumnSeries&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">BasedOn</span>=<span style="color: #ff0000;">&quot;{StaticResource columnBaseStyle}&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;PointFill&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;#c0504d&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>
    <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;vis:ColumnSeries&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">BasedOn</span>=<span style="color: #ff0000;">&quot;{StaticResource columnBaseStyle}&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;PointFill&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;#9bbb59&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>
    <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;vis:LineSeries&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;LineStroke&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;#8064a2&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;LineStrokeThickness&quot;</span> <span style="color: #000066;">Value</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;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Effect&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;{StaticResource shadow}&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>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/vis:Palette<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></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>
<span style="color: #009900;">      <span style="color: #000066;">util:GridUtils.RowDefinitions</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;vis:Chart</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;chart&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;Global Team Sales&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">LegendTitle</span>=<span style="color: #ff0000;">&quot;Team&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">Palette</span>=<span style="color: #ff0000;">&quot;{StaticResource palette}&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">local:MultiSeries.XValuePath</span>=<span style="color: #ff0000;">&quot;Region&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">local:MultiSeries.YValuePath</span>=<span style="color: #ff0000;">&quot;Sales&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">local:MultiSeries.TitlePath</span>=<span style="color: #ff0000;">&quot;TeamName&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">local:MultiSeries.ItemsSourcePath</span>=<span style="color: #ff0000;">&quot;TeamSales&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">local:MultiSeries.Source</span>=<span style="color: #ff0000;">&quot;{Binding SalesTeams}&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:MultiSeries.ChartTypeProvider<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:SalesTypeProvider</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/local:MultiSeries.ChartTypeProvider<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.XAxis<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;vis:CategoryAxis</span> <span style="color: #000066;">GridlineStyle</span>=<span style="color: #ff0000;">&quot;{StaticResource collapsedStyle}&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.XAxis<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>
&nbsp;
  <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;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding SalesTeams}&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">AutoGenerateColumns</span>=<span style="color: #ff0000;">&quot;False&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sdk:DataGrid.Columns<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sdk:DataGridTextColumn</span> <span style="color: #000066;">Binding</span>=<span style="color: #ff0000;">&quot;{Binding TeamName}&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;Team&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">IsReadOnly</span>=<span style="color: #ff0000;">&quot;True&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sdk:DataGridTextColumn</span> <span style="color: #000066;">Binding</span>=<span style="color: #ff0000;">&quot;{Binding [US]}&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;US&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sdk:DataGridTextColumn</span> <span style="color: #000066;">Binding</span>=<span style="color: #ff0000;">&quot;{Binding [UK]}&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;UK&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sdk:DataGridTextColumn</span> <span style="color: #000066;">Binding</span>=<span style="color: #ff0000;">&quot;{Binding [Germany]}&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;Germany&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sdk:DataGridTextColumn</span> <span style="color: #000066;">Binding</span>=<span style="color: #ff0000;">&quot;{Binding [Japan]}&quot;</span></span>
<span style="color: #009900;">                              <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;Japan&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sdk:DataGrid.Columns<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sdk:DataGrid<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>The result is show below, note that updating the data in the grid causes the chart to update accordingly:</p>
<div style="text-align: center;"><object width="400" height="300" 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/03/VisibloxMultiSeriesBinding.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 download the full sourcecode for this example: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/03/VisibloxMultiSeriesBinding2.zip'>VisibloxMultiSeriesBinding.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/03/mvvm-charting-binding-multiple-series-to-a-visiblox-chart/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>The mini-ViewModel pattern</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2009/08/the-mini-viewmodel-pattern/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2009/08/the-mini-viewmodel-pattern/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 16:23:18 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[binding]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=460</guid>
		<description><![CDATA[The construction of a ViewModel is often seen as the standard technique for solving binding problems within WPF and Silverlight. However, the addition of a ViewModel adds complexity to your code. This post describes an alternative method where a mini-ViewModel is applied directly to the problem areas in the view, leaving the rest to use [...]]]></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%252F2009%252F08%252Fthe-mini-viewmodel-pattern%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22The%20mini-ViewModel%20pattern%20%23%22%20%7D);"></div>
<p><em>The construction of a ViewModel is often seen as the standard technique for solving binding problems within WPF and Silverlight. However, the addition of a ViewModel adds complexity to your code. This post describes an alternative method where a mini-ViewModel is applied directly to the problem areas in the view, leaving the rest to use simpler, straightforward binding to business objects. </em></p>
<p>One of the features of WPF / Silverlight that appealed to me immediately when I started to learn it was the flexibility of the binding framework. The concepts of DataContext inheritence and flexibility of the value converters results in a lot less glue-code, which makes me a happy developer! However, it does not take long before you start finding examples that just dont fit with the framework and things start to get just little more complex. This blog post describes one such example.</p>
<p>The example used in this blog post is of a very simple application which displays a business card. The business object rendered by the application, <code>ContactDetails</code>, has a number of simple CLR properties. Constructing a UI for viewing this business object is very easy, simply create an instance of this object (in a real application this object might come from a database or web service) and set it as the DataContext of the view:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> Page<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;
    ContactDetails details <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContactDetails<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Company <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Scott Logic Ltd.&quot;</span>,
        Email <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;ceberhardt@scottlogic.co.uk&quot;</span>,
        URL <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://www.scottlogic.co.uk&quot;</span>,
        Country <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;UK&quot;</span>,
        FullName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Colin Eberhardt&quot;</span>,
        PhoneNumber <span style="color: #008000;">=</span> <span style="color: #FF0000;">4408452241930</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> details<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>We then create some simple XAML with UI elements that bind to the various propeties of our object &#8230;</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;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;Border</span> <span style="color: #000066;">BorderBrush</span>=<span style="color: #ff0000;">&quot;LightGray&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;White&quot;</span>  <span style="color: #000066;">BorderThickness</span>=<span style="color: #ff0000;">&quot;1.5&quot;</span> <span style="color: #000066;">CornerRadius</span>=<span style="color: #ff0000;">&quot;20&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span> <span style="color: #000066;">HorizontalAlignment</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;Border</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">BorderBrush</span>=<span style="color: #ff0000;">&quot;LightGray&quot;</span> <span style="color: #000066;">BorderThickness</span>=<span style="color: #ff0000;">&quot;1.5&quot;</span> <span style="color: #000066;">CornerRadius</span>=<span style="color: #ff0000;">&quot;15&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.Background<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ImageBrush<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ImageBrush.ImageSource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;BitmapImage</span> <span style="color: #000066;">UriSource</span>=<span style="color: #ff0000;">&quot;back.jpg&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ImageBrush.ImageSource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ImageBrush<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border.Background<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;Vertical&quot;</span> <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;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding FullName}&quot;</span></span>
<span style="color: #009900;">                           <span style="color: #000066;">FontSize</span>=<span style="color: #ff0000;">&quot;15&quot;</span></span>
<span style="color: #009900;">                           <span style="color: #000066;">FontWeight</span>=<span style="color: #ff0000;">&quot;Bold&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Rectangle</span> <span style="color: #000066;">Stroke</span>=<span style="color: #ff0000;">&quot;DarkGray&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&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: #000066;">Height</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;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Company}&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;0,10,0,0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;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;e: &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 Email}&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;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;w: &quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;HyperlinkButton</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;{Binding URL}&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;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;p: &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 PhoneNumber}&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;/StackPanel<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 style="color: #000000; font-weight: bold;">&gt;</span></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;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>And we get a lovely looking business card:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/07/card.png" alt="card" title="card" width="196" height="129" class="aligncenter size-full wp-image-462" /></p>
<p>One problem with the above example is the phone number, outputting the raw number &#8217;4408452241930&#8242; is not terribly readable, besides phone numbers have a standard format, in this case, the phone number would be represented as &#8216;+44 (0)845 2241930&#8242;. Also, let&#8217;s make the problem more interesting; you might want to highlight the international dialing code in a different colour, or, if the application is being used by a UK client, display the number in a local format without the international code &#8217;0845 2241930&#8242;. So just how do we achieve this?</p>
<p>In order to break the number up into different blocks of text, with different colours (i.e. styles), we need to modify our view to have multiple TextBlocks mapped to the PhoneNumber property of our business object. The part of our view which renders the phone number is modified 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;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;p: &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;... country code ...&quot;</span> <span style="color: #000066;">Foreground</span>=<span style="color: #ff0000;">&quot;LightGray&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;... area code ...&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;... local number ...&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></pre></div></div>

<p>But just how exactly do we bind the properties of these business objects?</p>
<p>One approach would be to use a value converter for each binding in order to extract the required part of the number, for example a CountryCodeValueConverter could be implemented which grabs the first 2 digits of the number. However, this approach is a little inelligent in that it fragments our logic into a number of separate classes, furthermore, if we want country specific formatting this requires the use of multibindings, which can be rather cumbersome if over-used (for a Silverlight implementation of multi bindings see <a href="http://www.scottlogic.co.uk/blog/colin/2009/06/silverlight-multibindings-how-to-attached-mutiple-bindings-to-a-single-property/">my blog post</a> from earlier this year). </p>
<p>Another approach which is often used to tackle tricky binding problems is the use of the Model-View-ViewModel (MVVM) pattern. With this pattern, our ContactDetails object is the Model, we construct a ViewModel which is a UI-oriented abstraction of the Model and it is this which we bind to the View. WPF guru Josh Smith describes MVVM as <a href="http://joshsmithonwpf.wordpress.com/2008/12/01/the-philosophies-of-mvvm/">&#8220;a value converter on steroids&#8221;</a> in order to highlight the usefulness of the pattern in this context.</p>
<p>The basic approach used here is to bind your View to a ViewModel rather than binding it to the business object directly. The ViewModel is specific to the view and wraps the business object (i.e. Model), forwarding the property values and change notification to the View. This extra layer allows us to supplement the properties of the Model object, adding new computed properties which are specific to the View. In this case, these computed properties can be used to expose &#8216;country code&#8217;, &#8216;area code&#8217;, etc&#8230; to our View. </p>
<p><em>(This is a common pattern so I am not going to reproduce the code required here, however, if you are interested, the attached sourcecode accompanying this article contains a regular ViewModel version as well as a mini-ViewModel implementation)</em></p>
<p>Whilst this approach works, my issue with it is that it adds quite a bit of boiler-plate code for wrapping properties, forwarding events etc &#8230; The removal of this glue-code is the exact reason that I like WPF/Silverlight so much! The MVVM pattern is primarily for supporting the designer-developer paradigm and enabling unit testing of UI applications, in my opinion the use of MVVM for anything else is just plain wrong!</p>
<p>So, how can we solve the problem of providing a nicely formatted phone number without the pain of bashing out line-after-line line of boiler-plate MVVM code? My solution to the problem isn&#8217;t to dispose of the MVVM pattern altogether, rather, it is to localise its usage to just the problem area. The first step is to encapsulate the rendering of the phone number as a 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;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;p: &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:PhoneNumberControl</span> <span style="color: #000066;">Number</span>=<span style="color: #ff0000;">&quot;{Binding PhoneNumber}&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>
...</pre></div></div>

<p>&#8230; which probably makes sense anyway from a perspective of good design and code re-use!</p>
<p>Now that we have moved the phone number property within the <code>PhoneNumberControl</code> we have isolated the problem and can deal with it locally. We can create a <code>PhoneNumberControlViewModel</code> which splits the phone number up into its various components and bind this to the <code>PhoneNumberControl</code>.</p>
<p>The control&#8217;s ViewModel looks something like this:</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> PhoneNumberControlViewModel <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> PhoneNumberControlViewModel<span style="color: #008000;">&#40;</span>PhoneNumberControl ctrl<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        _ctrl <span style="color: #008000;">=</span> ctrl<span style="color: #008000;">;</span>
        ComputeProperties<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;">// listen to property changes in the control (we are interested</span>
        <span style="color: #008080; font-style: italic;">// in just the Number property)</span>
        _ctrl<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> Control_PropertyChanged<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> PhoneNumberControl _ctrl<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _countryCodeText<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _areaCodeText<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _localNumberText<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> CountryCodeText
    <span style="color: #008000;">&#123;</span>
        get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _countryCodeText<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        set <span style="color: #008000;">&#123;</span> _countryCodeText <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;CountryCodeText&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: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> AreaCodeText
    <span style="color: #008000;">&#123;</span>
        get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _areaCodeText<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        set <span style="color: #008000;">&#123;</span> _areaCodeText <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;AreaCodeText&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: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> LocalNumberText
    <span style="color: #008000;">&#123;</span>
        get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _localNumberText<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        set <span style="color: #008000;">&#123;</span> _localNumberText <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;LocalNumberText&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: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Control_PropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, PropertyChangedEventArgs 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;">PropertyName</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Number&quot;</span> <span style="color: #008000;">||</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Country&quot;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            ComputeProperties<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>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> ComputeProperties<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span> formattedNumber <span style="color: #008000;">=</span> _ctrl<span style="color: #008000;">.</span><span style="color: #0000FF;">Number</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>           
        CountryCodeText <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;+&quot;</span> <span style="color: #008000;">+</span> formattedNumber<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        AreaCodeText <span style="color: #008000;">=</span> <span style="color: #666666;">&quot; (&quot;</span> <span style="color: #008000;">+</span> formattedNumber<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;)&quot;</span> <span style="color: #008000;">+</span> formattedNumber<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        LocalNumberText <span style="color: #008000;">=</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> formattedNumber<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">6</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>Note that here the ViewModel is &#8216;wrapping&#8217; the <code>PhoneNumberControl</code> rather than an instance of some Business / Model object.</p>
<p>Now we just set our ViewModel as the DataContext for the View, as per the typical usage of this 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> PhoneNumberControl<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: #008000;">new</span> PhoneNumberControlViewModel<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&#8230; and it doesn&#8217;t work. If you try the above, you will find that the binding on the <code>PhoneNumberControl</code>s Number property no longer works. So why is this? if you look back at where the <code>PhoneNumberControl</code> instance is defined in our business card&#8217;s XAML markup you see that its Number property is bound 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;local:PhoneNumberControl</span> <span style="color: #000066;">Number</span>=<span style="color: #ff0000;">&quot;{Binding PhoneNumber}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>The source of this binding is not explicitly set, so will default to using the DataContext of the <code>PhoneNumberControl</code> instance. However, we have just changed this DataContext in our constructor to something else &#8230; oops!</p>
<p>Solving this problem is actually quite simple, the <code>PhoneNumberControl</code> markup 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;UserControl</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;SLMiniViewModel.PhoneNumberControl&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&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;">x:Name</span>=<span style="color: #ff0000;">&quot;LayoutRoot&quot;</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;{Binding CountryCodeText}&quot;</span> <span style="color: #000066;">Foreground</span>=<span style="color: #ff0000;">&quot;DarkGray&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 AreaCodeText}&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 LocalNumberText}&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>If we modify our binding to the ViewModel 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> PhoneNumberControl<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;
    LayoutRoot<span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PhoneNumberControlViewModel<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The subtle difference being that the ViewModel is now bound to the StackPanel which is the root of our visual tree within the control. This means that the <code>PhoneNumberControl</code> DataContext is still inherited from the business card and is our business object instance, the DataContext switch to the ViewModel is now neatly tucked just within the <code>PhoneNumberControl</code>s visual tree, and it is this DataContext which our TextBlocks will inherit and bind to.</p>
<p>The result is that we can now use our mini-ViewModel embedded within the <code>PhoneNumberControl</code> to expose properties which are more amenable to binding to our View:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/08/card2.png" alt="card2" title="card2" width="210" height="143" class="aligncenter size-full wp-image-485" /></p>
<p>Whilst this is a quite trivial example, in real-world applications, more complex examples where the data representation within the View is dependant on a number of properties are quite common. The source code accompanying this article extends the example a little further by making the formatting dependant on a second property of the business object. This is the sort of problem that would either push you towards multi-binding and numerous fragmented value converters, or a ViewModel.</p>
<p><strong>In conclusion</strong>, the use of a mini-ViewModel which is embedded within a <code>UserControl</code> allows you to solve tricky binding problems without being weighed down by rolling out the MVVM pattern across your entire View. I am not against the MVVM pattern in itself, however I do firmly believe that it should only be used to solve the problems which it is was specifically designed for, i.e. supporting the designer-developer workflow and unit testing.</p>
<p>You can download the source-code for this article: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2009/08/SLMiniViewModel.zip'>SLMiniViewModel.zip</a> &#8211; this project contains both MVVM and mini-MVVM implementations (count the number of lines in the ViewModel and contrast it with the mini-ViewModel).</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2009/08/the-mini-viewmodel-pattern/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

