views:

49

answers:

4

In Silverlight, is it possible to do a "bouncing balls" style animation entirely in XAML - that is, with no code-behind at all?

+1  A: 

It depends. If you want the physics of the bouncing balls to be simulated at runtime (e.g. to enable interactivity) then that would not be straight-forward to do in pure Xaml. If you just want to play a predefined animation (e.g. as if it were a video) then you could do that via Storyboards and Keyframes. However, this would very likely end up with a very large and very complex Xaml file.

What are you trying to do and why do you want to avoid using code?

KeithMahoney
Thanks for the answer. It is not for any particular application - I'm just learning about Silverlight animation, and I was wondering how much could be done in XAML alone. There's some elegance (potentially) in being able to set up the objects mostly in XAML.
CraigS
+1  A: 

ofcourse! It is 100% possible to do it entirely in XAML. Using easing functions you can get the physics of the bouncing balls in no time.

funwithcoding
But you won't be able to do collision detection with easing functions. If you just want the balls bouncing off fixed walls but not each other, then it would be simple to do the bounce with easing functions.
KeithMahoney
Just bouncing off walls then - can someone show me the code?
CraigS
+1  A: 

By using a physics simulation library, such as physicshelper, you could use XAML to do most, if not all of the work for you. Issues such as collision, etc. would all be handled as behaviors. It's not pure XAML (as it would require a runtime binary reference), but it's the closest you'd probably get without animating the entire scene as a giant Storyboard. Check out this for example, which uses no code.

WPCoder
That was pretty sweet
CraigS
A: 

This is what I came up with -

<UserControl.Resources>
    <Storyboard  x:Name="bounce" >
        <DoubleAnimation Storyboard.TargetName="Ellipse1" 
                Duration="0:0:4" From="0" To="200" 
                RepeatBehavior="Forever" 
                AutoReverse="True" 
                Storyboard.TargetProperty="(Canvas.Top)">
        </DoubleAnimation>
        <DoubleAnimation Storyboard.TargetName="Ellipse1" 
                Duration="0:0:3" From="0" To="200" 
                RepeatBehavior="Forever" 
                AutoReverse="True" 
                Storyboard.TargetProperty="(Canvas.Left)">
        </DoubleAnimation>
    </Storyboard>

</UserControl.Resources>

<Canvas x:Name="LayoutRoot" Background="White">
    <Ellipse Canvas.Left="81" Canvas.Top="63" Height="35" Name="Ellipse1"
        Stroke="#FF190000" StrokeThickness="1" Width="34" Fill="#FF190000" />
</Canvas>

You still need some code to kick it off, though -

bounce.Begin();
CraigS