在WinRT(XAML)中使用DoubleAnimation代码

时间:2014-02-25 23:05:30

标签: c# wpf xaml animation winrt-xaml

我有一些问题。这是我的代码背后:

var s = new Storyboard();
var dAnimation = new DoubleAnimation();
dAnimation.RepeatBehavior = RepeatBehavior.Forever;
dAnimation.AutoReverse = true;
dAnimation.EnableDependentAnimation = true;
dAnimation.From = 200;
dAnimation.To = 300;
Storyboard.SetTarget(dAnimation, viewBox);
Storyboard.SetTargetProperty(dAnimation, "Width");
dAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(2000));
s.Children.Add(dAnimation);
s.Begin();

viewBox位于Canvas,只有几个属性:Canvas.Left,Canvas.Top,Height和Width。代码隐藏的代码与Opacity一起使用效果很好,但不能与WidthHeight一起使用。输入用户指针时的代码隐藏工作。我想要没有触发器就做。找到了Silverlight和WPF的一些解决方案:

WinRT/Metro Animation in code-behind

他们没有工作。我不明白为什么它与不透明度一起使用并且不适用于WidthHeight任何想法?

2 个答案:

答案 0 :(得分:2)

您无法使用DoubleAnimation为Width设置动画,您需要使用DoubleAnimationUsingKeyFrames。

var animation = new DoubleAnimationUsingKeyFrames();
var frame = new EasingDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(2000)), Value = 300});
animation.KeyFrames.Add(frame);
Storyboard.SetTargetProperty(animation, "(FrameworkElement.Width)");
Storyboard.SetTarget(animation, viewBox);
Storyboard.Children.Add(animation);

答案 1 :(得分:0)

我没有检查ViewBox,但我可以使用DoubleAnimation for Grid。

代码如下所示

var s = new Storyboard();
var widthAnim = new DoubleAnimation()
{
    To = w,
    Duration=new Duration(TimeSpan.FromMilliseconds(duration))
};
widthAnim.EnableDependentAnimation = true;
Storyboard.SetTarget(widthAnim,elem);
Storyboard.SetTargetProperty(widthAnim,"Width");
s.Children.Add(widthAnim);
s.Begin();