对Windows Phone应用程序的C#上的图像应用转换
我正在做一个小游戏,在这个游戏中,我需要使用翻译转换使图像从初始位置转到另一个位置 问题是:我不知道如何继续应用翻译 这是用来生成我的图像的代码对Windows Phone应用程序的C#上的图像应用转换,c#,windows-phone,transform,translation,C#,Windows Phone,Transform,Translation,我正在做一个小游戏,在这个游戏中,我需要使用翻译转换使图像从初始位置转到另一个位置 问题是:我不知道如何继续应用翻译 这是用来生成我的图像的代码 Image myImage1 = new Image(); myImage1.Source = new BitmapImage(new Uri("/Images/image1.png", UriKind.Relative)); myImage1.Name = "image" + index++.ToString(); myImage1.Tap += m
Image myImage1 = new Image();
myImage1.Source = new BitmapImage(new Uri("/Images/image1.png", UriKind.Relative));
myImage1.Name = "image" + index++.ToString();
myImage1.Tap += myImage_Tap;
Canvas.SetLeft(image, 200);
Canvas.SetTop(image, 600);
gameArea.Children.Add(image);
谢谢您的时间。您有两种选择来移动图像。第一种方法是像代码所示那样使用画布,但必须确保元素实际位于画布中。你的“游戏区”是画布吗?如果不是,代码将无法工作。另一个选项是使用变换
var myImage1 = new Image
{
Source = new BitmapImage(new Uri("/Images/image1.png", UriKind.Relative)),
Name = "image" + index++.ToString(),
Tap += myImage_Tap,
RenderTransform = new TranslateTransform
{
X = 200,
Y = 600
}
};
gameArea.Children.Add(image);
现在游戏区可以是任何类型的面板,它将工作
请注意,使用画布时,顶部和左侧将从画布的左上角开始。使用变换时,X和Y将相对于最初绘制元素的位置
UPDATE--Helper类使用画布制作简单动画
public sealed class ElementAnimator
{
private readonly UIElement _element;
public ElementAnimator(UIElement element)
{
if (null == element)
{
throw new ArgumentNullException("element", "Element can't be null.");
}
_element = element;
}
public void AnimateToPoint(Point point, int durationInMilliseconds = 300)
{
var duration = new Duration(TimeSpan.FromMilliseconds(durationInMilliseconds));
var easing = new BackEase
{
Amplitude = .3
};
var sb = new Storyboard
{
Duration = duration
};
var animateLeft = new DoubleAnimation
{
From = Canvas.GetLeft(_element),
To = point.X,
Duration = duration,
EasingFunction = easing,
};
var animateTop = new DoubleAnimation
{
From = Canvas.GetTop(_element),
To = point.Y,
Duration = duration,
EasingFunction = easing,
};
Storyboard.SetTargetProperty(animateLeft, "(Canvas.Left)");
Storyboard.SetTarget(animateLeft, _element);
Storyboard.SetTargetProperty(animateTop, "(Canvas.Top)");
Storyboard.SetTarget(animateTop, _element);
sb.Children.Add(animateLeft);
sb.Children.Add(animateTop);
sb.Begin();
}
}
这就是我对你的代码所做的,我只取了这一部分,并将其作为一个函数:
public void AnimateToPoint(UIElement image, Point point, int durationInMilliseconds = 300)
{
var duration = new Duration(TimeSpan.FromMilliseconds(durationInMilliseconds));
var sb = new Storyboard
{
Duration = duration
};
var animateTop = new DoubleAnimation
{
From = Canvas.GetTop(image),
To = point.Y,
Duration = duration
};
Storyboard.SetTargetProperty(animateTop, new PropertyPath("Canvas.Top")); // You can see that I made some change here because I had an error with what you gave me
Storyboard.SetTarget(animateTop, image);
sb.Children.Add(animateTop);
sb.Begin();
}
然后我打电话给:
Point myPoint = new Point(leftpos, 300); // leftpos is a variable generated randomly
AnimateToPoint(myImage, myPoint);
所以现在仍然有一个错误,它出现在指令“sb.Begin;”中。
它说:无法解析指定对象上的TargetProperty Canvas.Top。
我真的不知道该怎么做。
谢谢你以前的回答 你在那里做的是在一个不同的点上生成一个图像,而不是我想要的点。我想要的是在点{X=200,Y=600}上生成我的图像,并使其移动到顶部,使Y从600到0。我希望动画是可见的,而不仅仅是在另一点上生成图像。有没有办法使其成为动画并为其添加持续时间?@TahaBenabdallah查看我的更新答案。我已经包括了一个类,我刚才做了帮助画布动画。你需要使用故事板。如果您不使用画布,故事板也可以为您的变换值设置动画。@ManOVision您能看一下吗?抱歉,耽搁了,我从未收到通知。问题在于PropertyPath语法。注意我的示例中的额外括号。语法很奇怪,我总是要试几次才能把它弄对。如果你能用xaml声明故事板,那就容易多了,但是因为你在制作一个游戏,元素可能是动态的,所以你需要在PropertyPath中使用正确的字符串语法。我明天在我的电脑上测试你的代码。