<?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; silverlight</title>
	<atom:link href="http://www.scottlogic.co.uk/blog/colin/tag/silverlight/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>TweetSearch &#8211; A Cross platform Metro UI WinRT and Silverlight Application</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/09/tweetsearch-a-cross-platform-metro-ui-winrt-and-silverlight-application/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/09/tweetsearch-a-cross-platform-metro-ui-winrt-and-silverlight-application/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 15:04:20 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[cross-platform]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[Windows 8]]></category>
		<category><![CDATA[WinRT]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1674</guid>
		<description><![CDATA[With the Windows 8 preview release earlier this week, developers are now faced with a whole new and exciting Microsoft stack. The Windows 8 architecture has something of a split-personality, incorporating a completely new runtime, WinRT together with the older Win32 and .NET framework. However, these sit on different sides of a physical divide in [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F09%252Ftweetsearch-a-cross-platform-metro-ui-winrt-and-silverlight-application%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22TweetSearch%20-%20A%20Cross%20platform%20Metro%20UI%20WinRT%20and%20Silverlight%20Application%20%23%22%20%7D);"></div>
<p>With the Windows 8 preview release earlier this week, developers are now faced with a whole new and exciting Microsoft stack. The Windows 8 architecture has something of a split-personality, incorporating a completely new runtime, WinRT together with the older Win32 and .NET framework. However, these sit on different sides of a physical divide in both the architecture and the user interface. For more details of this, <a href="http://www.scottlogic.co.uk/blog/colin/2011/09/windows-8-an-os-of-two-halves/" title="Windows 8 – An OS of two halves.">see my earlier article</a>.</p>
<p>The new WinRT APIs support both C# and XAML, having an interface that is very similar to its .NET counterparts. However, there are a number of differences, most importantly much of the UI now resides in a new namespace. It is clear that transitioning from Silverlight, WP7 or WPF development to using WinRT will be quite easy.</p>
<p>I am sure there exists classes of application that sit best in the Win8 Metro UI, for which you will use WinRT for development. There will also be classes of application that sit best in the Win8 classic desktop, for which you will still be using WPF and Silverlight to develop.</p>
<p>However &#8230; there will certainly exist a class of application that sits well in <em>both</em> the Metro and Desktop environment. In the Win8 preview IE does just this. </p>
<p>This introduces a problem, how can we share code between our Silverlight Desktop and WinRT Metro applications?</p>
<p>I have previously <a href="http://www.scottlogic.co.uk/blog/colin/2011/07/silverlight-uk-user-group-presentation-on-cross-platform-xaml/" title="Silverlight UK User Group Presentation on Cross Platform XAML">presented talks</a> and <a href="http://www.scottlogic.co.uk/blog/colin/2010/10/white-paper-silverlight-wpf-and-windows-phone-7-cross-platform-development/" title="White Paper: Silverlight, WPF and Windows Phone 7 cross platform development">written articles</a> on cross-platform application development. So I thought I would grab myself a copy of the Win8 preview release and see how easy it is to code-share between a Silverlight and WinRT Metro application.</p>
<p>This blog post describes a simple TwitterSearch application, the Silverlight version is shown below. Simply type in a search string to see the results retrieved from twitter:</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/09/SilverlightTwitterApp.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>A screenshot of the TwitterSearch application built for WinRT Metro is shown below:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/MetroTwitter.png" alt="" title="MetroTwitter" width="513" height="566" class="aligncenter size-full wp-image-1678" /></p>
<p>When code-sharing between WPF, Silverlight and WP7 applications separation of concerns and solid design patterns are essential in order to navigate the framework differences. For that reason I use the Model-View-ViewModel pattern, this makes it easy to specialise the view for each platform, where they typically differ the most. I will use this same approach for WinRT.</p>
<p>The code is shared between the Silverlight and Win8 Metro application by having two separate solutions which both share files via the Visual Studio file linking feature.</p>
<p>The <code>TwitterSearchViewModel</code> which backs this application simply exposes a search string property and a list of tweets:</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> TwitterSearchViewModel <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> TweetViewModelCollection _tweets <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TweetViewModelCollection<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> IMarshalInvoke _marshalInvoke<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _searchText <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;WinRT&quot;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> TwitterSearchViewModel<span style="color: #008000;">&#40;</span>IMarshalInvoke marshalInvoke<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _marshalInvoke <span style="color: #008000;">=</span> marshalInvoke<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Gets / sets the search term</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> SearchText
  <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span>  <span style="color: #0600FF; font-weight: bold;">return</span> _searchText<span style="color: #008000;">;</span>  <span style="color: #008000;">&#125;</span>
    set
    <span style="color: #008000;">&#123;</span>
      _searchText <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
      OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;SearchText&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Gets the search results</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> TweetViewModelCollection Tweets
  <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _tweets<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    set <span style="color: #008000;">&#123;</span> _tweets <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Gets a command that executes the search</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> ICommand ExecuteSearchCommand
  <span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> DelegateCommand<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> ExecuteSearch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The <code>IMarshalInvoke</code> interface allows the view model to perform asynchronous calls via the Dispatcher, without being dependant on this UI class, more on this later.</p>
<p>It is here that I hit the first problem, WinRT has <code>ICommand</code>, but it lives in a different namespace! Time to add a WinRT compilation symbol to my WinRT project and throw in a pre-compiler directive:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080;">#if WINRT</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Windows.UI.Xaml.Input</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080;">#else</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Input</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080;">#endif</span></pre></div></div>

<p>Now my view model compiles just fine. However, when I hooked it up with the view, my bindings were only working in one direction. After appealing to Twitter, the MSDN forums and the WPF Disciples, <a href="http://www.galasoft.ch/">Laurent Bugnion</a> kindly informed me that WinRT has two different INotifyPropertyChanged interfaces. One within <code>System.ComponentModel</code> and the other in <code>Windows.UI.Xaml.Data</code>, if you use the one from <code>System.ComponentModel</code> it is ignored by the WinRT binding framework. Very confusing!</p>
<p>Fixing this is simply a matter of adding more platform specific namespaces:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080;">#if WINRT</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// used for ICommand</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Windows.UI.Xaml.Input</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// used for INotifyPropertyChanged</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Windows.UI.Xaml.Data</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080;">#else</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Input</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080;">#endif</span></pre></div></div>

<p>My original code used <code>WebClient</code>, which is not present in WinRT. Fortunately <code>HttpWebRequest </code>is present in both, so I changed my code to use that, hence the need for the <code>IMarshalInvoke</code> interface to marshal execution back onto the UI thread.</p>
<p>As a result, much of the core logic, which makes use of Linq-to-XML, in the view model is shared between both applications:</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;">/// Parses the response from our twitter request, creating a list of TweetViewModel instances</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> ParseXMLResponse<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> xml<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>xml<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> TweetViewModel<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>,
                    ProfileImageUrl <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;">Link</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Skip</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Attribute</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;href&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</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;
  _tweets<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>
    _tweets<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;">/// Searches for the given term</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> ExecuteSearch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  IsSearching <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// perform the search</span>
  <span style="color: #6666cc; font-weight: bold;">string</span> uri <span style="color: #008000;">=</span> _twitterUrl <span style="color: #008000;">+</span> SearchText<span style="color: #008000;">;</span>
  HttpWebRequest request <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>HttpWebRequest<span style="color: #008000;">&#41;</span>HttpWebRequest<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Uri<span style="color: #008000;">&#40;</span>uri<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  request<span style="color: #008000;">.</span><span style="color: #0000FF;">BeginGetResponse</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> AsyncCallback<span style="color: #008000;">&#40;</span>ReadCallback<span style="color: #008000;">&#41;</span>, request<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;">/// A callback that receives the search results</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> ReadCallback<span style="color: #008000;">&#40;</span>IAsyncResult asynchronousResult<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  HttpWebRequest request <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>HttpWebRequest<span style="color: #008000;">&#41;</span>asynchronousResult<span style="color: #008000;">.</span><span style="color: #0000FF;">AsyncState</span><span style="color: #008000;">;</span>
  HttpWebResponse response <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>HttpWebResponse<span style="color: #008000;">&#41;</span>request<span style="color: #008000;">.</span><span style="color: #0000FF;">EndGetResponse</span><span style="color: #008000;">&#40;</span>asynchronousResult<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>StreamReader streamReader1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #008000;">&#40;</span>response<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</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;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> resultString <span style="color: #008000;">=</span> streamReader1<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadToEnd</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;">// marshall onto the UI thread and parse</span>
    _marshalInvoke<span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
      <span style="color: #008000;">&#123;</span>
        Tweets<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>
        ParseXMLResponse<span style="color: #008000;">&#40;</span>resultString<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        IsSearching <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span><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 makes me happy <img src='http://www.scottlogic.co.uk/blog/colin/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>On a side note, whilst WinRT does not have <code>WebClient</code>, it actually has something much better via classes such as <code>SyndicationFeed </code> which make use of the new C# <code>async </code>and <code>await</code> pattern for dealing with asynchrony. Of course, using these new APIs reduces the amount of code you share with the Silverlight .NET code.</p>
<p>The <code>DelegateCommand </code>implementation again hits the same problem of the same interface being located in a different namespace, yet despite this, most of the code is shared:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080;">#if WINRT</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// used for ICommand</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Windows.UI.Xaml.Input</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080;">#else</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Input</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080;">#endif</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> SLUGUK<span style="color: #008000;">.</span><span style="color: #0000FF;">ViewModel</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> DelegateCommand <span style="color: #008000;">:</span> ICommand
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Action _action<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> DelegateCommand<span style="color: #008000;">&#40;</span>Action action<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      _action <span style="color: #008000;">=</span> action<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CanExecute<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> parameter<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080;">#if WINRT</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">event</span> Windows<span style="color: #008000;">.</span><span style="color: #0000FF;">UI</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Xaml</span><span style="color: #008000;">.</span><span style="color: #0000FF;">EventHandler</span> CanExecuteChanged<span style="color: #008000;">;</span>
<span style="color: #008080;">#else</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">event</span> EventHandler CanExecuteChanged<span style="color: #008000;">;</span>
<span style="color: #008080;">#endif</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Execute<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> parameter<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      _action<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>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Despite the various XAML UI controls being in a different C# namespace, they use the same URIs for their XML namespace mappings. This makes it possible to share XAML as well as C# code between your applications.</p>
<p>However, in much the same way as it makes little sense to share XAML between WP7 and Silverlight due to differences in form-factor and user-experience, I don&#8217;t think it makes sense to share XAML between Silverlight and Metro WinRT. The primary focus of the Win8 Metro applications is the touch-first tablet form factor, hence you would expect it to be quite different from a desktop application.</p>
<p>The following is the XAML for my Metro UI application:</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;WinRTMetroTwitter.View.TwitterSearchView&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>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:d</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/expression/blend/2008&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:mc</span>=<span style="color: #ff0000;">&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:local</span>=<span style="color: #ff0000;">&quot;using:WinRTMetroTwitter.View&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">mc:Ignorable</span>=<span style="color: #ff0000;">&quot;d&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">d:DesignHeight</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">d:DesignWidth</span>=<span style="color: #ff0000;">&quot;400&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;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;local:BoolToVisibilityConverter</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;BoolToVisibilityConverter&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:ImageConverter</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;ImageConverter&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</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;#FF0C0C0C&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;">Margin</span>=<span style="color: #ff0000;">&quot;50&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;400&quot;</span></span>
<span style="color: #009900;">              <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;Auto&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;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;TextBox</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=SearchText, Mode=TwoWay}&quot;</span></span>
<span style="color: #009900;">                     <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;200&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;{Binding Path=ExecuteSearchCommand}&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Go!&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span></span>
<span style="color: #009900;">                     <span style="color: #000066;">Visibility</span>=<span style="color: #ff0000;">&quot;{Binding Path=IsSearching, Converter={StaticResource BoolToVisibilityConverter}}&quot;</span></span>
<span style="color: #009900;">                     <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;Searching ...&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;">FontSize</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;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
            <span style="color: #808080; font-style: italic;">&lt;!-- renders the search results --&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;itemsControl&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;ItemsControl.ItemsPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsPanelTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel</span> <span style="color: #000066;">Orientation</span>=<span style="color: #ff0000;">&quot;Vertical&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsPanelTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl.ItemsPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ItemsControl.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;Horizontal&quot;</span></span>
<span style="color: #009900;">                                    <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Image</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;{Binding ProfileImageUrl, Converter={StaticResource ImageConverter}}&quot;</span></span>
<span style="color: #009900;">                                  <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;96&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;96&quot;</span></span>
<span style="color: #009900;">                                  <span style="color: #000066;">Stretch</span>=<span style="color: #ff0000;">&quot;UniformToFill&quot;</span></span>
<span style="color: #009900;">                                  <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;2&quot;</span></span>
<span style="color: #009900;">                                  <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&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: #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;">TextWrapping</span>=<span style="color: #ff0000;">&quot;Wrap&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;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span></span>
<span style="color: #009900;">                                           <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&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>
&nbsp;
                                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">FontWeight</span>=<span style="color: #ff0000;">&quot;Bold&quot;</span></span>
<span style="color: #009900;">                                               <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=Author}&quot;</span></span>
<span style="color: #009900;">                                               <span style="color: #000066;">FontSize</span>=<span style="color: #ff0000;">&quot;15&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;">Margin</span>=<span style="color: #ff0000;">&quot;10,0,0,0&quot;</span></span>
<span style="color: #009900;">                                              <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Path=Timestamp}&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;">Foreground</span>=<span style="color: #ff0000;">&quot;#333&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;/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;/ItemsControl.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ItemsControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Rectangle</span> <span style="color: #000066;">Fill</span>=<span style="color: #ff0000;">&quot;#FF0C0C0C&quot;</span></span>
<span style="color: #009900;">                   <span style="color: #000066;">Opacity</span>=<span style="color: #ff0000;">&quot;0.5&quot;</span></span>
<span style="color: #009900;">                   <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">                   <span style="color: #000066;">Visibility</span>=<span style="color: #ff0000;">&quot;{Binding Path=IsSearching, Converter={StaticResource BoolToVisibilityConverter}}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>If you are a Silverlight developer you will notice that<em> everything here</em> is standard Silverlight, it is just wired-up to different classes via the XAML namespace mappings.</p>
<p>I did discover that the IValueConverter signature within WinRT is a bit different. Within this application, the value conversion logic is so simple it is hardly worth code-sharing, however, it would be a very simple exercise if you wished to do so.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">namespace</span> WinRTMetroTwitter<span style="color: #008000;">.</span><span style="color: #0000FF;">View</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> BoolToVisibilityConverter <span style="color: #008000;">:</span> IValueConverter
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Convert<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, <span style="color: #6666cc; font-weight: bold;">string</span> typeName, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, <span style="color: #6666cc; font-weight: bold;">string</span> language<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>value <span style="color: #008000;">?</span> Visibility<span style="color: #008000;">.</span><span style="color: #0000FF;">Visible</span> <span style="color: #008000;">:</span> Visibility<span style="color: #008000;">.</span><span style="color: #0000FF;">Collapsed</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> ConvertBack<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> value, <span style="color: #6666cc; font-weight: bold;">string</span> typeName, <span style="color: #6666cc; font-weight: bold;">object</span> parameter, <span style="color: #6666cc; font-weight: bold;">string</span> language<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> NotImplementedException<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Finally, I did hit against one further problem. With WinRT it seems that there is a currently a bug which prevents <code>ItemsControl </code>binding to collections via <code>INotifyCollectionChanged </code>from working. This has been raised in the <a href="http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/aec84969-0947-4fe8-bcf6-ab533e7ab894">MSDN forums</a> and I am sure it will be fixed. As a simple workaround I added the following code-behind (gasp!):</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;">sealed</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> TwitterSearchView <span style="color: #008000;">:</span> UserControl
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> TwitterSearchViewModel feedViewModel<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> TwitterSearchView<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;">Loaded</span> <span style="color: #008000;">+=</span> ResultsView_Loaded<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> ResultsView_Loaded<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span> 
    feedViewModel <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #0600FF; font-weight: bold;">as</span> TwitterSearchViewModel<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// detect collection changed</span>
    feedViewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">Tweets</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CollectionChanged</span> <span style="color: #008000;">+=</span> FeedItems_CollectionChanged<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> FeedItems_CollectionChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, NotifyCollectionChangedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// manually 'refresh' the UI</span>
    itemsControl<span style="color: #008000;">.</span><span style="color: #0000FF;">ItemsSource</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    itemsControl<span style="color: #008000;">.</span><span style="color: #0000FF;">ItemsSource</span> <span style="color: #008000;">=</span> feedViewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">Tweets</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h2>Conclusions</h2>
