Hito
Hito

Reputation: 707

ColorAnimation doesn't do anything

I'm trying to integrate a little colorApplication to a little "game" I did in silverlight.

I'm kinda noob with C# so I just searched on the net and I found something which explain how to do a colorApplication.

Here, I just want to change my background color from orange to blue and then use the autoreverse to come back to orange.

Here is my XAML

<Grid x:Name="LayoutRoot" Background="Orange">
    <Grid.Resources>
        <Storyboard x:Name="colorStoryboard">
            <ColorAnimation BeginTime="00:00:00" AutoReverse="True"
                            Storyboard.TargetName="LayoutRoot"
                            Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" 
                            Duration="00:00:02" From="Orange" To="Blue" />
        </Storyboard>
    </Grid.Resources>

    <sdk:Label Height="16" HorizontalAlignment="Left" Margin="162,12,0,0" 
               Name="label1" VerticalAlignment="Top" Width="84" 
               Content="Vrai ou Faux ?" />
    <Button Content="Vrai" Height="23" HorizontalAlignment="Left" Margin="59,61,0,0"
            Name="buttonVrai" VerticalAlignment="Top" Width="75" 
             Click="buttonVrai_Click" />
    <Button Content="Faux" Height="23" HorizontalAlignment="Left" Margin="267,61,0,0"
            Name="buttonFaux" VerticalAlignment="Top" Width="75" 
            Click="buttonFaux_Click" />
    <sdk:Label Height="20" HorizontalAlignment="Left" Margin="59,184,0,0" 
               Name="label2" VerticalAlignment="Top" Width="60" Content="Score" />
    <sdk:Label Height="20" HorizontalAlignment="Left" Margin="322,184,0,0" 
               Name="labelScore" VerticalAlignment="Top" Width="20" Content="0" />
    <Button Content="Redémarrer" Height="23" HorizontalAlignment="Left" 
            Margin="162,265,0,0" Name="buttonRedemarrer" VerticalAlignment="Top" 
            Width="84" Click="buttonRedemarrer_Click" />
</Grid>

I just followed the tutorial but it doesn't work. My background color doesn't change. Did I forgot something ? Thanks for your help guys...

Upvotes: 0

Views: 119

Answers (3)

Labrinths
Labrinths

Reputation: 130

You can start it by altering the eventtrigger, right now the animation is trigged once the windows is loaded.

<EventTrigger RoutedEvent="Window.Loaded">

Or if you want to do it in codebehind at any point

colorStoryboard.Begin();

Upvotes: 0

Hito
Hito

Reputation: 707

I thought that I didn't have to write a method or something like that to start the animation. I thought that set the BeginTime = "00:00:00" will implicitly start the animation. I can compile and run this code.

So now, my "new question" is : How can I auto-start the animation ?

Upvotes: 0

Daniel
Daniel

Reputation: 11064

The problem is probably that nothing is triggering the animation. If I trigger the animation on, say, Window.Loaded, the animation works fine:

<Grid x:Name="LayoutRoot"
      Background="Orange">
  <Grid.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded">
      <BeginStoryboard>
        <Storyboard x:Name="colorStoryboard">
          <ColorAnimation BeginTime="00:00:00"
                          AutoReverse="True"
                          Storyboard.TargetName="LayoutRoot"
                          Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
                          Duration="00:00:02"
                          From="Orange"
                          To="Blue" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Grid.Triggers>
  <!-- ETC -->
</Grid>

You'll need to trigger the animation somehow -- perhaps on a button click or another routed event.

Upvotes: 2

Related Questions