如何使用 DoubleAnimation 缩小图像

How to scale down Image with DoubleAnimation

我有 DoubleAnimations 可以将我的 Image 元素从一个地方移动到另一个地方,但是当我尝试添加另一个 DoubleAnimation 来缩小图像时,我的代码中断,这是我的代码:

private Point GetAbsolutePosition(UIElement element)
{
   var ttv = element.TransformToVisual(Window.Current.Content);
   return ttv.TransformPoint(new Point(0, 0));
}

private void MoveToSolarSystem_Start()
{
        var imageToMoveToPoint= GetAbsolutePosition(imageToMoveTo);
        var myImagePoint= GetAbsolutePosition(myImage);
        var offsetX = imageToMoveToPoint.X - myImagePoint.X;
        var offsetY = imageToMoveToPoint.Y - myImagePoint.Y;

        var translateTransform = new TranslateTransform();
        myImage.RenderTransform = translateTransform;

        ScaleTransform scaleTransform = new ScaleTransform();
        myImage.RenderTransform = scaleTransform;

        var scaleAnimation = new DoubleAnimation()
        {
            From = 0,
            To = 100,
            Duration = TimeSpan.FromSeconds(2)
        };

        var animationX = new DoubleAnimation()
        {
            From = 0,
            To = offsetX + 50,
            Duration = TimeSpan.FromSeconds(2)
        };

        var animationY = new DoubleAnimation()
        {
            From = 0,
            To = offsetY + 50,
            Duration = TimeSpan.FromSeconds(2)
        };



        Storyboard.SetTarget(animationX, translateTransform);
        Storyboard.SetTargetProperty(animationX, "X");


        Storyboard.SetTarget(animationY, translateTransform);
        Storyboard.SetTargetProperty(animationY, "Y");

        Storyboard.SetTarget(scaleAnimation, scaleTransform);
        Storyboard.SetTargetProperty(scaleAnimation, "X");


        var storyboard = new Storyboard();

        storyboard.Children.Add(animationX);
        storyboard.Children.Add(animationY);
        storyboard.Children.Add(scaleAnimation);
        storyboard.Begin();

}

我在调试“无法解析指定对象上的 TargetProperty X”时收到此错误。

有谁知道我做错了什么吗?

您只能将一个 RenderTransform 分配给一个控件。幸运的是,CompositeTransform 允许您一次应用多个效果。

您的代码中有两处需要更改。首先,替换创建转换的代码,只创建 CompositeTransform:

var compositeTransform = new CompositeTransform();
myImage.RenderTransform = compositeTransform;

(将 translateTransformscaleTransform 的后续引用替换为 compositeTransform

第二件事是 CompositeTransform 上的属性名称不同。所以你需要调整你的动画:

 Storyboard.SetTarget(animationX, compositeTransform);
 Storyboard.SetTargetProperty(animationX, "TranslateX");

 Storyboard.SetTarget(animationY, compositeTransform);
 Storyboard.SetTargetProperty(animationY, "TranslateY");

 Storyboard.SetTarget(scaleAnimation, compositeTransform);
 Storyboard.SetTargetProperty(scaleAnimation, "ScaleX");