c# - WPF - 在 C# 中使用 Storyboard 的 EllipseGeometry 动画

标签 c# wpf storyboard scaletransform wpf-animation

我需要使用 ScaleTransform 在 C# 中缩放 EllipseGeometry,但它不起作用。 这是代码:

XAML:

<Image x:Name="rock" Stretch="None">
    <Image.Clip>
        <EllipseGeometry x:Name="rockClip" RadiusX="100" RadiusY="100" Center="200,150">
            <EllipseGeometry.Transform>
                    <ScaleTransform/>
            </EllipseGeometry.Transform>
        </EllipseGeometry>
    </Image.Clip>
</Image>

C#:

DoubleAnimation scaleX = new DoubleAnimation();
scaleX.BeginTime = TimeSpan.FromMilliseconds(fromMills);
scaleX.Duration = new Duration(TimeSpan.FromMilliseconds(2000));
scaleX.From = 0.0;
scaleX.To = 1.0;
scaleX.SetValue(Storyboard.TargetProperty, rockClip);
scaleX.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("(EllipseGeometry.Transform).(ScaleTransform.ScaleX)"));

DoubleAnimation scaleY = new DoubleAnimation();
scaleY.BeginTime = TimeSpan.FromMilliseconds(fromMills);
scaleY.Duration = new Duration(TimeSpan.FromMilliseconds(2000));
scaleY.From = 0.0;
scaleY.To = 1.0;
scaleY.SetValue(Storyboard.TargetProperty, rockClip);
scaleY.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("(EllipseGeometry.Transform).(ScaleTransform.ScaleY)"));

Storyboard storyboard = new Storyboard();
storyboard.Children.Add(scaleX);
storyboard.Children.Add(scaleY);
storyboard.Completed += storyboard_Completed;
animation.Begin();

storyboard_Completed 事件已触发,但 EllipseGeometry 上没有动画。

问题出在哪里?

我只能通过这种方式为 EllipseGeometry 设置动画:

DoubleAnimation scale = new DoubleAnimation();
scale.From = 0;
scale.To = 40;
scale.Duration = new Duration(TimeSpan.FromMilliseconds(5000));
rockClip.BeginAnimation(EllipseGeometry.RadiusXProperty, scale);
rockClip.BeginAnimation(EllipseGeometry.RadiusYProperty, scale);

我需要将此 DoubleAnimation 放入 Storyboard 中,但我不知道该怎么做。

谢谢。

最佳答案

如果您使用 Image 控件(而不是 Geometry)作为目标元素,它会起作用:

var scaleX = new DoubleAnimation();
scaleX.BeginTime = TimeSpan.FromMilliseconds(fromMills);
scaleX.Duration = TimeSpan.FromSeconds(2);
scaleX.From = 0.0;
scaleX.To = 1.0;
Storyboard.SetTarget(scaleX, rock);
Storyboard.SetTargetProperty(scaleX, new PropertyPath("Clip.Transform.ScaleX"));

var scaleY = new DoubleAnimation();
scaleY.BeginTime = TimeSpan.FromMilliseconds(fromMills);
scaleY.Duration = TimeSpan.FromSeconds(2);
scaleY.From = 0.0;
scaleY.To = 1.0;
Storyboard.SetTarget(scaleY, rock);
Storyboard.SetTargetProperty(scaleY, new PropertyPath("Clip.Transform.ScaleY"));

var storyboard = new Storyboard();
storyboard.Children.Add(scaleX);
storyboard.Children.Add(scaleY);
storyboard.Begin();

然而,最简单的方法可能是命名 ScaleTransform

<EllipseGeometry.Transform>
    <ScaleTransform x:Name="scale"/>
</EllipseGeometry.Transform>

然后像这样运行动画:

var scaleAnimation = new DoubleAnimation
{
    BeginTime = TimeSpan.FromMilliseconds(fromMills),
    Duration = TimeSpan.FromSeconds(2),
    From = 0.0,
    To = 1.0
};

scale.BeginAnimation(ScaleTransform.ScaleXProperty, scaleAnimation);
scale.BeginAnimation(ScaleTransform.ScaleYProperty, scaleAnimation);

编辑:为了通过 Storyboard 为 RadiusXRadiusY 属性设置动画,您可以这样写:

var radiusXAnimation = new DoubleAnimation();
radiusXAnimation.BeginTime = TimeSpan.FromMilliseconds(fromMills);
radiusXAnimation.Duration = TimeSpan.FromSeconds(2);
radiusXAnimation.From = 0;
radiusXAnimation.To = 100;
Storyboard.SetTarget(radiusXAnimation, rock);
Storyboard.SetTargetProperty(radiusXAnimation, new PropertyPath("Clip.RadiusX"));

var radiusYAnimation = new DoubleAnimation();
radiusYAnimation.BeginTime = TimeSpan.FromMilliseconds(fromMills);
radiusYAnimation.Duration = TimeSpan.FromSeconds(2);
radiusYAnimation.From = 0;
radiusYAnimation.To = 100;
Storyboard.SetTarget(radiusYAnimation, rock);
Storyboard.SetTargetProperty(radiusYAnimation, new PropertyPath("Clip.RadiusY"));

var storyboard = new Storyboard();
storyboard.Children.Add(radiusXAnimation);
storyboard.Children.Add(radiusYAnimation);
storyboard.Begin();

关于c# - WPF - 在 C# 中使用 Storyboard 的 EllipseGeometry 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27501122/

相关文章:

objective-c - iPhone 6 Plus 上的 UISplitViewController 旋转大师 Master

c# - 如何处理大量的静态变量和集合?

c# - 分部 View 异步加载,不加载,不报错

c# - 希伯来语问题 - MySql,c#

c# - 在 ProgressBar 控件模板中为 WPF ImageBrush 设置动画

c# - 用连字符包装单词不起作用

c# - 最佳实践问题 : Datatable and representation in objects

c# - 无法分配给属性 'Windows.UI.Xaml.FrameworkElement.MinHeight'

iPhone Storyboard导航

iphone - 你如何从一个 Storyboard转到另一个 Storyboard?不在 Storyboard 中查看