WinRT/Metro代码后台动画

15

以下代码在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();
}
在WinRT/Metro中,只需要进行一次小修改就可以使其编译成功:
Storyboard.SetTargetProperty(doubleAnimation, "Width");

但是当你运行它时,什么也不会发生。

如果将属性从“Width”更改为“Opacity”(还要更改From = 0和To = 1),那么可以正常工作。

“Width”的问题是什么?


你为什么没有使用EventTrigger(http://msdn.microsoft.com/en-us/library/system.windows.eventtrigger.aspx)将所有代码移动到xaml中呢? - RredCat
2
据我所知,WinRT中没有EventTrigger。@RredCat - Jeremy White
2个回答

21

你需要添加以下内容:

doubleAnimation.EnableDependentAnimation = true;

那似乎解决了问题。


2
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj819807#dependent 解释了原因。 - legends2k

1

我不确定,但可以尝试使用:

Storyboard.SetTargetProperty(doubleAnimation, Button.WidthProperty);

不是

Storyboard.SetTargetProperty(doubleAnimation, "Width");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接