我有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时,我收到此错误。'
有谁知道我做错了什么?
答案 0 :(得分:2)
您只能为控件分配一个RenderTransform
。幸运的是,CompositeTransform
允许您一次应用多个效果。
您的代码中有两件事需要改变。首先,替换创建变换的代码,仅创建CompositeTransform
:
var compositeTransform = new CompositeTransform();
myImage.RenderTransform = compositeTransform;
(将translateTransform
和scaleTransform
的后续引用替换为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");