<p>Creating a cross-platform WinRT / Silverlight application is entirely possible, using the same techniques that we currently employ for cross-platform Silverlight / WPF / WP7 applications. There are further complications relating to classes living in different namespaces and there will of course be other detail-level differences that this simple example does not touch upon. However, I think this presents a feasible and promising route for re-using existing Silverlight/ WPF code, and for developing applications that target the Win8 tablet, WP7 phone and Silverlight (or WPF) desktop with a single code-base.</p>
<p>You can download the full sourcecode: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/CrossPlatformTwitter.zip'>CrossPlatformTwitter.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/09/tweetsearch-a-cross-platform-metro-ui-winrt-and-silverlight-application/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Windows 8 &#8211; An OS of two halves.</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/09/windows-8-an-os-of-two-halves/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/09/windows-8-an-os-of-two-halves/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 20:59:26 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[Windows 8]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1662</guid>
		<description><![CDATA[In this blog post I take a look at Windows 8 on the outside, from a user perspective; and on the inside, from a developer perspective to see how it will change the way we develop with Windows on tablets, smartphones and the desktop when it is released next year. In Anaheim, California, the Microsoft [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F09%252Fwindows-8-an-os-of-two-halves%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Windows%208%20-%20An%20OS%20of%20two%20halves.%20%23%22%20%7D);"></div>
<p><em>In this blog post I take a look at Windows 8 on the outside, from a user perspective; and on the inside, from a developer perspective to see how it will change the way we develop with Windows on tablets, smartphones and the desktop when it is released next year.</em></p>
<p>In Anaheim, California, the Microsoft //build/ conference is drawing to a close. And as the attendees head for home, with their shiny new Samsung tablets under their arms, the news of Windows 8 and the impact it has on end-users and developers is starting to sink in around the globe.</p>
<p>There is no doubt that the impact of Windows 8 is going to be very large, it heralds the biggest change in the Windows architecture since 1993 and from an end-user perspective it is like no other version of Windows before it.</p>
<p>So why such a radical change to both the UI and the architecture? and why now? The answer to this question is quite simple &#8230; <strong>tablets</strong>. Previous versions of Windows have been targeted primarily at the desktop, laptop and netbook form-factors where the primary input devices are mouse and keyboard, and memory, disk-space, CPU performance is aplenty. Tablets have made the odd cameo appearance in the history of computing, but it is only recently with the launch of the iPad that they have suddenly become mainstream.</p>
<p>To take the Windows OS to a tablet it needs to be stripped-down, made faster and support multi-touch (or touch-first &#8211; a new Microsoft buzzword) and with Windows 8 Microsoft have done just that. With the requirements of a tablet interface being so different to a desktop, how have Microsoft reconciled these differences? The answer is a little surprising &#8230; <em> they haven&#8217;t</em>! They could have launched a dedicated Tablet Windows OS, just as they have done on the mobile with Windows Phone 7, but instead they have launched a single OS that works for both tablet and desktop. As a result it has something of a split personality. You can choose to run either the Metro or the &#8216;classic&#8217; desktop UI on your tablet computer and you have exactly the same choice on your desktop.</p>
<h2>An end-user perspective</h2>
<p>The Windows 8 user interface uses the same Metro style that Microsoft introduced with Windows Phone 7, bright colours, clean edges, a lack of drop-shadows and other adornments. The style is heavily influenced by fonts and designs used for road and street-signs. The same concepts of clarity-at-a-distance translates very well to a small smartphone screen and should be equally at home on the tablet.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/windows8.jpg" alt="" title="windows8" width="560" height="315" class="aligncenter size-full wp-image-1663" /></p>
<p>When applications are launched they run full-screen, with a similar multi-tasking model to Windows Phone 7 where  hidden applications are suspended. These features, combined with the fast shutdown / restart times make sense for tablet users where limiting the work that the processor does will significantly enhance battery life.</p>
<p>You can run multiple applications side-by-side in a split-screen mode:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/windows-8-split-screen.jpg" alt="" title="windows-8-split-screen" width="560" height="335" class="aligncenter size-full wp-image-1664" /></p>
<p>The current preview of Windows 8 has a few of the familiar applications available in the new Metro-desktop, these include IE which is now chromeless, i.e. it is permanently in full-screen mode. Gestures are used to reveal the open tabs or URL. Notably the Metro IE does not support any plugins, i.e. Flash, Silverlight &#8211; more on this later!</p>
<p>Metro applications for Windows 8 are distributed via the Windows Marketplace in much the same way as they are for Windows Phone (and iPad, Android etc&#8230;). </p>
<p>The Windows 8 UI clearly has tablets in mind and recent reviews by <a href="http://www.engadget.com/2011/09/13/windows-8-for-tablets-hands-on-preview/">Engadget</a> and <a href="http://gizmodo.com/5839665/windows-8-slate-hands-on-its-fantastic-but-dont-sell-your-ipad">Gizmodo</a> seem to indicate that the experience is very good and agree that when released it could be a serious competitor to the iPad.</p>
<p>However, hidden behind the glossy Windows 8 UI sits the more traditional Windows 7 style desktop. Simply click (or touch) the desktop button and the old-familiar Windows desktop appears. It almost feels like the desktop is an app running within the new Metro UI &#8211; you can also run a split screen which combines the Metro and desktop UIs:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/MetroDesktop.jpg" alt="" title="MetroDesktop" width="560" height="315" class="aligncenter size-full wp-image-1666" /></p>
<p>Interestingly if you <em>want </em>to run IE with plugins, there is a fully-featured version of IE present on the desktop. Try explaining these differences to your grandmother!</p>
<p>For a tablet, Windows 8 gives you the best of both worlds, an immersive and tactile Metro interface, plus the old-familiar Windows desktop.</p>
<p>On the desktop, Windows 8 does feel a bit strange. Metro applications are designed to work with the mouse and the keyboard, so it functions just fine, however, on a desktop monitor the Metro UI feels like a bit large and a touch gimmicky. The Metro concepts work well for small screens, but in my opinion are less effective on a large desktop monitor, especially in the absence of touch. I doubt it will make a big impact for desktop users, but at least they have the choice! When used on a PC with multiple monitors the new Metro UI is confined to a single screen, again further illustrating its tablet focus.</p>
<h2>A developer perspective</h2>
<p>Windows 8 marks a radical change from the outside, but what about on the inside? What is the developer story?</p>
<p>One of the most important diagrams revealed in the keynote is the Windows 8 architecture diagram:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/Windows8Architecture.jpg" alt="" title="Windows8Architecture" width="550" height="309" class="aligncenter size-full wp-image-1667" /></p>
<p>It is clear that the split personality of Windows 8 runs very deep. In this diagram you can see that Metro style apps and desktop apps share the kernel, but nothing else. This is a pretty significant change in Windows architecture; Metro apps are finally free of the Win32 APIs which .NET has been built upon.</p>
<p>The APIs for developing Metro apps reside within the WinRT. A few keys things to know about this new architecture are:</p>
<ul>
<li>WinRT applications are chromless, fullscreen, with no pop-up or modal dialogs</li>
<li>WinRT is an Object Oriented replacement for Win32</li>
<li>WinRT is very similar to .NET, however a number of APIs such as file I/O, security etc&#8230; are not present.</li>
<li>You can develop with C++, or C# etc&#8230; in much the same way as .NET applications</li>
<li>The UI defined using XAML, as per WPF and Silverlight</li>
<li>You can potentially port Silverlight applications to WinRT by changing a few namespaces</li>
<li>JavaScript is a first-class citizen of Windows 8, using a HTML UI</li>
</ul>
<p>The above already tells us a lot about WinRT and Metro applications. My first thoughts are that they are much more similar to Windows Phone 7 applications than desktop WPF applications. They live within a sandbox which uses a restricted subset of APIs, you certainly cannot write a virus checked or hard disk partitioning tool with WinRT!</p>
<p>Furthermore, when developing Metro UI apps, Visual Studio &#8216;deploys&#8217; your application too your Windows 8 Metro desktop in exactly the same way as it does with the WP7 phone.</p>
<p>So what about existing .NET applications? The important thing to know is that all the &#8216;old&#8217; APIs are still there. Your WPF, Silverlight and WinForms applications are safe! There were also a few new WPF features announced at //build/ as part of .NET4.5, but they are not very interesting, it is clear that Microsoft has been putting its energy into other things.</p>
<p>The JavaScript / HTML5 story is a bit of an odd one. Clearly Microsoft has embraced these technologies, with JavaScript having full access to the WinRT. Furthermore, they have added <a href="http://msdn.microsoft.com/en-us/library/windows/apps/br211385">APIs for creating Metro UIs</a> with HTML. However, I am not sure why someone would want to use JavaScript to create Windows 8 Metro applications? The main strength of JavaScript and HTML5 is the portability due to its support on a wide range of browsers, tablets, phones, etc&#8230; The Windows 8 JavaScript Metro apps are &#8216;tied&#8217; to WinRT so are in no way portable.</p>
<h2>Questions &#8230;</h2>
<p>Lots of questions &#8230; the rather complicated nature of Windows 8 has resulted in quite a lot of confusion. </p>
<p><strong>Is Silverlight Dead?</strong> &#8211; this was probably the most oft asked question before //build/ and it is still being asked. The news that the<a href="http://blogs.msdn.com/b/b8/archive/2011/09/14/metro-style-browsing-and-plug-in-free-html5.aspx"> Metro IE10 will not run Silverlight</a> is certainly not good news for the technology, however, it does not mean it is dead &#8211; Silverlight is still present in the desktop IE10 browser (as is Flash). Recently Silverlight has been re-positioned as the technology of choice for more complex line-of-business applications, the types of application which sit better in the Windows 8 &#8216;classic&#8217; desktop. Furthermore, whilst the Metro UI is leading the push into tablet computing, I doubt it will take off on the desktop. So, no, Silverlight is not dead, but it has unfortunately missed out on being part of the Metro UI for tablets.</p>
<p>There is a relatively simple migration path for <a href="http://msdn.microsoft.com/en-us/library/windows/apps/hh465136%28v=VS.85%29.aspx">Silverlight and Windows Phone 7 applications to the new Metro WinRT</a>. This might work for full-screen Silverlight applications, but will not work for applications that mix HTML and Silverlight content within a browser.</p>
<p><strong>Is WPF Dead?</strong> &#8211; Again, for much the same reasons as Silerlight the answer is &#8220;no&#8221;. WPF is a framework intended for line-of-business applications, and those belong on the Windows 8 desktop.</p>
<p><strong>How does this impact Windows Phone 7?</strong> &#8211; it doesn&#8217;t. The next version of WP7 is going RTM shortly and there has been no change to these plans. In fact for WP7 developers Windows 8 is a great opportunity. The WinRT seems most closely related to Silverlight for WP7, so your skills and code are readily portable to the tablet.</p>
<p><strong>Should I write my app as a desktop or Metro app?</strong> &#8211; this is probably the single hardest question that the Windows 8 preview has raised. As application developers we now have two very different ways which we can deliver applications to the desktop and tablet, either the new Metro UI, powered by the (deliberately) limited WinRT; or desktop applications using WPF or Silverlight. I think it will be very interesting to see how Microsoft tackles this problem with its own applications. So far the Windows 8 preview has Metro UI based Twitter, Weather and Social Networking applications and a desktop based Visual Studio. There is also IE which is available in both desktop and Metro, albeit with quite different functionality. Where will Outlook, Word, Excel sit in the new Windows 8?</p>
<p>I think there are rather a lot of applications that will want to make use of both Metro and desktop UIs. Take Outlook for example, you could imagine a simplified Metro interface sitting on one of your monitors, but a more feature rich desktop version available on the other.</p>
<p>One other point worth considering is &#8216;reach&#8217;. It will take a long time before everyone is running a Windows 8 PC, so if you choose to deliver your application as a Metro app, you are restricting yourself just to the Windows 8 user-base. However, if you are targeting the Windows tablet, you can of course guarantee that they will run Windows 8.</p>
<h2>Conclusions</h2>
<p>Windows 8 presents a significant change in the way we develop Windows applications.  The split personality of the Windows 8 user interface and architecture is going to cause confusion on many levels.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/09/TwoHeaded.jpg" alt="" title="TwoHeaded" width="461" height="318" class="aligncenter size-full wp-image-1668" /></p>
<p>The technical implications of WinRT and how it relates to .NET are slowly starting to become clear, thanks to great blog posts like <a href="http://tirania.org/blog/archive/2011/Sep-15.html">&#8220;WinRT Demystified&#8221;</a>.</p>
<p>Personally what I think will take a little bit longer to understand is how we design applications for Windows 8, as we start to discover what works well in Metro, what should remain on the desktop and what we deliver to both.</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/09/windows-8-an-os-of-two-halves/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Can Microsoft ‘fix’ JavaScript and make HTML5 applications viable?</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/08/can-microsoft-%e2%80%98fix%e2%80%99-javascript-and-make-html5-applications-viable/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/08/can-microsoft-%e2%80%98fix%e2%80%99-javascript-and-make-html5-applications-viable/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 11:59:32 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[silverlight]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1642</guid>
		<description><![CDATA[Microsoft’s recent change in stance over Silverlight, promoting HTML5 as “the only true cross-platform solution for everything”, seems to have sidelined Silverlight as a niche framework. This has understandably caused a great deal of upset and confusion in the .NET development community. Despite this, Microsoft are remaining steadfast and tight-lipped about their vision for tighter [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F08%252Fcan-microsoft-%2525e2%252580%252598fix%2525e2%252580%252599-javascript-and-make-html5-applications-viable%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Can%20Microsoft%20%E2%80%98fix%E2%80%99%20JavaScript%20and%20make%20HTML5%20applications%20viable%3F%20%23%22%20%7D);"></div>
<p><em>Microsoft’s recent change in stance over Silverlight, promoting HTML5 as “<a href="http://www.zdnet.com/blog/microsoft/microsoft-our-strategy-with-silverlight-has-shifted/7834">the only true cross-platform solution for everything</a>”, seems to have sidelined Silverlight as a niche framework. This has understandably caused a great deal of upset and confusion in the .NET development community. Despite this, Microsoft are remaining steadfast and tight-lipped about their vision for tighter integration of HTML5 into Windows 8 (and as a result just how side-lined Silverlight will be), until the <a href="http://www.buildwindows.com/">BUILD conference</a> next month. Recently I have read numerous blog posts about the difference between HTML5 and Silverlight, and how to select the right technology for your development, however almost all of them seem to ignore the great-big thorn in HTML5s side &#8230; JavaScript. In this blog post I will explain why JavaScript is such a big issue for Microsoft and what it could do to solve this.</em></p>
<p>That HTML5 will be a huge success is a given. The specification is not finished yet; the parts that are finished are not fully-rolled out, however it is making technology headlines on a daily basis. The HTML5 buzz has also been felt by both technical and non-technical managers, who you will often find asking, <em>“why aren’t we creating HTML5 applications?”</em>. Pointing at the unfinished specification is a little unfair on HTML5, you can reliably use a number of the APIs right now, and they will certainly provide a ‘boost’ to your current web-offerings.</p>
<p>HTML5 is a real mixed-bag of features, from the complex, such as canvas and video, to simple semantic markup improvements and a powerful set of CSS features. Because HTML5 provides a spectrum of features from the simple to the complex, I don’t doubt that every new site will be using HTML5 in a few years time, you simply have to add rounded borders or a <code>&lt;header&gt;</code> tag to earn yourself the coveted HTML5 badge!</p>
<p>One concept that underpins the HTML5 specification(s) is that of client-side web-applications. The server-side web-application, where business logic and presentation logic reside on the server, spitting out markup (sprinkled with a bit of JavaScript) are becoming an architecture of the past. The model where the server exposes business logic as a service makes it much easier to provide a front-end on a range of devices (mobile, tablet, desktop), a range of operating systems (Windows, iOS, Android), allows the effective use of client CPU, and provides a more accessible service that customers can integrate into their own application. From my perspective the most significant undertone of the HTML5 specification is that it supports this model. </p>
<p>The problem is that the name ‘HTML5’ can be a little misleading. Whilst the specification has much to do with HTML, it has much more to do with JavaScript. In fact, many of the HTML5 APIs have nothing to do with presentation or layout. Any article which compares HTML5 and Silverlight, without focussing on JavaScript is really missing the point.</p>
<p>So, what is my big issue with JavaScript?</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/08/broken_glass.jpg" alt="" title="SONY DSC" width="500" height="333" class="aligncenter size-full wp-image-1643" /><br />
<small>Image used under Creative Commons Licence from <a href="http://www.flickr.com/photos/hanne_exurban/4423655940/sizes/m/in/photostream/">Flickr</a></small></p>
<p>JavaScript was hastily developed as a language for scripting the web. Unfortunately it has not evolved much since its inception. Certainly the APIs that surround it have evolved, including the DOM APIs, HTML5 and popular libraries such as jQuery, however the language itself remains largely unchanged.</p>
<p>The big issue with JavaScript is that it is not well suited for the development of large applications, and if we shift to a model where the server provides a service which is consumed by the client web-application, this necessitates a lot of JavaScript code. I don’t want to go into all the gory details of the problems with JavaScript, but here are a few:</p>
<ul>
<li>Many language quirks and gotchas (see this <a href="http://www.codeproject.com/KB/scripting/javascript-gotchas.aspx">article on codeproject</a>) </li>
<li>A lack of ‘familiar’ object-oriented concepts (I am not going to make the mistake of saying it is not an OO language!)</li>
<li>A lack of dependency management</li>
<li>A lack of organisational structure (namespaces)</li>
<li>A very small ‘base’ library (for example no event support, string formatting, etc&#8230;)</li>
<li>It is not strongly-typed</li>
<li>A confusing array of patterns for solving some of the above!</li>
</ul>
<p>This list is certainly not exhaustive; rather it is just a brief illustration of some of the obstacles that JavaScript presents to the developer of complex applications. Many of the items listed above are features of the language that are advantageous in a simple, scripting context, but when used to develop complex applications they are quite troublesome. </p>
<p>Another issue with JavaScript is not so much the language itself, more the attitude of developers towards the language. Because of its quirks and its predominant usage as a language for scripting web-pages (with the usual &#8220;google → cut → paste&#8221; approach) many developers simply don’t bother learning how it works. I have interviewed many job candidates who list ‘JavaScript’ as one of their skills, yet they cannot describe the difference between the binding of the ‘this’ variable between JavaScript and C#/Java.</p>
<p>There are numerous JavaScript frameworks that go some way towards solving the above issues &#8230; filling in the gaps. This is an area that Microsoft has dabbled in before, with the Microsoft Ajax Library adding inheritance patterns and .NET style libraries (albeit with a very clumsy, verbose and convention-heavy syntax!). </p>
<p>Whilst the list of issues above presents a challenge to the JavaScript developer, they provide just as great a challenge to the tool providers. Many features of existing IDEs, Intellisense (auto-completion) and structured views of the code rely on language features that are absent from JavaScript. Again, Microsoft has provided some innovations in this area, with <a href="http://weblogs.asp.net/scottgu/archive/2010/04/08/javascript-intellisense-improvements-with-vs-2010.aspx">pseudo-execution of JavaScript code providing some Intellisense support</a>. Unfortunately this neat feature, which works well if you have all your code in a single JavaScript file, is less easy to manage on a large, more complex application. From personal experience I find Visual Studio adds little value to my JavaScript development experience, I am equally happy with Notepad++ and JSLint!</p>
<p>So when Microsoft announces their tighter integration of HTML5 / JavaScript into Windows 8, what innovations are they going to announce?</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/08/build_logo.png" alt="" title="build_logo" width="252" height="90" class="aligncenter size-full wp-image-1644" /></p>
<p>Personally, I think if Microsoft is serious about HTML5 and its potential for client-side application development they need to do much more than just add CSS3, canvas and video support to ASP.NET. They need to support the client-side application model, where ASP.NET no longer has a stake. To fully support this they need to ‘fix’ the problems with JavaScript.</p>
<p>One of Microsoft’s most prized assets is Visual Studio, their investment in tool-support allows you to use the same quality tooling for developing in a wide range of languages (C#, VB.NET, C++, F#, &#8230;) and frameworks (WinForms, MFC, WPF, Silverlight, ASP.NET, &#8230;). To fully embrace HTML5 / JavaScript it has to be fully embraced by Visual Studio. But therein lies the problem, the lack of namespacing, familiar object oriented concepts, strong-typing and base ‘class’ framework makes this a challenge.<br />
I think the worst thing Microsoft could do is add their own JavaScript framework to the mix, further fragmenting the JavaScript development experience. Thankfully they recently adopted jQuery within their ASP.NET framework, showing signs that they are willing to work with existing open-source frameworks. So if they do not announce a new JavaScript framework, what could they provide instead?</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/08/gears.jpg" alt="" title="Gears" width="500" height="375" class="aligncenter size-full wp-image-1645" /><br />
<small>Image used under Creative Commons Licence from <a href="http://www.flickr.com/photos/hanne_exurban/4423655940/sizes/m/in/photostream/">Flickr</a></small></p>
<p>Microsoft’s Scott Henselmann has published a couple of interesting blog posts about the concept of JavaScript being the <a href="http://www.hanselman.com/blog/JavaScriptIsAssemblyLanguageForTheWebSematicMarkupIsDeadCleanVsMachinecodedHTML.aspx">assembly language of the web</a> (or the JVM of the web depending on your perspective). His <a href="http://www.hanselman.com/blog/JavaScriptisAssemblyLanguagefortheWebPart2MadnessorjustInsanity.aspx">second blog post</a> included comments from a number of JavaScript thought leaders including its creator Brendan Eich who said <em>“Believe it or not, classical OOP languages &#8212; especially ones with not-very-expressive static type systems &#8212; are not the only way to build large apps. It can be done in JS, but it&#8217;s harder than it ought to be”</em>.</p>
<p>Google have already made some innovations in this area, the first is Google Web Toolkit (GWT), which allows developers to write Java code (using the powerful Eclipse IDE), which is compiled into JavaScript. The second is Google Closure Compiler, a JavaScript to JavaScript compiler / optimiser that provides type checking via annotated comments. It is this area that I think Microsoft should be investing in.</p>
<p>Microsoft could create a C# (or VB.NET etc&#8230;) to JavaScript compiler with an accompanying set of .NET base-class libraries. This would significantly boost Microsoft’s JavaScript offering in that it would be fully supported by Visual Studio, providing the developer with the quality tooling that they are used to when developing using other .NET frameworks. Furthermore, the C# to JavaScript compiler could be used to compile the existing .NET base-class library giving developers a large suite of familiar libraries for use within their code. You could event support C# to jQuery using a fluent Linq-style API!</p>
<p>So how would this work a runtime? When used on the internet to write applications for the browser or mobile browser, a set of C# DOM APIs could be created that compile to the ‘native’ JavaScript equivalent. HTML and CSS3 could of course be included directly within the project as resources.</p>
<p>On the desktop there are a few options, HTML could certainly be used as the presentation technology, but there is of course another option … XAML. Microsoft has clearly indicated that XAML will be part of Windows 8, considering that Silverlight 1.0 was XAML + JavaScript, it is clearly possible to have a XAML UI backed by JavaScript code. Although, it would seem a bit convoluted to compile C# to JavaScript to interact with the UI generated from XAML when C# APIs exist already!</p>
<p>In summary, I feel the current focus on HTML5 whilst ignoring the very real JavaScript issue is going to cause adopters of this technology a lot of pain. Whilst this technology set gives a fantastic boost to interactive websites, using JavaScript for the development of large client-side applications is an expensive option. My hope is that if Microsoft really embrace HTML5 they look beyond simply adding tag support and tooltips and address the real problem … JavaScript.</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/08/can-microsoft-%e2%80%98fix%e2%80%99-javascript-and-make-html5-applications-viable/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Silverlight UK User Group Presentation on Cross Platform XAML</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/07/silverlight-uk-user-group-presentation-on-cross-platform-xaml/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/07/silverlight-uk-user-group-presentation-on-cross-platform-xaml/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 10:48:09 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1567</guid>
		<description><![CDATA[Earlier this week I presented a talk to the Silverlight UK User Group on cross-platform application development with WPF, Silverlight and Windows Phone 7. Thanks to Gergley Orosz for these pictures Thanks to all who attended my talk. Here is a copy of the presentation I gave: You can see the Windows Phone 7 XAML [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwww.scottlogic.co.uk%252Fblog%252Fcolin%252F2011%252F07%252Fsilverlight-uk-user-group-presentation-on-cross-platform-xaml%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Silverlight%20UK%20User%20Group%20Presentation%20on%20Cross%20Platform%20XAML%20%23%22%20%7D);"></div>
<p>Earlier this week I presented a talk to the <a href="http://consultingblogs.emc.com/markmann/archive/2011/06/07/silverlight-uk-user-group-june-2011-agenda.aspx">Silverlight UK User Group</a> on cross-platform application development with WPF, Silverlight and Windows Phone 7.</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/07/sluguk.jpg" alt="" title="sluguk" width="626" height="156" class="aligncenter size-full wp-image-1568" /></p>
<p><small>Thanks to <a href="http://twitter.com/#!/gergelyorosz">Gergley Orosz</a> for these pictures</small></p>
<p>Thanks to all who attended my talk. Here is a copy of the presentation I gave:</p>
<p><object id="__sse8477573" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=slugukpresentation-110701041841-phpapp02&#038;stripped_title=silverlight-uk-user-group-cross-platform-xaml-applications&#038;userName=colineberhardt" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse8477573" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=slugukpresentation-110701041841-phpapp02&#038;stripped_title=silverlight-uk-user-group-cross-platform-xaml-applications&#038;userName=colineberhardt" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object></p>
<p>You can see the Windows Phone 7 XAML Finance application which I demonstrated in the following video:</p>
<p><iframe width="640" height="510" src="http://www.youtube.com/embed/ISWu2VOKIyc" frameborder="0" allowfullscreen></iframe></p>
<p>The Silverlight version is <a href="http://www.scottlogic.co.uk/blog/colin/xaml-finance/">also available online</a>.</p>
<p>Finally, the sourcecode for the simple cross-platform Twitter application I demonstrated is available to download: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/07/CrossPlatformDemo.zip'>CrossPlatformDemo.zip</a></p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/07/silverlight-uk-user-group-presentation-on-cross-platform-xaml/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>White Paper: Flex, Silverlight or HTML5? Time to decide&#8230;</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/05/flex-silverlight-html5-time-to-decide/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/05/flex-silverlight-html5-time-to-decide/#comments</comments>
		<pubDate>Thu, 05 May 2011 21:58:38 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[white paper]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1442</guid>
		<description><![CDATA[When meeting current and prospective clients the subject of web technology choice often arises. There has been a great deal of confusion and uncertainty out there, even before Microsoft&#8217;s perceived change of stance with respect to Silverlight emerged. This white paper is intended to help technology decision makers come to an informed choice when faced [...]]]></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%252Fflex-silverlight-html5-time-to-decide%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22White%20Paper%3A%20Flex%2C%20Silverlight%20or%20HTML5%3F%20Time%20to%20decide...%20%20%23%22%20%7D);"></div>
<p>When meeting current and prospective clients the subject of web technology choice often arises. There has been a great deal of confusion and uncertainty out there, even before Microsoft&#8217;s perceived change of stance with respect to Silverlight emerged.</p>
<p>This white paper is intended to help technology decision makers come to an informed choice when faced with this difficult decision. You can view the paper below, or download it in PDF format: <a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/05/Flex-Silverlight-HTML5.pdf">Flex-Silverlight-HTML5.pdf</a> (1.3 MBytes)</p>
<p><iframe src="http://docs.google.com/gview?url=http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/05/Flex-Silverlight-HTML5.pdf&#038;embedded=true" style="width:700px; height:600px;" frameborder="0"></iframe></p>
<p>Thoughts? comments? leave them below &#8230;</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/05/flex-silverlight-html5-time-to-decide/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Automatically Showing ToolTips on a Trimmed TextBlock (Silverlight + WPF)</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2011/01/automatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2011/01/automatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 16:25:54 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[codeproject]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cross-platform]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=1172</guid>
		<description><![CDATA[Both WPF and Silverlight have a property TextTrimming=”WordEllipsis”, which trims the text that a TextBlock displays based on the available width. This blog post describes a simple method for automatically showing the full text as a tooltip whenever the text is trimmed. This is presented as an attached behaviour and works in both WPF and [...]]]></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%252F01%252Fautomatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Automatically%20Showing%20ToolTips%20on%20a%20Trimmed%20TextBlock%20%28Silverlight%20%2B%20WPF%29%20%23%22%20%7D);"></div>
<p><em>Both WPF and Silverlight have a property TextTrimming=”WordEllipsis”, which trims the text that a TextBlock displays based on the available width. This blog post describes a simple method for automatically showing the full text as a tooltip whenever the text is trimmed. This is presented as an attached behaviour and works in both WPF and Silverlight</em></p>
<p>A few weeks ago I blogged about a Silverlight solution for <a href="http://www.scottlogic.co.uk/blog/colin/2011/01/showing-tooltips-on-trimmed-textblock-silverlight/">automatically adding tooltips when a TextBlock Text is trimmed</a> and renders an ellipsis. I found a decent looking WPF solutions on the web and linked it in my article, however, based on the comments to my previous blog post, it looks like the WPF solution didn&#8217;t work too well, failing to respect font size etc&#8230; In this blog post I have updated my solution to be cross-platform, working on WPF <strong>and</strong> Silverlight.</p>
<p>To briefly recap, my solution for automatically adding tooltips to trimmed text relies on the slightly odd behaviour of the <code>TextBlock</code> where its <code>ActualWidth</code> is reported as the width of the text without trimming:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/01/TextTrimming.png" alt="" title="TextTrimming" width="568" height="257" class="aligncenter size-full wp-image-1076" /></p>
<p>In order to determine whether to show a tooltip, all you have to do is compare the <code>ActualWidth</code> of the <code>TextBlock</code> to the <code>ActualWidth</code> of its parent.</p>
<p>However, the WPF <code>TextBlock</code> does not have this same quirky behaviour, so a completely different approach is required. The other solutions I have seen involve using the low-level WPF drawing APIs to compute the size of the rendered text, however, there is a simpler solution to this problem &#8230;</p>
<p><strong>UPDATE:</strong> I originally wrote about a method of finding the overall text width from various internal fields within the <code>TextBlock</code> via reflection, as shown below. However, a kind reader of my blog, <strong>Daniel Fihnn</strong>, pointed out that there is a simpler solution that does not require any reflection.</p>
<p><a href="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/01/TextBlockInternal.png"><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/01/TextBlockInternal-300x128.png" alt="" title="TextBlockInternal" width="300" height="128" class="aligncenter size-medium wp-image-1176" /></a></p>
<p>Daniel pointed out that the width of the TextBlock without the trimming can be obtained using the <code>Measure</code> method. This method is typically used by panels during layout, the <code>Measure</code> method is invoked with the size made available to the element, calling this method causes the element to update its <code>DesiredSize</code> property. Therefore, if you invoke <code>Measure</code> on a <code>TextBlock</code> which has trimming enabled, giving it an infinite available space, its <code>DesiredSize</code> property will report the width that the text would occupy without trimming.</p>
<p>The <code>ComputeAutoTooltip</code> method of the attached behaviour I described in my previous post is updated to have a completely different WPF implementation:</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> TextBlockUtils
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
  <span style="color: #008080; font-style: italic;">/// Gets the value of the AutoTooltipProperty dependency property</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> GetAutoTooltip<span style="color: #008000;">&#40;</span>DependencyObject obj<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>obj<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>AutoTooltipProperty<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;">/// Sets the value of the AutoTooltipProperty dependency property</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetAutoTooltip<span style="color: #008000;">&#40;</span>DependencyObject obj, <span style="color: #6666cc; font-weight: bold;">bool</span> value<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    obj<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>AutoTooltipProperty, value<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;">/// Identified the attached AutoTooltip property. When true, this will set the TextBlock TextTrimming</span>
  <span style="color: #008080; font-style: italic;">/// property to WordEllipsis, and display a tooltip with the full text whenever the text is trimmed.</span>
  <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty AutoTooltipProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterAttached</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;AutoTooltip&quot;</span>,
          <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>TextBlockUtils<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> PropertyMetadata<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span>, OnAutoTooltipPropertyChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnAutoTooltipPropertyChanged<span style="color: #008000;">&#40;</span>DependencyObject d, DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    TextBlock textBlock <span style="color: #008000;">=</span> d <span style="color: #0600FF; font-weight: bold;">as</span> TextBlock<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>textBlock <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: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">NewValue</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">TextTrimming</span> <span style="color: #008000;">=</span> TextTrimming<span style="color: #008000;">.</span><span style="color: #0000FF;">WordEllipsis</span><span style="color: #008000;">;</span>
      ComputeAutoTooltip<span style="color: #008000;">&#40;</span>textBlock<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">SizeChanged</span> <span style="color: #008000;">+=</span> TextBlock_SizeChanged<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>
      textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">SizeChanged</span> <span style="color: #008000;">-=</span> TextBlock_SizeChanged<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: #6666cc; font-weight: bold;">void</span> TextBlock_SizeChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, SizeChangedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    TextBlock textBlock <span style="color: #008000;">=</span> sender <span style="color: #0600FF; font-weight: bold;">as</span> TextBlock<span style="color: #008000;">;</span>
    ComputeAutoTooltip<span style="color: #008000;">&#40;</span>textBlock<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;">/// Assigns the ToolTip for the given TextBlock based on whether the text is trimmed</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> ComputeAutoTooltip<span style="color: #008000;">&#40;</span>TextBlock textBlock<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
<span style="color: #008080;">#if WPF</span>
    textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">Measure</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Size<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Double</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PositiveInfinity</span>, <span style="color: #6666cc; font-weight: bold;">Double</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PositiveInfinity</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var width <span style="color: #008000;">=</span> textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">DesiredSize</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualWidth</span> <span style="color: #008000;">&lt;</span> width<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      ToolTipService<span style="color: #008000;">.</span><span style="color: #0000FF;">SetToolTip</span><span style="color: #008000;">&#40;</span>textBlock, textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</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>
      ToolTipService<span style="color: #008000;">.</span><span style="color: #0000FF;">SetToolTip</span><span style="color: #008000;">&#40;</span>textBlock, <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>
&nbsp;
<span style="color: #008080;">#else</span>
    FrameworkElement parentElement <span style="color: #008000;">=</span> VisualTreeHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">GetParent</span><span style="color: #008000;">&#40;</span>textBlock<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> FrameworkElement<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>parentElement <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;">if</span> <span style="color: #008000;">&#40;</span>textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualWidth</span> <span style="color: #008000;">&gt;</span> parentElement<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualWidth</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        ToolTipService<span style="color: #008000;">.</span><span style="color: #0000FF;">SetToolTip</span><span style="color: #008000;">&#40;</span>textBlock, textBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</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>
        ToolTipService<span style="color: #008000;">.</span><span style="color: #0000FF;">SetToolTip</span><span style="color: #008000;">&#40;</span>textBlock, <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>
<span style="color: #008080;">#endif </span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This code now works in both a WPF and a Silverlight context. Here&#8217;s a screenshot of it working in WPF:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/01/AutoTooltip.png" alt="" title="AutoTooltip" width="601" height="153" class="aligncenter size-full wp-image-1179" /></p>
<p>You can download the full sourcecode, including the WPF and Silverlight demo: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2011/01/AutoTooltipTextBlock2.zip'>AutoTooltipTextBlock.zip</a></p>
<p>Thanks to Daniel Fihnn for a much improved WPF version of this code!</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2011/01/automatically-showing-tooltips-on-a-trimmed-textblock-silverlight-wpf/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Does HTML5 mean the end is in sight for Silverlight?</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/11/does-html5-mean-the-end-is-in-sight-for-silverlight/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2010/11/does-html5-mean-the-end-is-in-sight-for-silverlight/#comments</comments>
		<pubDate>Tue, 02 Nov 2010 21:52:07 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=899</guid>
		<description><![CDATA[This blog post looks at the fallout after last week’s PDC conference where Microsoft were quoted as saying “our strategy on Silverlight has shifted”, and the resulting fallout in the developer community. In this post I will describe why I think Silverlight has a future ahead of it and exactly where that future lies. Bob [...]]]></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%252F2010%252F11%252Fdoes-html5-mean-the-end-is-in-sight-for-silverlight%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Does%20HTML5%20mean%20the%20end%20is%20in%20sight%20for%20Silverlight%3F%20%23%22%20%7D);"></div>
<p><em>This blog post looks at the fallout after last week’s PDC conference where Microsoft were quoted as saying “our strategy on Silverlight has shifted”, and the resulting fallout in the developer community. In this post I will describe why I think Silverlight has a future ahead of it and exactly where that future lies.</em></p>
<h3>Bob Puts His Foot In It!</h3>
<p>Last week’s Microsoft Professional Developer Conference (PDC) has caused quite a stir. It was clear from Steve Ballmer’s keynote speech that HTML5 was taking centre stage, with Silverlight not getting a single mention in his opening speech.</p>
<p>The image below shows a word cloud showing the most frequent words in <a href="http://www.microsoft.com/presspass/exec/steve/2010/10-28PDC10.mspx">Steve Ballmer&#8217;s PDC keynote speech</a>, rendered using <a href="http://www.wordle.net">Wordle</a></p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/11/wordCloud.png" alt="" title="wordCloud" width="546" height="260" class="aligncenter size-full wp-image-904" /></p>
<p>This is in contrast to previous events such as MIX9 where (just over a year ago) Silverlight 3 took centre stage. This focus on HTML5, which many see as a competing technology to Silverlight was enough to get the community a little worried, however, it was the interview that <a href="http://www.zdnet.com/blog/microsoft/microsoft-our-strategy-with-silverlight-has-shifted/7834">Bob Muglia gave to ZDNet</a> that really caused a storm. </p>
<p>The key quotes are given below:</p>
<p><i>“[...] when it comes to touting Silverlight as Microsoft’s vehicle for delivering a cross-platform runtime, our strategy has shifted.”</i></p>
<p><i>“But HTML is the only true cross platform solution for everything, including (Apple’s) iOS platform”</i></p>
<p>The above statements caused quite a stir, with calls for Bob to clarify his statements which seemed to contradict the recent <a href="http://team.silverlight.net/announcement/the-future-of-silverlight/">“Future of Silverlight”</a> blog post which contrasted the difference between standardisation (HTML5) and innovation (Silverlight) to show a very positive future for the technology. I think this is the first time I had seen such a direct comparison of HTML5 vs. Silverlight from Microsoft’s Silverlight team, and the arguments they made were quite compelling. </p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/11/silverlightFuture.png" alt="" title="silverlightFuture" width="522" height="188" class="aligncenter size-full wp-image-901" /></p>
<h3>Bob Pulls His Foot Out Again</h3>
<p>As a result of Bob’s interview, which was commented upon by virtually everyone in the Silverlight community (<a href="http://csharperimage.jeremylikness.com/2010/10/so-whats-fuss-about-silverlight.html">Jeremy Likeness</a>, <a href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/11/01/silverlight-versus-html5-really.aspx">Mike Taulty</a>, and many many more), Bob quickly <a href="http://team.silverlight.net/announcement/pdc-and-silverlight/">followed up with a blog post</a>  where he stated the following:</p>
<p><i>“I said, ‘Our Silverlight strategy and focus going forward has shifted.’  This isn’t a negative statement, but rather, it’s a comment on how the industry has changed and how we’re adapting our Silverlight strategy to take advantage of that”</i></p>
<p>However, I think that the most important point he made came at the very end of his post:</p>
<p><i>“The purpose of Silverlight has never been to replace HTML, but rather to do the things that HTML (and other technologies) can’t, and to do so in a way that’s easy for developers to use.”</i></p>
<p>I think it is people’s misunderstanding of the differences between Silverlight and HTML that caused most of this uproar in the first place. It is this point which I would like to concentrate on, and hopefully provide a bit more clarity.</p>
<h3>PDC is an Event</h3>
<p>Before I focus on the differences between HTML and Silverlight I think it is worth noting that PDC is an event, and just like any other event it needs to be marketed in order to fuel people’s interest and get them through the door. Microsoft embraces a very wide range of technologies, and if the PDC tried to be a true reflection of this diversity it would be very fragmented and hard to market. It is much better that the event focuses on something new and interesting to provide an underlying theme. This year that theme was HTML5.</p>
<p>Interestingly after MIX9 where Silverlight was the main focus and <a href="http://www.scottlogic.co.uk/blog/colin/2009/03/silverlight-3-is-here-but-is-it-a-flash-killer-yet/">out-of-browser support was announced</a>, there was noticeable unrest amongst the WPF community, they felt short-changed (4 MIX sessions on WPF to Silverlight’s 31) and there was speculation that Silverlight would replace WPF entirely. However, when the knee-jerk reactions were forgotten about and a more sensible analysis of the difference between the two was considered (see <a href="http://10rem.net/blog/2009/12/01/the-future-of-client-app-dev--wpf-and-silverlight-convergence">Pete Brown’s Future of Client App Development blog post</a> for example), it was clear that WPF does have a future. </p>
<h3>The Web Landscape</h3>
<p>Back to HTML and Silverlight, if we look at the current landscape of the internet we can crudely characterise web sites and web based applications based on the degree of interactivity that provide:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/11/slidingScale.png" alt="" title="slidingScale" width="400" height="163" class="aligncenter size-full wp-image-903" /></p>
<p>In the above diagram we see a sliding scale of interactivity and complexity with a few well known sites positioned along this scale (don’t ask me about what units I am using to measure each site!).</p>
<p>With the above sliding scale we can position the popular internet technologies based on their relative strengths and the types of site they are most suited to:</p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/11/slidingScale2.png" alt="" title="slidingScale2" width="400" height="129" class="aligncenter size-full wp-image-902" /></p>
<p>I have not tried to pin each technology to a specific location on the scale because in my opinion the boundaries between each are blurred &#8230; a lot. It is possible to use any of the above technologies to create a web site / web application at either end of the spectrum, however, the further away you move from the ‘comfort zone’ of each technology, the harder the job becomes.</p>
<p>We’ll start at the Flash / Silverlight end of the spectrum. Why not capitalise on the fact that these plugin technologies provide a controlled environment for your code to run in, providing great performance and virtually zero browser compatibility issues?</p>
<p>Whilst this point may not have been addressed in great detail for Silverlight, there are certainly numerous strong arguments presented on the internet which describe quite clearly why full-page Flash sites will never fully replace HTML (For a thorough analysis take a look at Emil Stenström’s blog post on <a href="http://friendlybit.com/js/flash-only-vs-ajax-sites/">Flash vs. Ajax sites</a>). The main reasons cited are:</p>
<ul>
<li>Splash screens – Flash / Silverlight sites need to load the entire ‘executable’ before they can render. Contrast this with how quickly the pages from Wikipedia appear.</li>
<li>Maintenance – Quick fixes are often required for websites, with HTML / JavaScript this can often be left to the support team. For Silverlight / Flash this requires a full rebuild of the application.</li>
</ul>
<p>(There are a number of other points, including search engine indexing, etc &#8230;)</p>
<p>There is not a strong case for replacing simpler web sites with Silverlight. I think my fellow WPF Disciple <a href="http://justinangel.net/">Justin Angel’s blog</a>, which is powered entirely with Silverlight is unfortunately a good illustration of why Silverlight should not be used to replace HTML for the presentation of largely static content. Sitting here on a train using the pitiful WiFi connection I can see Justin’s stylish loading ‘splash screen’, however, after I minute of waiting I grow tired of staring at his beard and sunglasses and little else! </p>
<p><img src="http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/11/loading.png" alt="" title="loading" width="536" height="316" class="aligncenter size-full wp-image-900" /></p>
<p>On the flip-side, when HTML and JavaScript technologies are used to create complex applications, development becomes costly and complex. Cross-browser problems, lack of support for business concepts such as controls and validation, performance issues all start to have a significant impact on the development. To combat this more and more sophisticated solutions are being applied, from the numerous libraries that abstract the differences in browser DOM APIs (jQuery etc&#8230;) to <a href="http://en.wikipedia.org/wiki/Google_Web_Toolkit">Google Web Toolkit</a>, which generates JavaScript code from Java (which provides strong typing and better structuring).</p>
<p>Interestingly in the book <a href="http://oreilly.com/catalog/0636920001416/">Google Closure the Definitive Guide</a>, the preface describes how Google Closure (a JavaScript compiler and mechanism for checking types) was developed to combat the problems the Google Mail development team were experiencing in developing a complex JavaScript application composed of tens of thousands of lines of JavaScript. The take-home message of this is that for complex applications you need more than the JavaScript language alone provides.</p>
<h3>Bob’s Comments in Perspective</h3>
<p>In the previous section I described how the web landscape can be illustrated as a sliding scale of complexity, with HTML and Silverlight at opposite ends of the spectrum and HTML5 sitting somewhere in-between. With this in mind, Silverlight developers should not fear that HTML5 will replace Silverlight. Whilst it is true that there is some common ground between the two, i.e. a certain interactivity / web-site complexity where the two technologies are equally suited, Silverlight users should feel comfortable that for the more complex problems, Silverlight has a clear advantage.</p>
<p>To repeat Bob’s closing statement of his follow-up blog post:</p>
<p><i>“The purpose of Silverlight has never been to replace HTML, but rather to do the things that HTML (and other technologies) can’t, and to do so in a way that’s easy for developers to use.”</i></p>
<p>I think it is the failure of the developer community to realise that there is little direct competition between HTML5 and Silverlight and failure of Microsoft’s marketing to highlight this.</p>
<h3>The Web Increases its Reach.</h3>
<p>However, there are some important points to note in Bob’s comments and his reference to iOS (i.e. iPhone, iPad etc&#8230;). Whilst I do not think HTML5 is a significant threat to Silverlight for web based application development, the introduction of a more diverse range of mobile devices potentially increase the market for HTML.</p>
<p>Most people who use an Android phone or an iPad do not use them as a replacement for their laptop or desktop PC. They use them on the train, in the coffee shop and at home on the sofa as a way to stay online and connected for more of their waking hours. HTML5 is a suitable technology for delivering a interactivity to a diverse range of devices and it looks like the overall market size is increasing.</p>
<p>However, if Microsoft can produce Silverlight plugins for these devices, on the Android phone for example, there is no reason why Silverlight cannot be part of this growing market, delivering more than just interactivity to the mobile platform.</p>
<p>Unfortunately, more recent news, like today’s announcement by the Bing Maps team that seems to indicates they will be <a href="http://www.bing.com/community/site_blogs/b/maps/archive/2010/11/02/changes-to-bird-s-eye-and-3d-maps.aspx">dropping their Silverlight based 3D libraries in favour of HTML5</a> is going to fuel the fire which Bob and Steve Ballmer are currently trying to dowse.</p>
<p>Regards, Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2010/11/does-html5-mean-the-end-is-in-sight-for-silverlight/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>White Paper: Silverlight, WPF and Windows Phone 7 cross platform development</title>
		<link>http://www.scottlogic.co.uk/blog/colin/2010/10/white-paper-silverlight-wpf-and-windows-phone-7-cross-platform-development/</link>
		<comments>http://www.scottlogic.co.uk/blog/colin/2010/10/white-paper-silverlight-wpf-and-windows-phone-7-cross-platform-development/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 09:10:25 +0000</pubDate>
		<dc:creator>Colin Eberhardt</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cross-platform]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.scottlogic.co.uk/blog/colin/?p=829</guid>
		<description><![CDATA[Last week I gave a presentation at a joint Scott Logic / Microsoft event about how WPF and Silverlight are unifying the development platform for desktop, web and mobile. To accompany the talk I wrote a white paper which delves into this subject in a little more detail. You can download the whitepaper here: WPF [...]]]></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%252F2010%252F10%252Fwhite-paper-silverlight-wpf-and-windows-phone-7-cross-platform-development%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22White%20Paper%3A%20Silverlight%2C%20WPF%20and%20Windows%20Phone%207%20cross%20platform%20development%20%23%22%20%7D);"></div>
<p>Last week <a href="http://www.scottlogic.co.uk/2010/10/unifying-development-across-the-desktop-web-and-mobile-scott-logic-points-the-way/">I gave a presentation at a joint Scott Logic / Microsoft event</a> about how WPF and Silverlight are unifying the development platform for desktop, web and mobile. To accompany the talk I wrote a white paper which delves into this subject in a little more detail.</p>
<p>You can download the whitepaper here: <a href='http://www.scottlogic.co.uk/blog/colin/wp-content/uploads/2010/10/WPF-and-Silverlight-Unification.pdf'>WPF and Silverlight Cross Platform Development White Paper</a> (PDF, 1.8 MBytes)</p>
<p>Or view it online in the PDF viewer below: </p>
<p><iframe src="http://docs.google.com/viewer?url=http%3A%2F%2Fwww.scottlogic.co.uk%2Fblog%2Fcolin%2Fwp-content%2Fuploads%2F2010%2F10%2FWPF-and-Silverlight-Unification.pdf&#038;embedded=true" width="600" height="780" style="border: none;"></iframe></p>
<p>I also created a cross platform application that has a 75% shared codebase between the WPF (desktop), Silverlight (web) and Windows Phone 7 (mobile) versions. I plan to share more of this at a later date.</p>
<p>Any thoughts, ideas or comments, please share them below.</p>
<p>Regards,<br />
Colin E.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.scottlogic.co.uk/blog/colin/2010/10/white-paper-silverlight-wpf-and-windows-phone-7-cross-platform-development/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

