Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinRT/Metro Animation in code-behind

The following code works fine in Silverlight:

private void Button_Click_1(object sender, RoutedEventArgs e)
{    
    Storyboard storyboard = new Storyboard();
    DoubleAnimation doubleAnimation = new DoubleAnimation();
    doubleAnimation.From = 50;
    doubleAnimation.To = 100;
    doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
    doubleAnimation.AutoReverse = true;
    doubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(200));
    storyboard.Children.Add(doubleAnimation);
    Storyboard.SetTarget(doubleAnimation, button1);
    Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));
    storyboard.Begin();
}

In WinRT/Metro it needs one minor change to make it compile:

Storyboard.SetTargetProperty(doubleAnimation, "Width");

but when you run it, nothing happens.

If you change the property from "Width" to "Opacity" (also change From=0 and To=1) that works.

What is the problem with "Width"?

like image 975
MetaMapper Avatar asked Jun 29 '12 10:06

MetaMapper


2 Answers

You need to add the following:

doubleAnimation.EnableDependentAnimation = true;

That seems to fix the problem.

like image 89
MetaMapper Avatar answered Nov 11 '22 16:11

MetaMapper


I'm not sure but try to use:

Storyboard.SetTargetProperty(doubleAnimation, Button.WidthProperty);

Instead of

Storyboard.SetTargetProperty(doubleAnimation, "Width");
like image 35
RredCat Avatar answered Nov 11 '22 17:11

RredCat