首先学习C#-第1章propertytoanimate问题 [英] Head first C# - chapter 1 propertytoanimate problem
问题描述
我目前正在这门课程中学习Head First C#,在第一节课的最后一个错误仍然存在的情况下,我被困住了,我不知道如何解决它,请你们中的一些人能帮忙吗?
谢谢,真的很感激
这是有问题的代码...要设置动画的属性在其结尾
I am currently running throught this course Head First C# and I got stuck at the end of a first lesson where last error remains and I have no idea how to fix it please could some of you help?
thank you, really appreciate it
This is the problematic code... property to animate is at the end of it
Storyboard.SetTargetProperty(animation, propertyToAnimate);
整个代码:
WHOLE CODE:
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace Save_The_Human
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class MainWindow : Window
{
Random random = new Random();
private double To;
private void startButton_Click(object sender, RoutedEventArgs e)
{
AddEnemy();
}
private void AddEnemy()
{
ContentControl enemy = new ContentControl();
enemy.Template = Resources["EnemyTemplate"] as ControlTemplate;
AnimateEnemy(enemy, 0, playArea.ActualWidth - 100, "(Canvas.Left)");
AnimateEnemy(enemy, random.Next((int)playArea.ActualHeight - 100),
random.Next((int)playArea.ActualHeight - 100), "(Canvas.Top)");
playArea.Children.Add(enemy);
}
private void AnimateEnemy(ContentControl enemy, double from, double to, string propertyToAnimate)
{
Storyboard storyboard = new Storyboard() { AutoReverse = true, RepeatBehavior = RepeatBehavior.Forever };
DoubleAnimation animation = new DoubleAnimation();
{
var From = from;
To = to;
var Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6)));
};
Storyboard.SetTarget(animation, enemy);
Storyboard.SetTargetProperty(animation, propertyToAnimate);
storyboard.Children.Add(animation);
storyboard.Begin();
}
}
}
我尝试过的事情:
更改似乎引起麻烦的字符串属性
What I have tried:
Changing the string property which seems to cause the trouble
推荐答案
这更多的是注释而不是答案,但是注释部分受到限制.
您说这行给您带来麻烦:
This is more of a comment than an answer, but the comment section is a bit limited.
You say this line causes you trouble:
Storyboard.SetTargetProperty(animation, propertyToAnimate);
您需要说明哪种麻烦:
*是否存在编译器错误?
*有例外吗?
*您是否有意想不到的结果?
*什么都没发生?
另外,您没有提供足够的信息来帮助您解决问题.
您没有明确告诉我们变量propertyToAnimate是什么类型.
如果您在MSDN页面上查找 StoryBoard.SetTargetProperty [ ^ ]您可以看到该方法的签名如下所示:
You need to explain what kind of trouble:
* Is there a compiler error?
* Is there an exception?
* Do you have unexpected results?
* Does nothing happen?
Also you have not given enough information to help you solve the problem.
You don''t tell us explicitly what type the variable propertyToAnimate is.
If you look at the MSDN page for StoryBoard.SetTargetProperty[^] you can see that the signature for the method looks like this:
public static void SetTargetProperty(
Timeline element,
PropertyPath path
)
如果尝试将字符串用作第二个参数,则应得到编译器错误.
在帮助页面上的示例代码中,他们这样做如下:
If you try to use a string as the second argument, you should get a compiler error.
In the example code on the help page, they do it like this:
Storyboard.SetTargetProperty(myDoubleAnimation2, new PropertyPath("(Canvas.Top)"));
也许这会对您有所帮助.
您应该充分利用MSDN并查看示例.
而且,如果您遇到困难,则需要提供如上所述的相关信息.
Maybe this will help you, maybe not.
You should make good use of MSDN and look at the examples.
And if you get stuck you need to provide relevant information like I have stated above.
这篇关于首先学习C#-第1章propertytoanimate问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!