15

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"?

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
MetaMapper
  • 968
  • 8
  • 22
  • Why haven't you used the `EventTrigger` http://msdn.microsoft.com/en-us/library/system.windows.eventtrigger.aspx for moving all this code to xaml? – RredCat Jun 29 '12 at 11:32
  • 2
    @RredCat - As far as I know we don't have EventTrigger in WinRT. – Jeremy White Nov 21 '12 at 10:36

2 Answers2

21

You need to add the following:

doubleAnimation.EnableDependentAnimation = true;

That seems to fix the problem.

MetaMapper
  • 968
  • 8
  • 22
1

I'm not sure but try to use:

Storyboard.SetTargetProperty(doubleAnimation, Button.WidthProperty);

Instead of

Storyboard.SetTargetProperty(doubleAnimation, "Width");
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
RredCat
  • 5,259
  • 5
  • 60
  • 100