不能动画TranslateTransform.带有代码的XProperty



我有一个方法,使我能够动画对象与DoubleAnimation:

public void animDouble(DependencyObject target, DependencyProperty property, double to, TimeSpan duration, double? from = null, TimeSpan? beginTime = null, IEasingFunction e = null)
{
    DoubleAnimation animation = new DoubleAnimation();
    animation.To = to;
    if (beginTime == null)
        beginTime = TimeSpan.FromSeconds(0);
    if (from != null)
        animation.From = from;

    animation.BeginTime = beginTime;
    animation.Duration = duration;

    if (e != null)
        animation.EasingFunction = e;
    //start animating
    Storyboard.SetTarget(animation, target);  // what object will be animated?
    Storyboard.SetTargetProperty(animation, new PropertyPath(property)); // what property will be animated
    Storyboard sb = new Storyboard();
    sb.Children.Add(animation);
    sb.Begin();
}

例如,如果我有一个名为br1的边界我想为它的高度设置动画我将调用方法为:

animDouble(br1, FrameworkElement.HeightProperty, 150, TimeSpan.FromSeconds(5));

如果我想让它的宽度有动画效果,我会输入:

animDouble(br1, FrameworkElement.WidthProperty, 150, TimeSpan.FromSeconds(5));

我也可以用同样的方法动画它的可见性。

由于某些原因,我不能动画它的x属性,以便沿着x轴或y轴平移它。当我调用方法为:

a.animDouble(br1, TranslateTransform.XProperty, 150, TimeSpan.FromSeconds(5));

边框没有动画。我也没有得到任何错误。

不知怎么的,我会期望一个错误,好吧,无论如何,Border没有这样的属性,如果你想移动你的控制,你需要设置边界的RenderTransformLayoutTransformTranslateTransform,然后你可以传递转换本身到方法作为目标。

(整个故事板是非常多余的,因为你只有一个动画,你可以直接调用目标本身的BeginAnimation )

这与注册名称有关。我在这里找到了一个链接

我不知道registerName方法做什么,但我想我需要它。从页面上,我设法得到了基本的动画。有时我不能同时激活两个东西。如果你对这个方法感兴趣,可以看看这个问题。我认为这是一个非常好的类,可以用代码创建动画。将命名空间复制到visual studio,并复制我发布的第一个示例,以便您可以看到它是如何工作的。

最新更新