Silverlight 2.0 — Adjusting UI when Animation completes

I’ve discussed how to locate the UIElement associated with an animation’s Completed event a while back for WPF.

Here’s an update for how I do the same thing in Silverlight 2.0. The technique could be used in WPF with a few minor tweaks. I like this method rather than relying on a Storyboard (as I have better control over the completion of individual animations this way). Here’s a page on MSDN that describes various ways to work with animations programmatically in Silverlight.

The Completed event is raised when the animation timeline has ended. The event unfortunately passes only a reference to the sender (which is the Timeline). The EventArgs is empty.

Here’s a chunk of code that sets up an animation programmatically:

BitmapImage bmimg = sender as BitmapImage;

Game.Dispatcher.BeginInvoke(delegate()
{
    DoubleAnimation animation = new DoubleAnimation();
    animation.To = 0.0;
    animation.From = 1.0;
    animation.Duration = new Duration(TimeSpan.FromSeconds(2 + Game.Randomizer.Next(5)));
    animation.SetValue(Page.ImageProperty, bmimg);
    animation.Completed += new EventHandler(animation_Completed);

    Image img = bmimg.GetValue(Page.ImageProperty) as Image;
    Storyboard sb = new Storyboard();
    sb.Children.Add(animation);
    Storyboard.SetTarget(animation, img);
    Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
    sb.Begin();
});
}

The significant line above is the animation SetValue. Here, I’m using the power of the Dependency Object. I can attach new properties to the animation directly!

public static readonly DependencyProperty ImageProperty =
    DependencyProperty.RegisterAttached("Image", typeof(DependencyObject), typeof(Page), null);

I’ve created a new DependencyProperty (as an attached property) called ImageProperty. There’s not much meaning to the name — other than it makes sense in the context in which I’m using it in the larger application.

void animation_Completed(object sender, EventArgs e)
{
    Timeline t = sender as Timeline;   
    if (t != null)
    {
        BitmapImage img = t.GetValue(Page.ImageProperty) as BitmapImage;
        if (img != null)
        {
            // do something here...
        }
    }
}  

In the completed event, I retrieve the attached property’s value from the animation (which I’ve cast to one of the base types, a Timeline). Then, you could do whatever you want….

Use YSlow for analyzing web page performance

While waiting a VERY long time to look at a page at Adobe’s online store, I decided to use YSlow (a free Firefox plugin) to do a bit of analysis.

image

 

image

It’s too bad more web applications don’t use this (or at least learn some of these concepts as it would save all of us time on the Internet!).

If you’re doing web development, I’d highly recommend you at least try the tool a few times during the development cycle. You may choose not to follow all of the advice, but if you do, you’re users will likely thank you!

Coding Challenge #16

Coding Challenge Series / Technical Interview Series

You’re given the size of a rectangle, with a width and a height. The X = 0 and Y = 0 point is located in the upper left with the width and height in the lower right. The function you must write takes an array of rectangles (left, top, width, height). Return a list of intersecting rectangle groups. If, for a example, a rectangle is not overlapping, it should be considered a group of one. If two rectangles overlap/intersect, create a rectangle which is inclusive of all points within the two rectangles, and return that as a group, and so on.

A rectangle can be in only one group/set.

Interviewed by SD Times

This morning, I was interviewed by a senior editor from the Software Development Times regarding a post on Rock Star developers I made a few months ago. It was fun. We talked about how I look for team players, test platform knowledge, and “knowing” the business. If any thing makes it online/published, I’ll certainly link to it.