.NET


8
Jan 10

Verbosity: A Silverlight Story

Ok, so this isn’t an outright rip on Silverlight – I did say this morning that it was something that I was going to invest some time and get more familiar with and…well…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’s jump into some code.

First, a comparison. I’ve went ahead and opened Flex Builder, created a new actionscript project and created this really simple project – create a circle and animate it. The code looked like this:

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;
		}
	}
}

I will say, I’ve done this a thousand times so writing this code was almost muscle memory. It’s almost an unfair comparison but still…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’ll move the circle 1 pixel to the right until the end of time or whenever that page is closed. Not much to it.

On to Silverlight. Now you might think I’d use XAML and while it’s a perfectly acceptable assumption I’m just not crazy about coding things like this in xml. I don’t do it with MXML either in case you were wondering. So for this example, it’s all in C#. On with the code:

So I start with this very nice partial class here:

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();

        }
    }
}

Alright, let’s create a circle of some sort:

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

Great, now here is where things start getting a little…wordy.

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

Here I’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’s nice. I probably don’t need to set the opacity but I do anyway. Let’s set the fill for the circle now:

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;

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 0×0000ff 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.

circle.Effect = new DropShadowEffect();
LayoutRoot.Children.Add(circle);

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

Alright, from here we set a DropShadow on our circle, add it to the stage and….place it? Wait, what’s going on here? I honestly searched for X. I was like “Hmmmm, where did they put x?” but what I should have been looking for was Canvas.LeftProperty. Hmmm, I can understand why it would like that, I’m just not sure if I like it? It’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’s not clear, I set the circle 100 pixels from the left and the top of the window.

Now, I’m just going to put the code to animate this circle and I’m not going to walk you through it. Just look at it and we’ll regroup on the other side:

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();

I’m not exactly sure of what all is going on here – what I do know is that it’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 – there’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’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’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:

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();

        }
    }
}

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’d end up with this really difficult pointy and stabby xml file, one which a developer would have to rewrite in C#. I’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:

I’m not having fun.

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…I was doing work. Is it fair to stop playing with Silverlight based off of one example? Well…yeah. Learning a new language should be somewhat invigorating, like getting a new axe and finding trees to cut down. I’m just trying to get through the examples here, getting bored and frustrated and not being too excited about the payoff. I’m not going to end with some proclamation of one tech over the other, but I will say, I’m totally impressed with Silverlight and the tools to create Silverlight content are top notch – but it’s not a lot of fun to play with. I may do some more with it but I’m going to go play with something else for a while. :(


14
Jul 09

The continuing adventures in .NET

long_arm1

It’s been awhile since I’ve talked about doing some .NET development and it hasn’t exactly been a…man, what’s a good analogy? A hill of beans? A walk in the park? I know, I’ve been doing some .NET development and it hasn’t exactly been as bad as dragging my face through a pool filled with shards of glass and viruses. I think I really got off to the wrong foot though. It seems like most books/tutorials/videos for noobs consist mainly of using data components (those one you create in tags that don’t require any “actual code”) – which is cool – but it lulls you into a false sense of “wow, this is so easy I just need to drag and drop and oh wow this is so cool mang!!!!!” Obviously once you want to do something more than a simple CRUD operation on a single table you really start to realize you don’t know what you’re doing and you’re probably not going to know for some time. Fortunately I came across some stuff that really helped me get a grasp how to write asp.net with some amount of sanity (not necessarily properly, just without me wanting to to cram cicadas in my ears to stop the pain):

C#: At work I’m starting to work on an app that is written in VB.NET…yeah, I’m not starting a language war here but it’s been many many years since I touched some VB code and it was too hard to try and wrap my mind around. I quickly set up the app to compile both VB and C# classes so I could keep the legacy while I develop new stuff and haven’t looked back since.

I’m not say there is anything wrong with VB, it’s just not for me. :P

ASP.NET MVC: Ideally, I would re-code the entire app in this framework – it’s really pretty awesome. Obviously if you’re familiar with CodeIgnitor or rails(I don’t need to link to rails do I?) or just about any other MVC framework out there this won’t be a huge jump but it was nice to see how to write a web app without using asp.net components or code-behind or viewstate (ug, I know viewstate is pretty cool and it’s not like a user will ever really see it but I just don’t like it sitting there all snug in my html for some strange reason.) Plus the built-in IDE support was really handy, it really let’s you wire stuff together fairly fast.

Linq to SQL: No, it’s not dying. And why should it, it’s pretty awesome. I’ve been using linqpad to get a handle on the syntax and am getting a pretty good feel on how to use it throughout the application. The LINQ to SQL designer is pretty handy, although I sheepishly admit that I this point I don’t even care how it does its magic, I’m just glad it’s doing it.

Visual Studio: I’ve spent the last year in the eclipse IDE and while I’m not ready to say that Visual Studio is the new hotness for me, it has been pretty useful. Even running it in a VM it’s pretty smooth and having spent so long coding flex it’s nice to have an IDE that feels like it’s just itching to complete just about everything I type.

.NET Podcasts: I spend about 40 minutes driving each and like to learn something during that time – I recommend .NET rocks and Deep Fried Bytes…there are others but those are the two that I actually look forward to listening to…

So those are my tales of triumph and woe…i’m interested in trying some other .NET stuff like mono, silverlight (yes, I do want to at least try it), and XNA but we’ll see what I have time for. Maybe I should just focus on completing my app.


7
Jan 09

Adventures in .NET – An Introduction

What!? .NET!?  This is the site where a certain somebody poked fun at Vista and .NET and everything Microsoft for some time right!?

It is – however – in the spirit of keeping an open mind and trying to learn something new, and since we use a Microsoft stack aside from our heavy use of flash – it’s something that I feel like I should at least moderately comfortable with. Plus there are other motivating factors…

ASP.net for all intensive purposes is actually kinda slick. I’ve been tearing through the ASP.NET Unleashed book and playing with some of the examples (mostly data stuff and asp controls) and I’ve been really impressed with how fast you can crank out some basic CRUD apps. Admittedly, it has taken some time to grasp the whole Viewstate, code-behind, and postback model (and I’m not exactly sure I get it all yet) but it seems to be coming together. Plus, I’ve been having some fun MVC, although I’m not really sure what to call it (asp.net mvc, the mvc framework, just mvc?). We’ll see what happens.

Silverlight (I know I know I know I KNOW) : I’ve been quite vocal in my comparison between Silverlight and Flash and while I still love Flash and everything that is going on in the community I think I might be foolish to turn a blind eye to the rapid progress that is happening on the silverlight side. Things seem to rapidly be getting better over there and I’m kinda interested in getting my hands into some silverlight examples…

XNA…yeah, I still have childhood memories of sitting down with a ream of graph paper and designing the next great platform shooter (like the old 8-bit classics that we spent half of our time trying to make work in our aging and apparently dusty nintendos)…this memory brought me into Flash, I’m interested to see where it could go in this platform.

Soooo, no I’m not becoming an outright Microsoft fanboy but yes I am seeing what life is like on the other side of the fence.

Blore!