coder
coder

Reputation: 4283

silverlight animation not working

This is the first time I am creating a silverlight animation. I have been struggling to create a simple animation for the last few days I just couldn't get it to work. I can't believe why it is so difficult.

Xaml:

<UserControl x:Class="BuzzLifeAppsSilverlight.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <Storyboard x:Name="Storyboard1" AutoReverse="True" RepeatBehavior="Forever" BeginTime="1">
            <DoubleAnimation Duration="0:0:2" To="-332" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="button1" d:IsOptimized="True"/>
            <DoubleAnimation Duration="0:0:2" To="-12" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="button1" d:IsOptimized="True"/>
        </Storyboard>
    </UserControl.Resources>

    <Canvas x:Name="LayoutRoot" Background="White">
        <Button Canvas.Left="235" Canvas.Top="136" Content="Button" Height="23" Name="button1" Width="75" RenderTransformOrigin="0.5,0.5" >
            <Button.RenderTransform>
                <CompositeTransform/>
            </Button.RenderTransform>
        </Button>
    </Canvas>
</UserControl>

Code:

public MainPage()
{
    InitializeComponent();

    Storyboard1.Begin();
}

Upvotes: 2

Views: 524

Answers (1)

Jehof
Jehof

Reputation: 35544

Remove the declaration of BeginTime in your storyboard or change it to another value (like 0:0:1).

A value of 1 means that the animation should start in one hour, setting it to 0:0:1, will start the animation after 1 second.

Also start the animation, when the MainPage is loaded and added to the visual tree, like so..

public MainPage(){
  InitializeComponent();
  this.Loaded += new System.Windows.RoutedEventHandler(MainPage_Loaded);
}

private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e){
  Storyboard1.Begin();
}

Upvotes: 4

Related Questions