【问题标题】:Using DoubleAnimation code behind in WinRT (XAML)在 WinRT (XAML) 中使用 DoubleAnimation 代码
【发布时间】:2014-02-25 23:05:30
【问题描述】:

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

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 任何想法?

【问题讨论】:

  • 您应该立即在您的问题中链接到此答案,因为我(和其他人)没有写出相同的答案。

标签: c# wpf xaml animation winrt-xaml


【解决方案1】:

您不能使用 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);

【讨论】:

    【解决方案2】:

    我没有检查 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 2012-06-27
      • 2013-09-06
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多