After getting back from Microsoft, I dug up my old Compaq laptop, dusted it off and installed Vista Ultimate and Expression Studio (full disclosure – these were gifts from MS). Then I installed Silverlight 1.1, the Visual Studio 2008 trial, and the Silverlight 1.1 extensions for VS 2008 and got to work on some tutorials.
Once I got a few basic things working, I started trying out some stuff on my own. I figured the first thing would be to make a red ball that moved around the screen with x and y velocity, then give it gravity, make it bounce, make it draggable, etc. I ran into some things that made me wonder about people who say things like, “Flash is hard to learn. Silverlight is so much easier.” I’m honestly not trying to take a dig at Silverlight here. If someone can explain an easier way of doing things or something I’m missing, please do.
First off, making the ball was pretty easy. I made a canvas with an ellipse. Gave the canvas a name so I can access it in the C# code behind class. It’s there, giving code hints and everything. Great. So far, just like Flex.
Now, I need the equivalent of an “enterFrame” or a Timer… looking…. looking…… hmmm. Nothing like that at all. Time to search the web. I found this:
And several other links that recommend the exact same thing. This, as far as I can tell, is the accepted way of doing programmatic animation. Basically, you create a Storyboard object in XAML, and inside that put a DoubleAnimation, giving it a Duration.
Now, a DoubleAnimation, for you Flash folks, is analogous to some stuff in the various ActionScript tween packages. It’s designed to tween a double precision floating point number from one value to another. But it’s only being used as a makeshift timer here. The duration is set as a fraction of a second, like “00:00:00.017” which is about a 60th of a second. You can set up some dummy property to tween, or just leave it as is. When the “tween” is finished, it fires a complete event. In your C# class, you listen for the complete event and this is like your onEnterFrame or onTimer handler in Flash. The problem is, the Storyboard really is complete. It’s not a continuous timer or loop. So in your handler, you have to call Begin() on the storyboard so that it will run again and give you another complete event.
OK, I guess that works, but it seems like a complete hack. It seems like some of the old hacks we used to use back in AS1. Again, I’m not trying to just bash it. This is just my reaction. Having some kind of timer or looping system to repeatedly run code seems like a cornerstone of creating programmatic animation. The fact that developers had to come up with a hack like this seems like a MAJOR omission. Maybe this will be addressed by Silverlight 2.0. I certainly hope so.
The next major thing I ran into was in addressing objects from XAML in C#. For a Flex analogy, say you create an object, any object, say a Button, in MXML. In ActionScript you can address its properties directly, like:
[as]_currentX = myButton.x[/as]
Compare that to what you need to do in Silverlight:
[as]_currentX = (double)Ball.GetValue(Canvas.LeftProperty);[/as]
This is like Flash 4 getProperty / setProperty stuff. Yuck.
And to set a property:
[as])Ball.SetValue
So, whereas you could easily move something in AS3 like so:
[as]ball.x += 10;[/as]
In Silverlight, the equivalent code would be:
[as]Ball.SetValue
Please tell me if I am missing something here. My gut feeling is this can’t be right. But every tutorial I see is written like this.
For example, the following code explains how to do drag and drop in Silverlight:
https://msmvps.com/blogs/luisabreu/archive/2007/06/14/drag-n-drop-on-silverlight-alpha.aspx
In ActionScript, 90% of this would boil down to calls to startDrag() and stopDrag().
Now it’s not that I am scared of lower level, more verbose code. But usually a lower level language gives you a lot more power and control over things. I don’t see that that is the case here. I don’t see that the APIs for Silverlight are really lower level than what you have in ActionScript, just more verbose.
Also, I see how I could easily write classes that abstract that kind of stuff. Have a CanvasProxy class or something that has direct x and y properties, and internally manipulates the LeftProperty and TopProperty of a real canvas. I guess my real point is that I’ve seen so often from the Microsoft camp that “Flash is sooo hard to learn, try Silverlight. It’s so much easier.” If that is the intent, then why not give higher level APIs that abstract that stuff and make things simpler for developers?
I feel I need to say it once more – I’m really looking for answers, not just digging on Silverlight. I know you MS guys are reading this! 😉 Let me know.