WinRT/Metro Animation in code-behind

梦想与她 提交于 2019-12-08 15:55:31

问题


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


回答1:


You need to add the following:

doubleAnimation.EnableDependentAnimation = true;

That seems to fix the problem.




回答2:


I'm not sure but try to use:

Storyboard.SetTargetProperty(doubleAnimation, Button.WidthProperty);

Instead of

Storyboard.SetTargetProperty(doubleAnimation, "Width");


来源:https://stackoverflow.com/questions/11260529/winrt-metro-animation-in-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!