Wednesday 9 November 2011

Silverlight Animation

For creating animation in Silverlight, we have to change object's properties like size, position, color etc over period of time.

Here is a simple example of button-growing where storyboard applies a DoubleAnimation to the Width property of a button.

1) Open Visual Studio -> Click File -> New Project -> In Project dialog box :- Select Silverlight application -> Ok.

2) Click Assets -> Control -> Button.

//MainPage.xaml
<Grid x:Name="LayoutRoot">
        <Button x:Name="btnGrow" Width="160" Height="30" Click="btnGrow_Click"
Content="Keep Growing"></Button>
    </Grid>

For Creating Animation, write :

 <UserControl.Resources>
        <Storyboard x:Name="storyboard">
            <DoubleAnimation Storyboard.TargetName="btnGrow" Storyboard.TargetProperty="Width"
From="160" To="300" Duration="0:0:5"></DoubleAnimation>
        </Storyboard>
    </UserControl.Resources>

//MainPage.xaml.cs
private void btnGrow_Click(object sender, RoutedEventArgs e)
        {
            storyboard.Begin();
        }

3) Run the application,



On Clicking Button :


No comments:

Post a Comment