<?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>paulmignard &#187; Silverlight</title>
	<atom:link href="http://paulmignard.com/category/silverlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://paulmignard.com</link>
	<description></description>
	<lastBuildDate>Sun, 18 Sep 2011 17:20:41 +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>Verbosity: A Silverlight Story</title>
		<link>http://paulmignard.com/2010/01/verbosity-a-silverlight-story/</link>
		<comments>http://paulmignard.com/2010/01/verbosity-a-silverlight-story/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 05:03:47 +0000</pubDate>
		<dc:creator>paul</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://paulmignard.com/?p=342</guid>
		<description><![CDATA[Ok, so this isn&#8217;t an outright rip on Silverlight &#8211; I did say this morning that it was something that I was going to invest some time and get more familiar with and&#8230;well&#8230;more on that later. This is a sample (&#8230;)</p><p><a href="http://paulmignard.com/2010/01/verbosity-a-silverlight-story/">Read the rest of this entry &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Ok, so this isn&#8217;t an outright rip on Silverlight &#8211; I did say <a href="http://paulmignard.com/2010/01/new-year-new-projects/">this morning</a> that it was something that I was going to invest some time and get more familiar with and&#8230;well&#8230;more on that later. This is a sample of my experience of playing with silverlight over the past couple of days. All I wanted to do was to create a circle, add it to the stage, and animate it. Let&#8217;s jump into some code.</p>
<p>First, a comparison. I&#8217;ve went ahead and opened Flex Builder, created a new actionscript project and created this really simple project &#8211; create a circle and animate it. The code looked like this:</p>
<pre>
<code>package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;

	public class CircleTest extends Sprite
	{
		public function CircleTest()
		{
			var circle:Sprite = new Sprite();
			circle.graphics.lineStyle(1,0);
			circle.graphics.beginFill(0x1d5d8f,1);
			circle.graphics.drawCircle(0,0,100);

                        this.addChild(circle);
			circle.x = 100;
			circle.y = 100;

			circle.filters = [new DropShadowFilter(2,45,0,.5)]

			circle.addEventListener(Event.ENTER_FRAME,moveCircle);
		}

		private function moveCircle(event:Event):void
		{
			event.currentTarget.x += 1;
		}
	}
}</code>
</pre>
<p>I will say, I&#8217;ve done this a thousand times so writing this code was almost muscle memory. It&#8217;s almost an unfair comparison but still&#8230;Anyway, nothing too crazy here. Create a Sprite, define the line style, determine the fill, draw the circle, add it to the stage, determine the circle location, add a drop shadow filter as part of an array to the filters property on the Sprite and then add an event listener for the ENTER_FRAME event which occurs 30 times a second. In that listener, I&#8217;ll move the circle 1 pixel to the right until the end of time or whenever that page is closed. Not much to it.</p>
<p>On to Silverlight. Now you might think I&#8217;d use XAML and while it&#8217;s a perfectly acceptable assumption I&#8217;m just not crazy about coding things like this in xml. I don&#8217;t do it with MXML either in case you were wondering. So for this example, it&#8217;s all in C#. On with the code:</p>
<p>So I start with this very nice partial class here:</p>
<pre>
<code>using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Shapes;

namespace TestSilverlightApp
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

        }
    }
}</code>
</pre>
<p>Alright, let&#8217;s create a circle of some sort:</p>
<pre>
<code>Ellipse circle = new Ellipse();
circle.Width = 100;
circle.Height = 100;</code>
</pre>
<p>Great, now here is where things start getting a little&#8230;wordy.</p>
<pre>
<code>SolidColorBrush stroke = new SolidColorBrush(Colors.Black);
stroke.Opacity = 100.0;
circle.Stroke = stroke;</code>
</pre>
<p>Here I&#8217;m creating a new object to set the stroke on my circle. Not too bad, I can see where the SolidColorBrush actually has a ton of paramaters that you can set to really customize it so that&#8217;s nice. I probably don&#8217;t need to set the opacity but I do anyway. Let&#8217;s set the fill for the circle now:</p>
<pre>
<code>byte r = System.Convert.ToByte("1d", 16);
byte g = System.Convert.ToByte("5d", 16);
byte b = System.Convert.ToByte("8f", 16);
Color s = Color.FromArgb(100, r, g, b);

SolidColorBrush fill = new SolidColorBrush(s);
fill.Opacity = 100.0;
circle.Fill = fill;</code>
</pre>
<p>Now, to set the color on the fill you can use solid colors like Color.Blue or Color.Red but it only takes about 15 seconds with a designer before you realize that they will never ever ever use either 0x0000ff or 0xff0000 for anything ever for any reason ever. So we need to use a uint and you can see that requires some more code. Still not too bad, I can abstract some of that pain away into some function somewhere but on its face it seems to be a lot of code to set a particular color.</p>
<pre>
<code>circle.Effect = new DropShadowEffect();
LayoutRoot.Children.Add(circle);

