<?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>Jason Mitchell &#187; Silverlight</title>
	<atom:link href="http://jason-mitchell.com/index.php/category/programming/silverlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://jason-mitchell.com</link>
	<description></description>
	<lastBuildDate>Thu, 26 Jan 2012 22:31:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using the Bing Maps Silverlight Control</title>
		<link>http://jason-mitchell.com/silverlight/using-the-bing-maps-silverlight-control/</link>
		<comments>http://jason-mitchell.com/silverlight/using-the-bing-maps-silverlight-control/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 21:49:21 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.jason-mitchell.com/?p=260</guid>
		<description><![CDATA[This week I played around with the Bing Maps Silverlight Control to find out how easy it would be to get a map up and running in an application, place pins on said map and get the name of the country that the user has clicked on.  I soon discovered that achieving this functionality was [...]]]></description>
			<content:encoded><![CDATA[<p>This week I played around with the Bing Maps <a href="http://www.silverlight.net/" target="_blank">Silverlight</a> Control to find out how easy it would be to get a map up and running in an application, place pins on said map and get the name of the country that the user has clicked on.  I soon discovered that achieving this functionality was incredibly easy!</p>
<p>The simple application I created can be found at <a href="http://jason-mitchell.com/uploads/bingmaps/bingmapstestpage.html">http://jason-mitchell.com/uploads/bingmaps/bingmapstestpage.html</a></p>
<p><span id="more-260"></span></p>
<p>Naturally the first thing that needed to be done was to download and install the Bing Maps Silverlight Control from <a href="http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&amp;FamilyID=beb29d27-6f0c-494f-b028-1e0e3187e830" target="_blank">here</a>.  I also needed to create a Bing Maps Key; see how to do this on <a href="http://msdn.microsoft.com/en-us/library/ff428642.aspx" target="_blank">MSDN</a>.  During the brief time I spent searching for information on this topic I also came across an “Interactive SDK” provided by Microsoft.  This turned out to be very useful in figuring out how to work the map control and you can find it at <a href="http://www.microsoft.com/maps/isdk/silverlight/">http://www.microsoft.com/maps/isdk/silverlight/</a>.</p>
<p>Once this was done I created a new Silverlight application project in Visual Studio 2010 and added the reference to Microsoft.Maps.MapControl.dll that I had just installed.  In my MainPage.xaml markup I added a new namespace inside the UserControl tag by adding:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">xmlns<span style="color: #008000;">:</span>bing<span style="color: #008000;">=</span><span style="color: #666666;">&quot;clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl</span></pre></div></div>

<p>I’m not going to provide a full code listing in this article so if you aren’t sure on how to do this then check out the zip file at the end of this article.</p>
<p>My next step was to add the markup to display the map control in my LayoutRoot.  I was pleasantly surprised when this was just a simple one line tag as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>bing<span style="color: #008000;">:</span>Map x<span style="color: #008000;">:</span>Name<span style="color: #008000;">=</span><span style="color: #666666;">&quot;MapControl&quot;</span> CredentialsProvider<span style="color: #008000;">=</span><span style="color: #666666;">&quot;your Bing maps key&quot;</span> <span style="color: #008000;">/&gt;</span></pre></div></div>

<p>In order to create the functionality in my demo application I added a handler for the Map’s MouseClick event.  This handler will call one of two methods based on what radio button has been checked.  Again I’m not putting the code in the article but it is in the download.</p>
<p>In order to place a push pin it took <strong>three lines of code</strong> without mushing it all together and not counting the method signature.  See it below:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> PlacePinOnMap<span style="color: #008000;">&#40;</span>MapMouseEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Location worldCoords <span style="color: #008000;">=</span> MapControl<span style="color: #008000;">.</span><span style="color: #0000FF;">ViewportPointToLocation</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">ViewportPoint</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Pushpin pushpin <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Pushpin <span style="color: #008000;">&#123;</span>Location <span style="color: #008000;">=</span> worldCoords<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    MapControl<span style="color: #008000;">.</span><span style="color: #0000FF;">Children</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>pushpin<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>There’s nothing complicated here.  MapMouseEventArgs.ViewportPoint returns the click location on the screen which can then be passed to the MapControl.ViewportPointToLocation method which will convert the click location to a <strong>geocode </strong>(longitude and latitude) representing the location on the map.   Next I create a new Pushpin control, give it the geocode and add it to the map.  Pretty easy!</p>
<p>In order to get the location information such as street and country we have to <strong>use Microsoft’s Geocode web service</strong>.  The address for this is :</p>
<p><a title="http://dev.virtualearth.net/webservices/v1/GeocodeService/GeocodeService.svc" href="http://dev.virtualearth.net/webservices/v1/GeocodeService/GeocodeService.svc">http://dev.virtualearth.net/webservices/v1/GeocodeService/GeocodeService.svc</a></p>
<p>I added a new Service Reference to my Silverlight application which pointed to this service.  This will generate a proxy class called <strong>GeocodeServiceClient</strong> which provides a <strong>ReverseGeocodeAsync method</strong> which will convert a geocode into a real address.  The proxy class also provides a GeocodeAsync method which presumably converts an address to a geocode (I haven’t tried it).  The code below shows how to get the name of the country that has been clicked on using this web service.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> GetCountryFromClickLocation<span style="color: #008000;">&#40;</span>MapMouseEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Location worldCoords <span style="color: #008000;">=</span> MapControl<span style="color: #008000;">.</span><span style="color: #0000FF;">ViewportPointToLocation</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">ViewportPoint</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    GeocodeServiceClient geocodeClient <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> GeocodeServiceClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;BasicHttpBinding_IGeocodeService&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    geocodeClient<span style="color: #008000;">.</span><span style="color: #0000FF;">ReverseGeocodeCompleted</span> <span style="color: #008000;">+=</span> ReverseGeocodeCompleted<span style="color: #008000;">;</span>
&nbsp;
    ReverseGeocodeRequest request <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ReverseGeocodeRequest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    request<span style="color: #008000;">.</span><span style="color: #0000FF;">Culture</span> <span style="color: #008000;">=</span> MapControl<span style="color: #008000;">.</span><span style="color: #0000FF;">Culture</span><span style="color: #008000;">;</span>
    request<span style="color: #008000;">.</span><span style="color: #0000FF;">Location</span> <span style="color: #008000;">=</span> worldCoords<span style="color: #008000;">;</span>
    request<span style="color: #008000;">.</span><span style="color: #0000FF;">ExecutionOptions</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ExecutionOptions<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    request<span style="color: #008000;">.</span><span style="color: #0000FF;">ExecutionOptions</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SuppressFaults</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
&nbsp;
    MapControl<span style="color: #008000;">.</span><span style="color: #0000FF;">CredentialsProvider</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetCredentials</span><span style="color: #008000;">&#40;</span>credentials <span style="color: #008000;">=&gt;</span>
               <span style="color: #008000;">&#123;</span>
                  request<span style="color: #008000;">.</span><span style="color: #0000FF;">Credentials</span> <span style="color: #008000;">=</span> credentials<span style="color: #008000;">;</span>
                  geocodeClient<span style="color: #008000;">.</span><span style="color: #0000FF;">ReverseGeocodeAsync</span><span style="color: #008000;">&#40;</span>request<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
               <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> ReverseGeocodeCompleted<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, ReverseGeocodeCompletedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    ResultTextBlock<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Country: &quot;</span> <span style="color: #008000;">+</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Results</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Address</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CountryRegion</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Again we have to convert our click location into a geocode to give to the web service.  This code is pretty straightforward so I’m not going to go through it.</p>
<p>That’s basically it!  Overall I’m very impressed with Microsoft’s Bing Maps support in Silverlight both in terms of the SDK and the documentation available online.  The demo application took no time at all to whip up after a few minutes spent researching.</p>
<p><strong><em>Demo Project Download:</em></strong> <a href="http://www.jason-mitchell.com/Uploads/BingMapsDemo-12_03_11.zip" target="_blank">http://www.jason-mitchell.com/Uploads/BingMapsDemo-12_03_11.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/silverlight/using-the-bing-maps-silverlight-control/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Working with Isolated Storage in Silverlight</title>
		<link>http://jason-mitchell.com/silverlight/working-with-isolated-storage-in-silverlight/</link>
		<comments>http://jason-mitchell.com/silverlight/working-with-isolated-storage-in-silverlight/#comments</comments>
		<pubDate>Sat, 13 Nov 2010 15:59:35 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WP7]]></category>
		<category><![CDATA[Isolated Storage]]></category>

		<guid isPermaLink="false">http://www.jason-mitchell.com/index.php/2010/11/13/working-with-isolated-storage-in-silverlight/</guid>
		<description><![CDATA[When using Silverlight, developers have no direct access to the file system on a user’s computer.  However Silverlight does use isolated storage as a virtual file system to store data on machines providing the application has the correct file permissions to do so.  Additionally, Windows Phone 7 uses isolated storage for saving data to the [...]]]></description>
			<content:encoded><![CDATA[<p>When using <a href="http://www.silverlight.net/" target="_blank">Silverlight</a>, developers have no direct access to the file system on a user’s computer.  However Silverlight does use <a href="http://www.silverlight.net/learn/quickstarts/isolatedstorage/http://www.silverlight.net/learn/quickstarts/isolatedstorage/" target="_blank">isolated storage</a> as a virtual file system to store data on machines providing the application has the correct file permissions to do so.  Additionally, Windows Phone 7 uses isolated storage for saving data to the phone which prevents applications interfering with each others data.</p>
<p>Since the Windows Phone 7 developer tools were released, I have playing around with developing little apps for the phone to get to grips with the basics of app development for the platform.  When I got around to playing with isolated storage on the phone, I quickly got tired of writing the same code over and over again (I like to try and keep my code <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" target="_blank">DRY</a>).  For this reason, I decided to create a short helper class which uses delegates to help reduce the amount of repeated code I was writing.</p>
<p><span id="more-211"></span></p>
<p><strong>IsolatedStorageHelper</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> IsolatedStorageHelper
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">object</span> FileOperation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> fileName, FileMode fileMode, FileAccess fileAccess, Func<span style="color: #008000;">&lt;</span>Stream, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span> fileOperation<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IsolatedStorageFile userStore <span style="color: #008000;">=</span> IsolatedStorageFile<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUserStoreForApplication</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IsolatedStorageFileStream stream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IsolatedStorageFileStream<span style="color: #008000;">&#40;</span>fileName, fileMode, fileAccess, userStore<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> fileOperation<span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> FileOperation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> fileName, FileMode fileMode, FileAccess fileAccess, Action<span style="color: #008000;">&lt;</span>Stream<span style="color: #008000;">&gt;</span> fileOperation<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        FileOperation<span style="color: #008000;">&#40;</span>fileName, fileMode, fileAccess, stream <span style="color: #008000;">=&gt;</span>
                                                            <span style="color: #008000;">&#123;</span>
                                                                fileOperation<span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                                                                <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</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>Using this class is pretty simple.  I use it frequently in my test Windows Phone 7 applications to load and save XML.  Here’s an example:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">xDoc <span style="color: #008000;">=</span> IsolatedStorageHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">FileOperation</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;myFile.xml&quot;</span>, FileMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span>, FileAccess<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span>, stream <span style="color: #008000;">=&gt;</span> XDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> XDocument<span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/silverlight/working-with-isolated-storage-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Toggle Visual States with a Custom Action in Silverlight</title>
		<link>http://jason-mitchell.com/silverlight/toggle-visual-states-with-a-custom-action-in-silverlight/</link>
		<comments>http://jason-mitchell.com/silverlight/toggle-visual-states-with-a-custom-action-in-silverlight/#comments</comments>
		<pubDate>Wed, 19 May 2010 21:12:07 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.jason-mitchell.com/index.php/2010/05/19/toggle-visual-states-with-a-custom-action-in-silverlight/</guid>
		<description><![CDATA[I was recently working on a project in Silverlight and required the ability to toggle between two states in a UserControl.  When I started working with Silverlight, I would have handled click or mouse events to change the visual state but I soon moved on to work with behaviours and actions so I could remove [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently working on a project in <a href="http://www.silverlight.net" target="_blank">Silverlight</a> and required the ability to toggle between two states in a UserControl.  When I started working with Silverlight, I would have handled click or mouse events to change the visual state but I soon moved on to work with behaviours and actions so I could remove such code from my code-behind files.  Since an to toggle between two visual states did not exist, this seemed like an ideal opportunity to make my first custom action.</p>
<p><span id="more-155"></span></p>
<p>This is just a quick article and since there is nothing too complex here I have just posted the code here:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Controls</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Interactivity</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> IssueTracker<span style="color: #008000;">.</span><span style="color: #0000FF;">Actions</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#91;</span>DefaultTrigger<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>FrameworkElement<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #000000;">System</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Interactivity</span><span style="color: #008000;">.</span><span style="color: #0000FF;">EventTrigger</span><span style="color: #008000;">&#41;</span>,
                    <span style="color: #666666;">&quot;MouseLeftButtonUp&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ToggleStateAction <span style="color: #008000;">:</span> TargetedTriggerAction
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty OffStateProperty<span style="color: #008000;">;</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 OnStateProperty<span style="color: #008000;">;</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 UseTransitionsProperty<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> isOnStateActive<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">static</span> ToggleStateAction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            OffStateProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;OffState&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>, 
                                      <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>ToggleStateAction<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            OnStateProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;OnState&quot;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>, 
                                       <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>ToggleStateAction<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            UseTransitionsProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;UseTransitions&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>ToggleStateAction<span style="color: #008000;">&#41;</span>, <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: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> Invoke<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> o<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Control control <span style="color: #008000;">=</span> FindTargetElement<span style="color: #008000;">&#40;</span>Target<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> Control<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>isOnStateActive<span style="color: #008000;">&#41;</span>
                VisualStateManager<span style="color: #008000;">.</span><span style="color: #0000FF;">GoToState</span><span style="color: #008000;">&#40;</span>control, OffState, UseTransitions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">else</span>
                VisualStateManager<span style="color: #008000;">.</span><span style="color: #0000FF;">GoToState</span><span style="color: #008000;">&#40;</span>control, OnState, UseTransitions<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            isOnStateActive <span style="color: #008000;">=</span> <span style="color: #008000;">!</span>isOnStateActive<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> FrameworkElement FindTargetElement<span style="color: #008000;">&#40;</span>FrameworkElement element<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            FrameworkElement parent <span style="color: #008000;">=</span> element<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>parent <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>
                IList vsgs <span style="color: #008000;">=</span> VisualStateManager<span style="color: #008000;">.</span><span style="color: #0000FF;">GetVisualStateGroups</span><span style="color: #008000;">&#40;</span>parent<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>vsgs <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;&amp;</span>amp<span style="color: #008000;">;</span> vsgs<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    Control control <span style="color: #008000;">=</span> parent<span style="color: #008000;">.</span><span style="color: #0000FF;">Parent</span> <span style="color: #0600FF; font-weight: bold;">as</span> Control<span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>control <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> control<span style="color: #008000;">;</span>
                    <span style="color: #0600FF; font-weight: bold;">return</span> parent<span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                parent <span style="color: #008000;">=</span> parent<span style="color: #008000;">.</span><span style="color: #0000FF;">Parent</span> <span style="color: #0600FF; font-weight: bold;">as</span> FrameworkElement<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> OffState
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>GetValue<span style="color: #008000;">&#40;</span>OffStateProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> SetValue<span style="color: #008000;">&#40;</span>OffStateProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> OnState
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>GetValue<span style="color: #008000;">&#40;</span>OnStateProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> SetValue<span style="color: #008000;">&#40;</span>OnStateProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> UseTransitions
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span>GetValue<span style="color: #008000;">&#40;</span>UseTransitionsProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
            set <span style="color: #008000;">&#123;</span> SetValue<span style="color: #008000;">&#40;</span>UseTransitionsProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>One of the really nice things when using custom actions is that if you are using Expression Blend, the designer will pick up on your properties and provide you with the ability to edit them without needing to touch the XAML.  It&#8217;s worth pointing out that I had to add the method &#8220;FindTargetElement&#8221; to return the control to use in the transition.  In fact, I nabbed this code right out of the source for the GoToState which can be seen at <a title="http://expressionblend.codeplex.com" href="http://expressionblend.codeplex.com">http://expressionblend.codeplex.com</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/silverlight/toggle-visual-states-with-a-custom-action-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MVVM for Silverlight (Windows Phone 7 App)</title>
		<link>http://jason-mitchell.com/silverlight/mvvm-for-silverlight-windows-phone-7-app/</link>
		<comments>http://jason-mitchell.com/silverlight/mvvm-for-silverlight-windows-phone-7-app/#comments</comments>
		<pubDate>Sun, 16 May 2010 18:26:49 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WP7]]></category>
		<category><![CDATA[mvvm]]></category>

		<guid isPermaLink="false">http://www.jason-mitchell.com/index.php/2010/05/16/mvvm-for-silverlight-windows-phone-7-app/</guid>
		<description><![CDATA[For a little while now I&#8217;ve had a bit of interest in the Model View ViewModel (MVVM) pattern for Silverlight and WPF so I thought I would finally give it a go and see if I could get an understanding of how to use this pattern. I had no prior knowledge of the details of [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">For a little while now I&#8217;ve had a bit of interest in the Model View ViewModel (<a href="http://en.wikipedia.org/wiki/MVVM" target="_blank">MVVM</a>) pattern for <a href="http://www.silverlight.net" target="_blank">Silverlight</a> and <a href="http://msdn.microsoft.com/en-us/library/ms754130.aspx" target="_blank">WPF</a> so I thought I would finally give it a go and see if I could get an understanding of how to use this pattern.</p>
<p><span id="more-136"></span><a href="http://www.jason-mitchell.com/images/blog/MVVMforSilverlightWindowsPhone7App_E533/image.png"><img style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 7px; display: inline; border-style: initial; border-color: initial; border-image: initial; border-width: 0px;" title="Twitter timeline application" src="http://www.jason-mitchell.com/images/blog/MVVMforSilverlightWindowsPhone7App_E533/image_thumb.png" alt="Twitter timeline application" width="191" height="377" align="right" border="0" /></a></p>
<p style="text-align: justify;">I had no prior knowledge of the details of MVVM so I needed to do a bit of research.  Thankfully, there seems to be no shortage of information on the web about it.  I found Wikipedia to have a useful article about it at <a title="http://en.wikipedia.org/wiki/MVVM" href="http://en.wikipedia.org/wiki/MVVM">http://en.wikipedia.org/wiki/MVVM</a> (especially the &#8220;Pattern Description&#8221; section) and also an article on <a href="http://msdn.microsoft.com/" target="_blank">MSDN</a> by <a href="http://wildermuth.com/" target="_blank">Shawn Wildermuth</a> at <a href="http://msdn.microsoft.com/en-us/magazine/dd458800.aspx">http://msdn.microsoft.com/en-us/magazine/dd458800.aspx</a>.  The second article is for Silverlight 2 so it&#8217;s a little bit outdated, however I still found it incredibly useful.</p>
<p style="text-align: justify;">In this article I&#8217;m going to walkthrough the creation of the basic Windows Phone 7 Twitter application pictured to the right.  If you do not have the tools for Windows Phone 7 development, you can find some information about them at <a href="http://developer.windowsphone.com">t</a>he <a href="http://create.msdn.com" target="_blank">App Hub</a>.  If you have no interest in getting the tools, then you should find that most of the content in this article is applicable to a regular Silverlight project as well.  For this project, I used Expression Blend 4 RC and the Windows Phone add-in (see <a href="http://electricbeach.org/?p=671" target="_blank">here</a> for the April 2010 refresh) and Visual Studio 2010 Ultimate (but any version should do).</p>
<p style="text-align: justify;">I created a new Windows Phone Application project in Blend 4 and called it TwitterTimelineDemo and began by making some minor changes to the application and page titles as seen in the picture of my application.  Since this is just a quick application for experimentation, I have simply hardcoded my name into the TextBlock for the page title.</p>
<p style="text-align: justify;">Next I added a simple class called Tweet to represent all tweets in the user&#8217;s timeline:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> TwitterTimelineDemo
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Tweet
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Status <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> DateTime Date <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">This class would act as the <strong>Model</strong> in my application.  The Model is an object that represents the content to be displayed.  In this example I am keeping this very basic and only pulling in the user&#8217;s status and the date it was posted.</p>
<p style="text-align: justify;">Now we will create our <strong>ViewModel</strong> class called TweetViewModel.  The ViewModel is an abstraction of our View and will present our Model in a consumable manner appropriate to the View.  Our ViewModel will populate and hold a collection of Tweet objects (our Model) that we can bind to in the View to present them to the user.  Here is the initial code for the basic ViewModel:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.ObjectModel</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> TwitterTimelineDemo
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> TweetViewModel
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> ObservableCollection<span style="color: #008000;">&lt;</span>Tweet<span style="color: #008000;">&gt;</span> tweets <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ObservableCollection<span style="color: #008000;">&lt;</span>Tweet<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> ObservableCollection<span style="color: #008000;">&lt;</span>Tweet<span style="color: #008000;">&gt;</span> 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>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;"><a href="http://www.jason-mitchell.com/images/blog/MVVMforSilverlightWindowsPhone7App_E533/image_3.png"><img style="margin: 0px 5px 0px 0px; display: inline; border-width: 0px;" title="image" src="http://www.jason-mitchell.com/images/blog/MVVMforSilverlightWindowsPhone7App_E533/image_thumb_3.png" alt="image" width="277" height="102" align="left" border="0" /></a> Next, I created the <strong>View</strong> for my application.  The View refers to all our visual elements and will bind to our ViewModel.  At this point, we can now use Blend&#8217;s sample data feature to style our View without needing to write any code to download tweets.  This is a great feature for designing an application in Blend.  We can create our sample data from an existing class in our project but first we will need to build it from Blend&#8217;s project menu.  Once it has been built, we can open the Data pane on the right-hand side, click the second button from the right, and then select &#8220;Create Sample Data from Class&#8230;&#8221; and then select our TweetViewModel class from the list.</p>
<p style="text-align: justify;">In order to display our sample data, expand the TweetViewModel source and drag the Tweets collection to the controls LayoutRoot grid.  This will create a ListBox containing the sample data; reset it&#8217;s margins to make it fill the entire grid.  You can now style the individual list items by editing the ListBox&#8217;s ItemTemplate.</p>
<p style="text-align: justify;">Now we need to add our TweetView control to MainPage.xaml.  Drag TweetView from the Project section in the Assets pane on the left-hand side of the screen onto the ContentGrid of MainPage.xaml (if it is not there, try rebuilding the project).  Select the newly added control and reset the width and height, change the horizontal and vertical alignments to stretch and reset the margins so it fills the grid.  I don&#8217;t see my sample data when I drag the control to MainPage.xaml and I suspect you can only see it in the control it was added to (<strong>any confirmation?</strong>).  At this stage, I am now done with MainPage.xaml and my XAML looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>phoneNavigation<span style="color: #008000;">:</span>PhoneApplicationPage
    xmlns<span style="color: #008000;">=</span><span style="color: #666666;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span>
    xmlns<span style="color: #008000;">:</span>x<span style="color: #008000;">=</span><span style="color: #666666;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span>
    xmlns<span style="color: #008000;">:</span>phoneNavigation<span style="color: #008000;">=</span><span style="color: #666666;">&quot;clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Navigation&quot;</span>
    xmlns<span style="color: #008000;">:</span>local<span style="color: #008000;">=</span><span style="color: #666666;">&quot;clr-namespace:TwitterTimelineDemo&quot;</span>
    x<span style="color: #008000;">:</span><span style="color: #6666cc; font-weight: bold;">Class</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;TwitterTimelineDemo.MainPage&quot;</span>
    FontFamily<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{StaticResource PhoneFontFamilyNormal}&quot;</span>
    FontSize<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{StaticResource PhoneFontSizeNormal}&quot;</span>
    Foreground<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{StaticResource PhoneForegroundBrush}&quot;</span><span style="color: #008000;">&gt;</span>
&nbsp;
    <span style="color: #008000;">&lt;</span>Grid x<span style="color: #008000;">:</span>Name<span style="color: #008000;">=</span><span style="color: #666666;">&quot;LayoutRoot&quot;</span> Background<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{StaticResource PhoneBackgroundBrush}&quot;</span><span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>Grid<span style="color: #008000;">.</span><span style="color: #0000FF;">RowDefinitions</span><span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>RowDefinition Height<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Auto&quot;</span><span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;</span>RowDefinition Height<span style="color: #008000;">=</span><span style="color: #666666;">&quot;*&quot;</span><span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;/</span>Grid<span style="color: #008000;">.</span><span style="color: #0000FF;">RowDefinitions</span><span style="color: #008000;">&gt;</span>
&nbsp;
        <span style="color: #008000;">&lt;</span>Grid x<span style="color: #008000;">:</span>Name<span style="color: #008000;">=</span><span style="color: #666666;">&quot;TitleGrid&quot;</span> Grid<span style="color: #008000;">.</span><span style="color: #0000FF;">Row</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;0&quot;</span><span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>TextBlock Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Twitter Timeline&quot;</span> x<span style="color: #008000;">:</span>Name<span style="color: #008000;">=</span><span style="color: #666666;">&quot;textBlockPageTitle&quot;</span> Style<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{StaticResource PhoneTextPageTitle1Style}&quot;</span><span style="color: #008000;">/&gt;</span>
            <span style="color: #008000;">&lt;</span>TextBlock Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;jmitch18&quot;</span> x<span style="color: #008000;">:</span>Name<span style="color: #008000;">=</span><span style="color: #666666;">&quot;textBlockListTitle&quot;</span> Style<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{StaticResource PhoneTextPageTitle2Style}&quot;</span><span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;/</span>Grid<span style="color: #008000;">&gt;</span>
&nbsp;
        <span style="color: #008000;">&lt;</span>Grid x<span style="color: #008000;">:</span>Name<span style="color: #008000;">=</span><span style="color: #666666;">&quot;ContentGrid&quot;</span> Grid<span style="color: #008000;">.</span><span style="color: #0000FF;">Row</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;1&quot;</span><span style="color: #008000;">&gt;</span>
            <span style="color: #008000;">&lt;</span>local<span style="color: #008000;">:</span>TweetView<span style="color: #008000;">/&gt;</span>
        <span style="color: #008000;">&lt;/</span>Grid<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;/</span>Grid<span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;/</span>phoneNavigation<span style="color: #008000;">:</span>PhoneApplicationPage<span style="color: #008000;">&gt;</span></pre></div></div>

<p style="text-align: justify;">Now, we need to populate the ViewModel&#8217;s tweet collection with real data.  Twitter feeds are downloaded in an XML format so I used LINQ to extract Tweets.  I added a project reference to System.Xml.Linq and created the following method called GetTwitterEntries in TweetViewModel.cs to return my tweets.  <strong>Be sure to add &#8220;using System.Linq;&#8221; and any other required using statements at the top of the class to prevent build errors.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> IEnumerable<span style="color: #008000;">&lt;</span>Tweet<span style="color: #008000;">&gt;</span> GetTwitterEntries<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> twitterFeed<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    XNamespace <span style="color: #008080;">atomNS </span><span style="color: #008000;">=</span> <span style="color: #666666;">&quot;http://www.w3.org/2005/Atom&quot;</span><span style="color: #008000;">;</span>
    XDocument feed <span style="color: #008000;">=</span> XDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span>twitterFeed<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    IEnumerable<span style="color: #008000;">&lt;</span>Tweet<span style="color: #008000;">&gt;</span> entries <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">from</span> tweet <span style="color: #0600FF; font-weight: bold;">in</span> feed<span style="color: #008000;">.</span><span style="color: #0000FF;">Descendants</span><span style="color: #008000;">&#40;</span>atomNS <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;entry&quot;</span><span style="color: #008000;">&#41;</span>
                                 <span style="color: #0600FF; font-weight: bold;">select</span> <span style="color: #008000;">new</span> Tweet
                                 <span style="color: #008000;">&#123;</span>
                                     Status <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>tweet<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span>atomNS <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;title&quot;</span><span style="color: #008000;">&#41;</span>,
                                     Date <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Parse</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>tweet<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span>atomNS <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;published&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                                 <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> entries<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">To get tweets for my username (jmitch18) I would use the URL <a href="http://search.twitter.com/search.atom?q=from%3Ajmitch18">http://search.twitter.com/search.atom?q=from%3Ajmitch18</a>.  To get tweets for any other user, just replace my name.  In production code, we would get the username from some more dynamic source, but for the purposes of experimentation I have opted for hard coding it.  I added the following constructor and event handler to my TweetViewModel class to populate it&#8217;s collection with real data:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> TweetViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    WebClient webClient <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WebClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    webClient<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadStringCompleted</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> DownloadStringCompletedEventHandler<span style="color: #008000;">&#40;</span>DownloadStringCompleted<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    webClient<span style="color: #008000;">.</span><span style="color: #0000FF;">DownloadStringAsync</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Uri<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;http://search.twitter.com/search.atom?q=from%3Ajmitch18&quot;</span>, UriKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Absolute</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> DownloadStringCompleted<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, DownloadStringCompletedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Tweet tweet <span style="color: #0600FF; font-weight: bold;">in</span> GetTwitterEntries<span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        tweets<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>tweet<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">The last required step is to bind the ViewModel to the View.  I opened TweetView.xaml in Blend and selected it&#8217;s LayoutRoot grid in the Objects pane.  Under the Common Properties section on the Properties pane, you will see a heading &#8220;DataContext&#8221;; click the &#8220;New&#8221; button next to it and select TweetViewModel from the list.  Now if you run the application, after a brief second for loading, you should see a list of real tweets displayed.</p>
<p style="text-align: justify;">This article is based on my current understanding of the MVVM pattern.  I would not say this code is production ready but I am open to constructive feedback and suggested improvements on the topic of this article.  Please feel free to contact me or use the comments section.</p>
<p><strong>Sample project: </strong><a href="http://www.jason-mitchell.com/uploads/TwitterTimelineDemo.zip">http://www.jason-mitchell.com/uploads/TwitterTimelineDemo.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/silverlight/mvvm-for-silverlight-windows-phone-7-app/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Silverlight 3 Rotating Video Player</title>
		<link>http://jason-mitchell.com/silverlight/silverlight-3-rotating-video-player/</link>
		<comments>http://jason-mitchell.com/silverlight/silverlight-3-rotating-video-player/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 11:50:00 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.jason-mitchell.com/index.php/2009/08/30/silverlight-3-rotating-video-player/</guid>
		<description><![CDATA[Yesterday I wrote an article about creating a rotating video player using Silverlight 3 (see here) and I admit that there was an awful lot of text in it, so here is a list of numbered steps for you to follow with less of the rambling in between steps. Create a new Silverlight 3 + [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Yesterday I wrote an article about creating a rotating video player using <a href="http://www.silverlight.net" target="_blank">Silverlight</a> 3 (see <a href="http://www.jason-mitchell.com/index.php/2009/08/29/silverlight-3-fun-3d-projection-and-element-binding/" target="_blank">here</a>) and I admit that there was an awful lot of text in it, so here is a list of numbered steps for you to follow with less of the rambling in between steps.</p>
<p><span id="more-93"></span></p>
<ol>
<li>
<div>Create a new Silverlight 3 + website project (obviously) in Blend 3</div>
</li>
<li>
<div>Create 3 rows in the default grid layout</div>
</li>
<li>
<div>Add MediaElement control to top row</div>
</li>
<li>
<div>Add Slider control to middle row</div>
</li>
<li>
<div>Add two Button controls to bottom row</div>
</li>
<li>
<div>Set Slider maximum value to 360</div>
</li>
<li>
<div>Set MediaElement source to your video</div>
<ul>
<li>
<div>Embedded in project or…</div>
</li>
<li>
<div>web address</div>
</li>
</ul>
</li>
<li>
<div>Select Slider control, open Common Properties pane, right click Value textbox, select Data Binding</div>
</li>
<li>
<div>Select Element Property tab, click MediaElement control from left-hand list</div>
</li>
<li>
<div>Check “Use custom path expression” box and enter Projection.RotationY into the textbox</div>
</li>
<li>
<div>Expand Data Binding window and select TwoWay from the Binding Direction radio buttons</div>
</li>
<li>
<div>Select MediaElement, open Transform pane and make sure the X centre of rotation is 0.5 and Y is 1</div>
</li>
<li>
<div>Run application and you should be able to turn the video using the slider</div>
</li>
<li>
<div>Create a new storyboard called Down</div>
</li>
<li>
<div>Move to 1.5 seconds on timeline and set X value of Projection of the MediaElement to –90</div>
</li>
<li>
<div>Click the key frame on the time line to open the Easing pane</div>
</li>
<li>
<div>Select Bounce Out from drop down list</div>
</li>
<li>
<div>Select a Button control and open it’s event pane (small icon in the top left with a lightning bolt)</div>
</li>
<li>
<div>Enter FallDown into the Click text box and press enter</div>
</li>
<li>
<div>Type “Down.Begin();” in the new method</div>
</li>
<li>
<div>Run application and click the button to make the video fall over</div>
</li>
<li>
<div>Create a new storyboard called Up</div>
</li>
<li>
<div>Move to 1.5 second on timeline and set X value of Projection of the MediaElement to 0</div>
<ul>
<li>
<div>If this is already 0, click the Add New Key Frame button on the timeline</div>
</li>
</ul>
</li>
<li>
<div>Add a Bounce Out easing effect</div>
</li>
<li>
<div>Select the second Button control and create a click event for it and enter “Up.Begin();” into the new method</div>
</li>
</ol>
<p align="justify">And there you have it!  25 steps to create a useless and impractical video player, at least I hope its only 25!</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/silverlight/silverlight-3-rotating-video-player/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Silverlight 3 Fun: 3D Projection and Element Binding</title>
		<link>http://jason-mitchell.com/silverlight/silverlight-3-fun-3d-projection-and-element-binding/</link>
		<comments>http://jason-mitchell.com/silverlight/silverlight-3-fun-3d-projection-and-element-binding/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 16:34:36 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.jason-mitchell.com/index.php/2009/08/29/silverlight-3-fun-3d-projection-and-element-binding/</guid>
		<description><![CDATA[Yesterday at work, there was a talk about Silverlight 3 about its capabilities and the improvements over Silverlight 2.  The talk featured demonstrations of existing applications and some step by step “how to&#8221; tutorials in which I saw how to rotate controls in 3D space and a cool Twitter reader which was made surprisingly quickly; albeit [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Yesterday at work, there was a talk about <a href="http://www.silverlight.net" target="_blank">Silverlight</a> 3 about its capabilities and the improvements over Silverlight 2.  The talk featured demonstrations of existing applications and some step by step “how to&#8221; tutorials in which I saw how to rotate controls in 3D space and a cool <a href="http://www.twitter.com" target="_blank">Twitter</a> reader which was made surprisingly quickly; albeit with some ready prepared code.  It had been a while since I had played with Silverlight and this talk inspired me to go home and have a play about.  In this article I am going to outline the steps taken to create this video player but check out <a href="http://www.jason-mitchell.com/index.php/2009/08/30/silverlight-3-rotating-video-player/">this article</a> for step-by-step instructions to create this video player.</p>
<p><span id="more-89"></span></p>
<p align="justify">To make this application, I used <strong>Expression Blend 3</strong> although since Silverlight is based on <a href="http://en.wikipedia.org/wiki/XAML" target="_blank">XAML</a>, it is entirely possible to do this just from Visual Studio.  Personally, I don’t really like writing XAML unless I really have to and prefer to use the automatically generated code from Expression Blend.</p>
<p align="justify">The first step in creating this application was to give the default <strong>grid layout three rows</strong>, one for the video, one for the slider and one for the up and down buttons.  For this example, this step isn’t all <a href="http://www.jason-mitchell.com/images/blog/Silverlight3Fun3DProjectionandElementBin_D20F/blendControlsDialog.jpg"><img style="border-right-width: 0px; margin: 10px 0px 0px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="blendControlsDialog" src="http://www.jason-mitchell.com/images/blog/Silverlight3Fun3DProjectionandElementBin_D20F/blendControlsDialog_thumb.jpg" border="0" alt="blendControlsDialog" width="492" height="344" align="right" /></a>that important but I did it anyway! The layout for this application is very straightforward to create; we need to add a<strong> MediaElement control</strong> to the top row, a<strong> Slider control</strong> to the second row finally <strong>two Button controls</strong> to the bottom row.  If you don’t know where to find the <strong>MediaElement </strong>and <strong>Slider</strong> controls, you can search for them in the <strong>assets dialog box</strong> which is shown in the image to the right.  Once you have added the<strong> Slider</strong> to your application, you can change the <strong>minimum and maximum values in the Common Properties pane</strong>.  I set mine to go from<strong> 0 to 360 so the Slider will spin the MediaElement around fully</strong>.  You will also want to set the<strong> media source for the MediaElement</strong> control, you can embed a video in the project and reference it in the Source box, however this will create a very large XAP package that will take some time to download.  To solve this you can specify a web address to use as your video source which is what I have done.</p>
<p align="justify">Next, we want to link the value of my <strong>Slider</strong> control, to the <strong>Projection.RotationY</strong> property to rotate the video around the <strong>Y-axis</strong>.  This is really simple to do , in Blend if you select the <strong>Slider control</strong>, open the <strong>Common Properties</strong> pane,<strong> right click on Value</strong> and select <strong>Data Binding</strong> you will see a new dialog box on the screen allowing to create a new data binding.  We want to bind the<strong> Slider</strong> value to the property of another element in the application so select the <strong>Element Property</strong> tab at the top of this window.  You should now see two lists, one that displays the elements in your application and another that displays the element properties.  Select your <strong>MediaElement</strong> control from the list on the left (I called mine<strong> mediaElement</strong>).  Unfortunately we can’t select the <strong>RotationY</strong> property from the second list so we need to check the box below the lists that say<strong> “Use a custom path expression” and enter Projection.RotationY</strong>.  We need to set the <strong>binding direction</strong> option to TwoWay, so click the arrow below the custom expression box and select TwoWay then click ok.  To make the <strong>MediaElement</strong> rotate about it’s centre, we need to change it’s<strong> centre of rotation in the Transform pane</strong>; make sure that the <strong>value of X is 0.5</strong> (the range goes from 0 to 1).   Now if you build and run the application you should be able to rotate the video using the Slider.</p>
<p align="justify">Now we need to make the buttons make the<strong> MediaElement</strong> fall down and come back up again.  First step is to make another change to it’s <strong>centre of rotation</strong>, so if we open the Transform pane again and <strong>set it’s Y value to 1</strong> to indicate that we want it to rotate on the X-axis around the bottom of the element.  In order to create the animations we need to use <strong>Storyboards</strong>, you can create a new storyboard by clicking the “+” in the<strong> Objects and Timeline pane</strong>.  Name your storyboard “Down”, we will use this to to make the<strong> MediaElement</strong> rotate and make it look like it fell over.  Once you create your<strong> storyboard</strong>, the timeline should automatically open; <strong>move the yellow marker</strong> to 1.5 seconds and with the <strong>MediaElement selected</strong>, go to the <strong>Transform pane and set the value of X under Projection to –90</strong>.  Now click play on the timeline and you should see the MediaElement “fall “ down….but it doesn’t look very realistic!!  To make this animation look a bit better, we can use something called<strong> Easing</strong>, a feature new to Silverlight 3. Click on the key frame in the timeline and the<strong> Easing pane</strong> should open up (by default this appears on the right hand side of the screen).  The<strong> Easing pane</strong> contains a drop down list with all the different types of easing, you should definitely have a play about with these and see their effects.  For my example, I chose <strong>Bounce Out </strong>to make it look as if the<strong> MediaElement</strong> has actually hit something that’s stopping it falling further.  Click play on the timeline and see what happens!</p>
<p>We now need to <strong>tell our application when to play this animation</strong>.  So select the button you want to use to make the video fall and open the<strong> events pane</strong> (little icon in the top right of the window with a lightning bolt on it).  In the <strong>Click box, type MoveDown and press enter (</strong>oh look Blend 3 has a built-in code editor), this will automatically hook up the event and create the method to handle it.  In your method type in “Down.Begin();” this will tell the storyboard to begin playing.</p>
<p>Creating the <strong>Up animation</strong> is similar to the Down animation.  Create a new storyboard and call it Up, then in the timeline move the yellow marker to 1.5 seconds and this time click the Record Keyframe button.  You MediaElement should already be displayed in the up-right position so we don’t need to change any properties.  If it isn’t upright, then set the X value for Projection in the Transform pane to 0.  Follow the same steps to add an Easing effect to this animation.  I used a Bounce Out effect again and set it’s bounces to 1 and bounciness to 23.  Don’t forget to set the event to your Up button!</p>
<p>Boy that was a lot of waffling, thinking back I should have maybe just made a numbered list of steps; maybe I will do that later!</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/silverlight/silverlight-3-fun-3d-projection-and-element-binding/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

