Silverlight ‘Loading’ Spin Icon in XAML

by Dean 18. January 2009 15:36

When loading large object collections in Silverlight, there is enough of a time delay so that I need some kind of animated icon that indicates a ‘loading’ state. There were many such icons used when loading data via AJAX, which are basically animated Gif’s. As Gif’s aren't supported in Silverlight, I needed to create one. I decided therefore to create a design in Expression Design, and then animate it in Blend, with a little tidying up in VS2008.

Im not sure how well it’ll perform with large number of instances in a single control, but it does the trick for my needs.

The code for my animated spinning logo is below.

(Use the scaletransform in the grid to change the size)

<Grid x:Name="LayoutRoot" Background="White"> 
    <Grid.RenderTransform> 
        <ScaleTransform x:Name="SpinnerScale" ScaleX="0.5" ScaleY="0.5" /> 
    </Grid.RenderTransform> 
    <Canvas RenderTransformOrigin="0.5,0.5" Width="120" Height="120"> 
        <Ellipse Width="21.835" Height="21.862" Canvas.Left="20.1696" Canvas.Top="9.76358" 
            Stretch="Fill" Fill="#E6000000"/> 
        <Ellipse Width="21.835" Height="21.862" Canvas.Left="2.86816" Canvas.Top="29.9581" 
            Stretch="Fill" Fill="#CD000000"/> 
        <Ellipse Width="21.835" Height="21.862" Canvas.Left="5.03758e-006" Canvas.Top="57.9341" 
            Stretch="Fill" Fill="#B3000000"/> 
        <Ellipse Width="21.835" Height="21.862" Canvas.Left="12.1203" Canvas.Top="83.3163" 
            Stretch="Fill" Fill="#9A000000"/> 
        <Ellipse Width="21.835" Height="21.862" Canvas.Left="36.5459" Canvas.Top="98.138" 
            Stretch="Fill" Fill="#80000000"/> 
        <Ellipse Width="21.835" Height="21.862" Canvas.Left="64.6723" Canvas.Top="96.8411" 
            Stretch="Fill" Fill="#67000000"/> 
        <Ellipse Width="21.835" Height="21.862" Canvas.Left="87.6176" Canvas.Top="81.2783" 
            Stretch="Fill" Fill="#4D000000"/> 
        <Ellipse Width="21.835" Height="21.862" Canvas.Left="98.165" Canvas.Top="54.414" 
            Stretch="Fill" Fill="#34000000"/> 
        <Ellipse Width="21.835" Height="21.862" Canvas.Left="92.9838" Canvas.Top="26.9938" 
            Stretch="Fill" Fill="#1A000000"/> 
        <Ellipse Width="21.835" Height="21.862" Canvas.Left="47.2783" Canvas.Top="0.5" 
            Stretch="Fill" Fill="#FF000000"/> 
        <Canvas.RenderTransform> 
            <RotateTransform x:Name="SpinnerRotate" Angle="0" /> 
        </Canvas.RenderTransform> 
        <Canvas.Triggers> 
            <EventTrigger RoutedEvent="ContentControl.Loaded"> 
                <BeginStoryboard> 
                    <Storyboard> 
                        <DoubleAnimation Storyboard.TargetName="SpinnerRotate" 
                                 Storyboard.TargetProperty="(RotateTransform.Angle)" 
                                 From="0" To="360" Duration="0:0:01" 
                                 RepeatBehavior="Forever" /> 
                    </Storyboard> 
                </BeginStoryboard> 
            </EventTrigger> 
        </Canvas.Triggers> 
    </Canvas> 
</Grid> 

 

Note: If you want to turn this animated spinner on and off (say, when accessing a WCF service), you could do so with something like the code below:

First, in your XAML make the spinner invisible by default

<Grid x:Name="Spinner" Background="White" Visibility="Collapsed">

Then in your code do something like below:

 
var client = new DataServiceClient();
client.DoWorkCompleted += (s, e) => Spinner.Visibility = Visibility.Collapsed;
Spinner.Visibility = Visibility.Visible;
client.DoWorkAsync();

 

hope some of you find this usefull

Dean

Tags: ,

Silverlight

Comments


January 19. 2009 17:28
Philipp
Simple yet effective, and it looks very nice. Thanks for sharing this one!

Cheers,
Philipp


Ireland Thierry 
February 28. 2009 05:34
Thierry
Hi,

Great article! I've been looking for this for the last 2 days, trying to find an equivalent for animated gif and this is absolutely amazing. I was really blown away when I actually ran the code. I'm not familiar with the fancy animation you can do with Silverlight, but really, this is superb.

Can you tell me one thing though, I would like to trigger this at load time or on demand. What do I mean? When my page loads up data the first time, this should be done at load up time which I can do based on your article but how do I go about it to call it when I for example click on an update button and I want to display this, then I want to call my update function?

Do you recommend that I turn this into a control that I load just before calling the update and then remove it from the page when my update is done?

Thanks.

Thierry.


February 28. 2009 08:39
Dean Chalk
Hi Tierry

I have added an extra paragraph at the end of the post to demonstrate turning the spinner on and off.

Hope it helps

Dean


Ireland Thierry 
February 28. 2009 23:15
Thierry
Hi Dean,

Great, thanks a lot for this. I'll be give it a try in a bit. I'm trying to turn this into a re-usable control as I want to pretty much include this in any pages that will make a wfc call and from what I can see right now, there will be quite a few.

Just to let you know that the method I started playing with i.e. loading the control just before making a wfc call, and then remove it after the wfc call was completed seem to also work, though your is definitely better & cleaner.

I'll let you know how I get on.

Thanks.

T.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Most comments

Tom Tom
1 comments
Derek Lakin Derek Lakin
1 comments
gb United Kingdom
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 ButtonChrome.com