簡體   English   中英

WPF - 使用Storyboard在C#中的EllipseGeometry動畫

[英]WPF - EllipseGeometry animation in C# using a Storyboard

我需要使用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放在故事板中,但我不知道如何。

謝謝。

如果您使用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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM