BouncingPlane Behavior for Silverlight

A quick post showing a gratuitous demo of a bouncing plane using the Silverlight 3 perspective projection feature, packaged as a reusable behavior...

Time for a brief but fun post... some time back Tim Heuer posted the Silverlight 3 bouncing plane gratuitous demo. Click an element, and the nearest corner would bounce backwards and forwards as it comes back to rest.

Tim had the code to setup the storyboards, and handle the mouse interaction in code-behind. I look at it, and immediately see a reusable component (even if it is a gratuitous one), or more specifically a behavior, that encapsulates all the logic, and can be attached declaratively in XAML to one or more elements simply without needing any code-behind logic. So I created one such behavior. I used this behavior in my TwitterBug sample at TechEd recently, and thought it could use a dedicated blog post. :-)

Here is a screenshot, which really doesn’t do much justice to something interactive. So go ahead, and click it to run the sample live. Click around on the images. And if you’re wondering how its done, go ahead and download the code. You’ll see a BouncingPlane class that derives from Behavior.

Bouncing Images

Here is a an example of using the behavior in XAML as applied to one of the images in the application:

<Border Grid.Column="2" Grid.Row="0" Style="{StaticResource borderStyle}">
  <fxui:Interaction.Behaviors>
    <demo:BouncingPlane />
  </fxui:Interaction.Behaviors>

  <Image Style="{StaticResource imageStyle}" Source="/Images/Penguins.jpg" />
</Border>

There isn’t anything really new in the specifics of the implementation - I just refactored the code-behind logic into a behavior, but it does show the power of the behavior concept. Here is the sample code for you to download and check out.

I've been quite fond of the behavior concept for a long while now. I first did an implementation of the behaviors concept in asp.net ajax as well as control extender server controls on the server, and then in Silverlight.FX. More recently, I worked with the Blend team on getting this concept into Expression where they’re really becoming popular. I hope they will be in the SDK proper at some point in the future.

I think behaviors are truly useful (and essential) in UI and in declarative programming as a lightweight component mechanism that is based on composition rather than derivation.

Posted on Thursday, 8/27/2009 @ 4:15 PM | #Silverlight


Comments

8 comments have been posted.

Corey

Posted on 8/27/2009 @ 6:43 PM
Great behavior Nikhil, you should put this on the Expression Community Gallery http://gallery.expression.microsoft.com/en-us/

Fallon Massey

Posted on 8/27/2009 @ 11:26 PM
This code uses overrides that don't exist in standard behaviors, they must exist in your library, but OnAttach() should be OnAttached() in standard behaviors.

Nikhil Kothari

Posted on 8/28/2009 @ 12:15 AM
I'd love for the standard ones to be in the SDK, so as to avoid taking dependency on a toolkit component. In addition, the big reason I've had to keep mine around is support for data-binding capability on behavior properties (for now).

FWIW, the TechEd link I pointed to has the same code but compiled against the Expression assemblies.

dude

Posted on 8/28/2009 @ 8:22 AM
great stuff !

ashraful.asif

Posted on 8/30/2009 @ 10:19 PM
nice job

Peter Bromberg

Posted on 9/14/2009 @ 2:15 PM
I'm glad to see you guys interacting and posting all this cool Silverlight stuff. You in particular, are a real thinker. Kudos.

David Rousset

Posted on 10/2/2009 @ 7:48 AM
Hi! This is a very cool behavior. I've blogged a tutorial in French here : http://blogs.msdn.com/davrous/archive/2009/09/29/silverlight-3-ajoutez-un-behavior-d-effets-physiques-vos-l-ments.aspx to show how to use your behavior directly inside Expression Blend 3.0.

Thanks Nikhil for all your posts. There are very valuable for us! Keep up the good work. :)

David

sherry

Posted on 3/7/2010 @ 9:01 PM
can you share the original code with me please? thank you !
Post your comment and continue the discussion.