Gone are those days where animations in Silverlight are viewed as fancy add-ons. Nowadays simple animations are becoming an essential part of the Silverlight applications. Nothing much to imagine, displaying collection as list in brief generally and then displaying the same in detail upon selecting the list item is one such scenario where animation helps us a lot in achieving it. Ok, let us drift our focus to the topic now.
Enough with description, let us feel it in code snippet as follows.
Snippet 1
<Grid x:Name="LayoutRoot" Background="White"> <Grid.Resources> <Storyboard x:Key="RectHeightStoryBoard" > <DoubleAnimation From="20" To="200" Duration="00:00:3" Storyboard.TargetName="AnimationTestRectangle" Storyboard.TargetProperty="Height"></DoubleAnimation> </Storyboard> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <Rectangle x:Name="AnimationTestRectangle" MouseLeftButtonDown="AnimationTestRectangle_MouseLeftButtonDown" Fill="Crimson" Width="200" Height="20" Grid.ColumnSpan="2" /> <StackPanel Grid.Row="1" Grid.ColumnSpan="2" Orientation="Vertical"> <StackPanel > <TextBlock Text="Height" Margin="10"/> <TextBlock Name="HeightValueTextBlock" Text="" Margin="10"/> </StackPanel> <StackPanel > <TextBlock Text="AnimationBaseValue Height" Margin="10"/> <TextBlock Name="AnimationBaseValueTextBlock" Text="" Margin="10"/> </StackPanel> <Button Content="Check Height" Name="CheckHeightButton" Width="85" Click="CheckHeightButton_Click" /> </StackPanel> </Grid>
public partial class GetAnimationBaseValueTest : UserControl { public GetAnimationBaseValueTest() { InitializeComponent(); } private void AnimationTestRectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Storyboard rectHeightStoryBoard = (Storyboard)LayoutRoot.Resources["RectHeightStoryBoard"]; rectHeightStoryBoard.Begin(); ReadAnimationTestRectangleHeight(); } private void CheckHeightButton_Click(object sender, RoutedEventArgs e) { ReadAnimationTestRectangleHeight(); } private void ReadAnimationTestRectangleHeight() { HeightValueTextBlock.Text = AnimationTestRectangle.Height.ToString(); AnimationBaseValueTextBlock.Text = AnimationTestRectangle.GetAnimationBaseValue(HeightProperty).ToString(); } }
In the above snippet, I've created a storyboard targeting a rectangle for animating the height of the rectangle with some time delay. In the Click event of the CheckHeightButton, I'm calculating the height normally and then using the GetAnimationBaseValue method and displaying the values in related textblocks for verification. In the MouseLeftButtonDown event of the rectangle, I start the animation from the codebehind. Now, in order to evaluate, first click on the rectangle which triggers the animation. In due course when the animation in progress, click on the CheckHeightButton which displays the resultant values. There you can infer the use of GetAnimationBaseValue method.