来自代码隐藏的 TranslateTransform 不起作用

问题描述 投票:0回答:1

我想将代码隐藏中的动画应用到各种控件。对于应用于图像控件的不透明动画来说,这基本上工作得很好。但如果我想对 TranslateTransform 动画做同样的事情,那么什么也不会发生(它应该从侧面滑入)。当我检查教程时,我认为我做得正确,但一定有一个我没有看到的错误。

这是我的代码,任何好主意都会受到赞赏。

DoubleAnimation animation = new DoubleAnimation();
animation.From = -100;
animation.To = 0;
animation.Duration = TimeSpan.FromSeconds(1);
animation.EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseOut };

Storyboard.SetTarget(animation, imageContainer);
Storyboard.SetTargetProperty(animation, new PropertyPath(TranslateTransform.XProperty));
            
Storyboard sb = new Storyboard();
sb.Children.Add(animation);

sb.Begin();
c# animation storyboard
1个回答
0
投票

好吧,我终于找到了使用不同方法的解决方案。我不确定为什么它不能与上面的代码一起使用,即使我将动画不应用于 imagecontainer 而是应用于 imageContainer.RenderTransform 。 然而这段代码正在运行:

imageContainer.RenderTransform = new TranslateTransform(-200, 0);
Duration duration = new Duration(TimeSpan.FromSeconds(0.5));
DoubleAnimation anim = new DoubleAnimation(0, duration);
anim.EasingFunction = new ExponentialEase() { EasingMode = EasingMode.EaseOut };

imageContainer.RenderTransform.BeginAnimation(TranslateTransform.XProperty, anim);
© www.soinside.com 2019 - 2024. All rights reserved.