<?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>Tim Van Wassenhove &#187; Silverlight</title>
	<atom:link href="http://www.timvw.be/category/information-technology/silverlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.timvw.be</link>
	<description>The journey of a thousand miles begins with one step.</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:07:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Calculate EndpointAddress for Silverlight client</title>
		<link>http://www.timvw.be/calculate-endpointaddress-for-silverlight-client/</link>
		<comments>http://www.timvw.be/calculate-endpointaddress-for-silverlight-client/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 20:44:18 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1691</guid>
		<description><![CDATA[Because Silverlight checks the origin it considers http://localhost and http://127.0.0.1 as different locations. In case you want your visitors to be able to use both addresses you can recalculate the address as following: EndpointAddress GetEndpointAddress(EndpointAddress endpointAddress) { var scheme = Application.Current.Host.Source.GetComponents(UriComponents.Scheme, UriFormat.Unescaped); var serverAndPort = Application.Current.Host.Source.GetComponents(UriComponents.HostAndPort, UriFormat.Unescaped); var pathAndQuery = endpointAddress.Uri.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped); return new EndpointAddress(scheme [...]]]></description>
			<content:encoded><![CDATA[<p>Because Silverlight checks the origin it considers http://localhost and http://127.0.0.1 as different locations. In case you want your visitors to be able to use both addresses you can recalculate the address as following:</p>

<pre class="brush: csharp;">EndpointAddress GetEndpointAddress(EndpointAddress endpointAddress)
{
 var scheme = Application.Current.Host.Source.GetComponents(UriComponents.Scheme, UriFormat.Unescaped);
 var serverAndPort = Application.Current.Host.Source.GetComponents(UriComponents.HostAndPort, UriFormat.Unescaped);
 var pathAndQuery = endpointAddress.Uri.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
 return new EndpointAddress(scheme + &quot;://&quot; + serverAndPort + pathAndQuery);
}</pre>

<p>And you can use this method as following:</p>

<pre class="brush: csharp;">var client = new DirectoryServiceClient();
client.Endpoint.Address = GetEndpointAddress(client.Endpoint.Address);
client.GetMessageCompleted += ClientGetMessageCompleted;
client.GetMessageAsync();</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/calculate-endpointaddress-for-silverlight-client/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating graphs with the Silverlight Toolkit</title>
		<link>http://www.timvw.be/creating-graphs-with-the-silverlight-toolkit/</link>
		<comments>http://www.timvw.be/creating-graphs-with-the-silverlight-toolkit/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 20:58:55 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1594</guid>
		<description><![CDATA[As i wrote already: In a chart the elements on the X-axis are usually numbers or dates, and the elements on the Y-axis are usually doubles. We can define such a combination as following: public class Point&#60;T&#62; { public T X { get; set; } public double Y { get; set; } } Here is [...]]]></description>
			<content:encoded><![CDATA[<p>As i wrote already: In a chart the elements on the X-axis are usually numbers or dates, and the elements on the Y-axis are usually doubles. We can define such a combination as following:</p>

<pre class="brush: csharp;">public class Point&lt;T&gt;
{
 public T X { get; set; }
 public double Y { get; set; }
}</pre>

<p>Here is a little helper function for creating line series that are used by the <a href="http://silverlight.codeplex.com/">Silverlight Toolkit</a>:</p>

<pre class="brush: csharp;">public LineSeries Create&lt;T&gt;(string title, Series&lt;T&gt; series, Func&lt;T, double&gt; f) where T : IComparable&lt;T&gt;
{
 var points = series.Select(x =&gt; new Point&lt;T&gt; { X = x, Y = f(x) });

 var lineSeries = new LineSeries
 {
  Title = title,
  ItemsSource = points,
  IndependentValuePath = &quot;X&quot;,
  DependentValuePath = &quot;Y&quot;
 };

 return lineSeries;
}</pre>

<p>Given all this infrastructure we can easily draw the graph of a function:</p>

<pre class="brush: csharp;">public MainPage()
{
 InitializeComponent();

 var series = 0.To(100);

 Func&lt;int, double&gt; multiplyByTwo = x =&gt; 2 * x;
 Chart1.Series.Add(Create(&quot;2x&quot;, series, multiplyByTwo));

 Func&lt;int, double&gt; multiplyByThree = x =&gt; 3 * x;
 Chart1.Series.Add(Create(&quot;x/2&quot;, series, multiplyByThree));
}</pre>

<p>Here is how the result looks like (too much data on the chart):</p>

<img src="http://www.timvw.be/wp-content/images/silverlightchart.png" alt="screenshot of chart in silverlight toolkit" />


]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/creating-graphs-with-the-silverlight-toolkit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silverlight and unit testing..</title>
		<link>http://www.timvw.be/silverlight-and-unit-testing/</link>
		<comments>http://www.timvw.be/silverlight-and-unit-testing/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 20:32:13 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1517</guid>
		<description><![CDATA[A while ago i was looking for a unittesting framework that can be used with Silverlight. Because i don&#8217;t want to launch a webbrowser on my buildserver i ruled the Unit Test Framework for Microsoft Silverlight out. A couple of websearches later i decided to try a Silverlight port of good ol&#8217; NUnit, nunitsilverlight, and [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago i was looking for a unittesting framework that can be used with Silverlight. Because i don&#8217;t want to launch a webbrowser on my buildserver i ruled the <a href="http://code.msdn.microsoft.com/silverlightut/">Unit Test Framework for Microsoft Silverlight</a> out. A couple of websearches later i decided to try a Silverlight port of good ol&#8217; NUnit, <a href="http://code.google.com/p/nunitsilverlight/">nunitsilverlight</a>, and was pretty pleased with results.</p>

<p>A couple of things to keep in mind though:</p>
<ul>
<li>Make sure your test runner loads the correct System assembly (Possible solution: set Copy Local to true in your test project)</li>
<li>In case your test runner has to run tests in both &#8216;regular&#8217; and &#8216;silverlight&#8217; assemblies, make sure that your runner uses separate AppDomains (For NUnit use the /Domain=Multiple option)</li>
</ul>


]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/silverlight-and-unit-testing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Couple of methods missing on ObservableCollection</title>
		<link>http://www.timvw.be/couple-of-methods-missing-on-observablecollection/</link>
		<comments>http://www.timvw.be/couple-of-methods-missing-on-observablecollection/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 15:42:40 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1512</guid>
		<description><![CDATA[Here are a couple of methods that are missing on ObservableCollection&#60;T&#62;: public static class ObservableCollectionExtensions { public static void AddRange&#60;T&#62;(this ObservableCollection&#60;T&#62; observableCollection, IEnumerable&#60;T&#62; elements) { foreach (var element in elements) observableCollection.Add(element); } public static ObservableCollection&#60;T&#62; Create&#60;T&#62;(IEnumerable&#60;T&#62; elements) { var observableCollection = new ObservableCollection&#60;T&#62;(); observableCollection.AddRange(elements); return observableCollection; } }]]></description>
			<content:encoded><![CDATA[<p>Here are a couple of methods that are missing on <a href="http://msdn.microsoft.com/en-us/library/ms668604.aspx">ObservableCollection&lt;T&gt;</a>:</p>

<pre class="brush: csharp;">public static class ObservableCollectionExtensions
{
 public static void AddRange&lt;T&gt;(this ObservableCollection&lt;T&gt; observableCollection, IEnumerable&lt;T&gt; elements)
 {
  foreach (var element in elements) observableCollection.Add(element);
 }

 public static ObservableCollection&lt;T&gt; Create&lt;T&gt;(IEnumerable&lt;T&gt; elements)
 {
  var observableCollection = new ObservableCollection&lt;T&gt;();
  observableCollection.AddRange(elements);
  return observableCollection;
 }
}</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/couple-of-methods-missing-on-observablecollection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Separation of concerns: Behavior = Trigger + TriggerAction</title>
		<link>http://www.timvw.be/separation-of-concerns-behavior-trigger-triggeraction/</link>
		<comments>http://www.timvw.be/separation-of-concerns-behavior-trigger-triggeraction/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 19:40:43 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1482</guid>
		<description><![CDATA[If you look at my KeyBehavior you notice that it is doing two things: register for events so that the behavior can be triggered and handle the actual command invocation. In order to enhance reuse we can refactor this KeyBehavior in a KeyTrigger and an InvokeCommandAction. Well, we&#8217;re not going to do that, because they [...]]]></description>
			<content:encoded><![CDATA[<p>If you look at my <a href="http://www.timvw.be/true-keybehavior-with-system-windows-interactivity-behavior/">KeyBehavior</a> you notice that it is doing two things: register for events so that the behavior can be triggered and handle the actual command invocation. In order to enhance reuse we can refactor this KeyBehavior in a KeyTrigger and an InvokeCommandAction. Well, we&#8217;re not going to do that, because they exist already in the silverlight sdk <img src='http://www.timvw.be/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>

<p>A shortcoming of the InvokeCommandAction is that it can only invoke commands on the FrameworkElement itself, thus we write a custom implementation that finds commands on the DataContext instead:</p>

<pre class="brush: csharp;">public class InvokeCommandAction : TriggerAction&lt;FrameworkElement&gt;
{
 public string CommandName { get; set; }

 protected override void Invoke(object parameter)
 {
  var viewModel = AssociatedObject.DataContext;
  GetCommandAndExecuteIt(viewModel, CommandName);
 }

 void GetCommandAndExecuteIt(object viewModel, string commandName)
 {
  var command = viewModel.GetPropertyValue&lt;ICommand&gt;(commandName);
  if(command.CanExecute(null)) command.Execute(null);
 }
}</pre>

<p>And now we can drag this action on our design surface in Blend and select a trigger that goes with it:</p>

<img src="http://www.timvw.be/wp-content/images/InvokeCommandAction_ChooseTrigger.png" alt="choosing a keypress trigger in blend" />

<p>All we have to do is choose the Key and Command to invoke:</p>

<img src="http://www.timvw.be/wp-content/images/InvokeCommandAction_SetProperties.png" alt="setting properties for action in blend" />

<p>In XAML this looks like:</p>

<pre class="brush: xml;">&lt;interactivity:Interaction.Triggers&gt;
 &lt;ii:KeyTrigger Key=&quot;Right&quot;&gt;
  &lt;inf:InvokeCommandAction CommandName=&quot;PlayerRight&quot;/&gt;
 &lt;/ii:KeyTrigger&gt;
 &lt;ii:KeyTrigger Key=&quot;Left&quot;&gt;
  &lt;inf:InvokeCommandAction CommandName=&quot;PlayerLeft&quot;/&gt;
 &lt;/ii:KeyTrigger&gt;
 &lt;ii:KeyTrigger Key=&quot;Up&quot;&gt;
  &lt;inf:InvokeCommandAction CommandName=&quot;PlayerUp&quot;/&gt;
 &lt;/ii:KeyTrigger&gt;
 &lt;ii:KeyTrigger Key=&quot;Down&quot;&gt;
  &lt;inf:InvokeCommandAction CommandName=&quot;PlayerDown&quot;/&gt;
 &lt;/ii:KeyTrigger&gt;
&lt;/interactivity:Interaction.Triggers&gt;</pre>

<p>I guess this ends our exploration of the behavior features in Silverlight.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/separation-of-concerns-behavior-trigger-triggeraction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>True KeyBehavior with System.Windows.Interactivity.Behavior</title>
		<link>http://www.timvw.be/true-keybehavior-with-system-windows-interactivity-behavior/</link>
		<comments>http://www.timvw.be/true-keybehavior-with-system-windows-interactivity-behavior/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 10:22:56 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1472</guid>
		<description><![CDATA[Yesterday i demonstrated how attached properties can be used to invoke commands on specific key presses (and releases). With the aid of System.Windows.Interactivity.Behavior we can implement a true behavior and we get an extension point to do the required cleanup. &#60;Grid&#62; &#60;interactivity:Interaction.Behaviors&#62; &#60;inf:KeyBehavior&#62; &#60;inf:KeyBehavior.DownKeyCommands&#62; &#60;inf:KeyCommandName Key=&#34;Right&#34; CommandName=&#34;PlayerRight&#34; /&#62; &#60;inf:KeyCommandName Key=&#34;Left&#34; CommandName=&#34;PlayerLeft&#34; /&#62; &#60;inf:KeyCommandName Key=&#34;Up&#34; CommandName=&#34;PlayerUp&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday i demonstrated how attached properties can be used to invoke commands on specific key presses (and releases). With the aid of System.Windows.Interactivity.Behavior we can implement a true behavior and we get an extension point to do the required cleanup.</p>

<img src="http://www.timvw.be/wp-content/images/BlendKeyBehavior.png" alt="screenshot of Blend managing a KeyBehavior" />

<pre class="brush: xml;">&lt;Grid&gt;
 &lt;interactivity:Interaction.Behaviors&gt;
  &lt;inf:KeyBehavior&gt;
   &lt;inf:KeyBehavior.DownKeyCommands&gt;
    &lt;inf:KeyCommandName Key=&quot;Right&quot; CommandName=&quot;PlayerRight&quot;  /&gt;
    &lt;inf:KeyCommandName Key=&quot;Left&quot; CommandName=&quot;PlayerLeft&quot; /&gt;
    &lt;inf:KeyCommandName Key=&quot;Up&quot; CommandName=&quot;PlayerUp&quot; /&gt;
    &lt;inf:KeyCommandName Key=&quot;Down&quot; CommandName=&quot;PlayerDown&quot; /&gt;
   &lt;/inf:KeyBehavior.DownKeyCommands&gt;
  &lt;/inf:KeyBehavior&gt;
 &lt;/interactivity:Interaction.Behaviors&gt;
 ...
&lt;/Grid&gt;</pre>

<p>The behavior implementation is the same as yesterday, only this time we thankfully override the OnAttached and OnDetaching methods:</p>

<pre class="brush: csharp;">public class KeyBehavior : Behavior&lt;FrameworkElement&gt;
{
 public List&lt;KeyCommandName&gt; DownKeyCommands { get; set; }
 public List&lt;KeyCommandName&gt; UpKeyCommands { get; set; }

 public KeyBehavior()
  : base()
 {
  DownKeyCommands = new List&lt;KeyCommandName&gt;();
  UpKeyCommands = new List&lt;KeyCommandName&gt;();
 }

 protected override void OnAttached()
 {
  base.OnAttached();
  SubscribeToKeyEvents();
 }

 protected override void OnDetaching()
 {
  UnsubscribeFromKeyEvents();
  base.OnDetaching();
 }

 void SubscribeToKeyEvents()
 {
  AssociatedObject.KeyDown += AssociatedObject_KeyDown;
  AssociatedObject.KeyUp += AssociatedObject_KeyUp;
 }

 void UnsubscribeFromKeyEvents()
 {
  AssociatedObject.KeyDown -= AssociatedObject_KeyDown;
  AssociatedObject.KeyUp -= AssociatedObject_KeyUp;
 }

 ...
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/true-keybehavior-with-system-windows-interactivity-behavior/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Silverlight: leveraging attached properties to handle key events</title>
		<link>http://www.timvw.be/silverlight-leveraging-attached-properties-to-handle-key-events/</link>
		<comments>http://www.timvw.be/silverlight-leveraging-attached-properties-to-handle-key-events/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 22:21:18 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1457</guid>
		<description><![CDATA[I strongly believe that input handling is a responsability that belongs to the View. At first i simply added the following in the code-behind of my GameView: protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == Key.Left) Model.MovePlayerLeft(); ... } But i wanted to play with the cool kids so i exposed ICommand properties [...]]]></description>
			<content:encoded><![CDATA[<p>I strongly believe that input handling is a responsability that belongs to the View. At first i simply added the following in the code-behind of my GameView:</p>

<pre class="brush: csharp;">protected override void OnKeyDown(KeyEventArgs e)
{
 base.OnKeyDown(e);
 
 if (e.Key == Key.Left) Model.MovePlayerLeft();
 ...
}</pre>

<p>But i wanted to play with the cool kids so i exposed ICommand properties on my ViewModel instead and rewrote the code like this:</p>

<pre class="brush: csharp;">protected override void OnKeyDown(KeyEventArgs e)
{
 base.OnKeyDown(e);
 
 if (e.Key == Key.Left) Model.PlayerLeft.Execute(null);
 ...
</pre>

<p>Offcourse, designers should not have to write code at all, thus i searched for a different solution. Because there isn&#8217;t a behavior that allows me to differentiate the command based on the actual key being pressed i wrote my own KeyEvents class which allows the designer to map a key to a command. Here is an example:</p>

<pre class="brush: xml;">
 &lt;inf:KeyEvents.Down&gt;
  &lt;inf:KeyCommand Key=&quot;Right&quot; CommandName=&quot;PlayerRight&quot;  /&gt;
  &lt;inf:KeyCommand Key=&quot;Left&quot; CommandName=&quot;PlayerLeft&quot; /&gt;
  &lt;inf:KeyCommand Key=&quot;Up&quot; CommandName=&quot;PlayerUp&quot; /&gt;
  &lt;inf:KeyCommand Key=&quot;Down&quot; CommandName=&quot;PlayerDown&quot; /&gt;
 &lt;/inf:KeyEvents.Down&gt;
 ...
&lt;/Grid&gt;</pre>

<p>The down property is nothing more than an attached property:</p>

<pre class="brush: csharp;">public static readonly DependencyProperty DownProperty =
 DependencyProperty.RegisterAttached(&quot;Down&quot;, typeof(List&lt;KeyCommand&gt;), typeof(KeyEvents), new PropertyMetadata(null, OnSetDownCallback));
</pre>

<p>A KeyCommand is a simple pair of a Key and a Command name:</p>

<pre class="brush: csharp;">public class KeyCommand
{
 public Key Key { get; set; }
 public string CommandName { get; set; }
}</pre>

<p>The GetDown method (for the attached Down property) will instantiate a KeyBehavior class which hooks up to the element&#8217;s KeyDown and KeyUp events and uses a bit of reflection to find the commands&#8230;</p>

<pre class="brush: csharp;">public class KeyBehavior
{
 public KeyBehavior(FrameworkElement frameworkElement)
 {
  FrameworkElement = frameworkElement;
  DownKeyCommands = new List&lt;KeyCommand&gt;();
  UpKeyCommands = new List&lt;KeyCommand&gt;();
 
  frameworkElement.KeyDown += frameworkElement_KeyDown;
  frameworkElement.KeyUp += frameworkElement_KeyUp;
 }

 public FrameworkElement FrameworkElement { get; set; }
 public IList&lt;KeyCommand&gt; DownKeyCommands { get; set; }
 public IList&lt;KeyCommand&gt; UpKeyCommands { get; set; }

 void frameworkElement_KeyUp(object sender, KeyEventArgs e)
 {
  ExecuteCommandsForKey(e.Key, UpKeyCommands);
 }

 void frameworkElement_KeyDown(object sender, KeyEventArgs e)
 {
  ExecuteCommandsForKey(e.Key, DownKeyCommands);
 }

 void ExecuteCommandsForKey(Key key, IEnumerable&lt;KeyCommand&gt; commands)
 {
  var commandNamesForKey = commands.Where(p =&gt; p.Key == key).Select(p =&gt; p.CommandName);
  var viewModel = FrameworkElement.DataContext;
  foreach (var command in commandNamesForKey) GetCommandAndExecuteIt(viewModel, command);
 }

 void GetCommandAndExecuteIt(object model, string name)
 {
  var command = GetCommand(model, name);
  command.Execute(null);
 }

 ICommand GetCommand(object model, string name)
 {
  return (ICommand)model.GetType().GetProperty(name).GetValue(model, null);
 }
}</pre>

<p>The only thing that is missing is a way to unsubscribe from the events (and so you will end up with memory leaks). WeakReferences may come of use but i&#8217;ll leave that as an exercise for the reader. Many thanks go to the <a href="http://wpfdisciples.wordpress.com/">WPF Disciples</a> because they inspired me to come up with this attached properties magic.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/silverlight-leveraging-attached-properties-to-handle-key-events/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Exploring graphical programming with Blend, Visual State Manager and Behaviors</title>
		<link>http://www.timvw.be/exploring-graphical-programming-with-blend-visual-state-manager-and-behaviors/</link>
		<comments>http://www.timvw.be/exploring-graphical-programming-with-blend-visual-state-manager-and-behaviors/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 20:37:21 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1437</guid>
		<description><![CDATA[A while ago i presented the ControlStateMachine and in Silverlight this concept is implemented as the Visual State Manager. In my sokoban implementation i have a cellview which exists out of 6 canvasses but only two of them (one for the cell type and one for the piece type) are visible at any given point [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago i presented the <a href="http://www.timvw.be/experimenting-with-controlstatemachine-and-fluent-interfaces/">ControlStateMachine</a> and in Silverlight this concept is implemented as the <a href="http://windowsclient.net/wpf/wpf35/wpf-35sp1-toolkit-visual-state-manager-overview.aspx">Visual State Manager</a>.</p>

<p>In my sokoban implementation i have a cellview which exists out of 6 canvasses but only two of them (one for the cell type and one for the piece type) are visible at any given point in time. I have implemented this with 6 properties CanvasXVisible (with X being Player, Box, Wall, Goal, Floor and Cell) in my ViewModel but a State Machine / Manager may help clarify how the visibility of the canvasses are related. Here are the 2 visual state groups and their states that i would need for the CellView:</p>

<img src="http://www.timvw.be/wp-content/images/cellview-vsm.png" alt="screenshot of visual state manager in expression blend" />

<p>As you can see there is quite a lof of XAML involved to make the correct canvas visible:</p>

<pre class="brush: xml;">
 &lt;Storyboard&gt;
  &lt;ObjectAnimationUsingKeyFrames BeginTime=&quot;00:00:00&quot;  Duration=&quot;00:00:00.0010000&quot; 
   Storyboard.TargetName=&quot;Space&quot;  Storyboard.TargetProperty=&quot;(UIElement.Visibility)&quot;&gt;
    &lt;DiscreteObjectKeyFrame KeyTime=&quot;00:00:00&quot;&gt;
     &lt;DiscreteObjectKeyFrame.Value&gt;
      &lt;Visibility&gt;Visible&lt;/Visibility&gt;
     &lt;/DiscreteObjectKeyFrame.Value&gt;
    &lt;/DiscreteObjectKeyFrame&gt;
   &lt;/ObjectAnimationUsingKeyFrames&gt;
  &lt;/Storyboard&gt;
&lt;/VisualState&gt;</pre>

<p>For a simple modification to the Visibility property this seems like overkill but in many situations you will want to change more than this one property.</p>

<p>With the aid of the behaviors that come with Blend i can quickly add a couple of radio buttons, toss in some gotostate actions and end up with an interactive application:</p>

<img src="http://www.timvw.be/wp-content/images/gotostateaction.png" alt="screenshot of gotostate action properties" />

<pre class="brush: xml;">
 &lt;i:Interaction.Triggers&gt;
  &lt;i:EventTrigger EventName=&quot;Checked&quot;&gt;
   &lt;ic:GoToStateAction StateName=&quot;Wall1&quot;/&gt;
  &lt;/i:EventTrigger&gt;
 &lt;/i:Interaction.Triggers&gt;
&lt;/RadioButton&gt;</pre>

<p>Feel free to try it yourself by changing the radio buttons:</p>

<div id="exploringVSM">
 <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"  width="300" height="240">
  <param name="source" value="http://www.timvw.be/ClientBin/ExploringVSM.xap"/>
  <param name="onError" value="onSilverlightError" />
  <param name="background" value="white" />
  <param name="minRuntimeVersion" value="3.0.40624.0" />
  <param name="autoUpgrade" value="true" />
  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&#038;v=3.0.40624.0" style="text-decoration:none">
   <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
  </a>
 </object>
 <iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
</div></div>

<p>Conclusion: All in all it is relatively easy to create interactive applications using Blend without writing a single line of code. Too bad that the behaviors are in an Expression assembly and don&#8217;t come with standard Silverlight. Another attention point is the maintainability of this new style of programming.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/exploring-graphical-programming-with-blend-visual-state-manager-and-behaviors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ViewModel to translate domain messages to view events</title>
		<link>http://www.timvw.be/viewmodel-to-translate-domain-messages-to-view-events/</link>
		<comments>http://www.timvw.be/viewmodel-to-translate-domain-messages-to-view-events/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 16:18:16 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1430</guid>
		<description><![CDATA[Here is an example of a ViewModel that translates domain messages to view events: class GameViewModel : INotifyPropertyChanged, IListener&#60;BoardChanged&#62; { public event PropertyChangedEventHandler PropertyChanged = delegate { }; public GameViewModel() { var messageBus = ServiceLocator.MessageBus; messageBus.Subscribe&#60;BoardChanged&#62;(this); } void IListener&#60;BoardChanged&#62;.Handle(BoardChanged message) { PropertyChanged(&#34;Board&#34;); } }]]></description>
			<content:encoded><![CDATA[<p>Here is an example of a ViewModel that translates domain messages to view events:</p>

<pre class="brush: csharp;">class GameViewModel : INotifyPropertyChanged, IListener&lt;BoardChanged&gt;
{
 public event PropertyChangedEventHandler PropertyChanged = delegate { }; 

 public GameViewModel()
 {
  var messageBus = ServiceLocator.MessageBus;
  messageBus.Subscribe&lt;BoardChanged&gt;(this);
 }

 void IListener&lt;BoardChanged&gt;.Handle(BoardChanged message)
 {
  PropertyChanged(&quot;Board&quot;);
 }
}</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/viewmodel-to-translate-domain-messages-to-view-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>About databinding and composite views</title>
		<link>http://www.timvw.be/about-databinding-and-composite-views/</link>
		<comments>http://www.timvw.be/about-databinding-and-composite-views/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 15:44:43 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=1417</guid>
		<description><![CDATA[A couple of days ago i had a databound ItemsControl (collection of Model.Cell) which instantiated sub views (with their own viewmodel). &#60;Grid.Resources&#62; &#60;DataTemplate x:Key=&#34;CellTemplate&#34;&#62; &#60;views:CellView /&#62; &#60;/DataTemplate&#62; &#60;/Grid.Resources&#62; &#60;ItemsControl ItemTemplate=&#34;{StaticResource CellTemplate}&#34; ItemsSource=&#34;{Binding Cells}&#34; /&#62; &#60;/Grid&#62; Because each CellViewModel needs to know which cell they manage i used the following dirty hack: public CellView() { Loaded [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago i had a databound ItemsControl (collection of Model.Cell) which instantiated sub views (with their own viewmodel).</p>

<pre class="brush: xml;">
 &lt;Grid.Resources&gt;
  &lt;DataTemplate x:Key=&quot;CellTemplate&quot;&gt;
   &lt;views:CellView /&gt;
  &lt;/DataTemplate&gt;
 &lt;/Grid.Resources&gt;
 &lt;ItemsControl 
   ItemTemplate=&quot;{StaticResource CellTemplate}&quot; 
   ItemsSource=&quot;{Binding Cells}&quot; /&gt;
&lt;/Grid&gt;</pre> 

<p>Because each CellViewModel needs to know which cell they manage i used the following dirty hack:</p>

<pre class="brush: csharp;">public CellView() 
{
 Loaded += CellView_Loaded; 
}

void CellView_Loaded(object sender, RoutedEventArgs e) 
{
 DataContext = new CellViewModel(DataContext);
}</pre>

<p>Later on that day i realised there was a much cleaner solution: Let the BoardViewModel expose a collection of ViewModels.CellViewModel instead of Model.Cell. What a relief that i don&#8217;t have to use the Loaded event hack <img src='http://www.timvw.be/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/about-databinding-and-composite-views/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