circle.SetValue(Canvas.LeftProperty, (double)100);
circle.SetValue(Canvas.TopProperty, (double)100);</code>
</pre>
<p>Alright, from here we set a DropShadow on our circle, add it to the stage and&#8230;.place it?  Wait, what&#8217;s going on here? I honestly searched for X. I was like &#8220;Hmmmm, where did they put x?&#8221; but what I should have been looking for was Canvas.LeftProperty. Hmmm, I can understand why it would like that, I&#8217;m just not sure if I like it?  It&#8217;s certainly more elegant that x but x = y is a lot easier to write and comprehend than SetValue(Canvas.LeftProperty,(double)y). Anyway, in case it&#8217;s not clear, I set the circle 100 pixels from the left and the top of the window.</p>
<p>Now, I&#8217;m just going to put the code to animate this circle and I&#8217;m not going to walk you through it. Just look at it and we&#8217;ll regroup on the other side:</p>
<pre>
<code>Duration duration = new Duration(TimeSpan.FromSeconds((double)6));
DoubleAnimation animation = new DoubleAnimation();
animation.From = 100;
animation.To = 600;
animation.Duration = duration;

Storyboard storyboard = new Storyboard();
storyboard.Duration = duration;
storyboard.Children.Add(animation);

Storyboard.SetTarget(animation, circle);
Storyboard.SetTargetProperty(animation,new PropertyPath("(Canvas.Left)"));
storyboard.Begin();</code>
</pre>
<p>I&#8217;m not exactly sure of what all is going on here &#8211; what I do know is that it&#8217;s take my circle, moving it from 100 pixels from the left to 600 pixels from the left over the course of 6 seconds. It works, but great day, that is a lot of code. 3 different objects need to be instantiated so that I can move a circle. Granted, there is a plus here &#8211; there&#8217;s not the ever present and always ticking timeline like you find in Flash. I do like that you create your animation for a specific point and time and when you don&#8217;t need it anymore, you throw it away. That pretty cool. But what make me nervous is that if this is the most simple of things that you can do in Silverlight, what does it look like when you try to accomplish something more complicated? I can see how you&#8217;d be able to start stringing things together to make a predetermined animation but how do you implement physics or changing directions based on a changing variable in the browser (like a mouse?) Here is the complete Silverlight class:</p>
<pre>
<code>using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Shapes;

namespace TestSilverlightApp
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            Ellipse circle = new Ellipse();
            circle.Width = 100;
            circle.Height = 100;

            SolidColorBrush stroke = new SolidColorBrush(Colors.Black);
            stroke.Opacity = 100.0;
            circle.Stroke = stroke;

            byte r = System.Convert.ToByte("1d", 16);
            byte g = System.Convert.ToByte("5d", 16);
            byte b = System.Convert.ToByte("8f", 16);
            Color s = Color.FromArgb(100, r, g, b);

            SolidColorBrush fill = new SolidColorBrush(s);
            fill.Opacity = 100.0;
            circle.Fill = fill;

            circle.Effect = new DropShadowEffect();

            LayoutRoot.Children.Add(circle);

            circle.SetValue(Canvas.LeftProperty, (double)100);
            circle.SetValue(Canvas.TopProperty, (double)100);

            Duration duration = new Duration(TimeSpan.FromSeconds((double)6));
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = 100;
            animation.To = 600;
            animation.Duration = duration;

            Storyboard storyboard = new Storyboard();
            storyboard.Duration = duration;
            storyboard.Children.Add(animation);

            Storyboard.SetTarget(animation, circle);
            Storyboard.SetTargetProperty(animation,new PropertyPath("(Canvas.Left)"));
            storyboard.Begin();

        }
    }
}</code>
</pre>
<p>So, wow. This makes me think of the designer exodus from Flash when they switched from AS2 to AS3. Teaching a designer how to add an event listener is really kinda hard because animation used to be fairly easy. I certainly could not teach them this. Sure, you can create this stuff in XAML and configure all of your animations but even that seems like you&#8217;d end up with this really difficult pointy and stabby xml file, one which a developer would have to rewrite in C#. I&#8217;m probably making too much out of it but as I was going through this exercise and having waves of doubt as to my proficiency as a developer I came across a sobering revelation:</p>
<p>I&#8217;m not having fun.</p>
<p>For me, the end result to gauge my satisfaction by shows up in the browser, not in the code. I certainly felt awesome being able to wield this powerful language with this awesome set of tools but after messing around with this for what seems like way too long, I was getting bored&#8230;I was doing work. Is it fair to stop playing with Silverlight based off of one example? Well&#8230;yeah. Learning a new language should be somewhat invigorating, like getting a new axe and finding trees to cut down. I&#8217;m just trying to get through the examples here, getting bored and frustrated and not being too excited about the payoff. I&#8217;m not going to end with some proclamation of one tech over the other, but I will say, I&#8217;m totally impressed with Silverlight and the tools to create Silverlight content are top notch &#8211; but it&#8217;s not a lot of fun to play with. I may do some more with it but I&#8217;m going to go play with something else for a while. <img src='http://paulmignard.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://paulmignard.com/2010/01/verbosity-a-silverlight-story/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

