【发布时间】:2017-01-22 18:07:04
【问题描述】:
我是 C# 和编程方面的新手,正在尝试通过 Head First C# 书学习。 重写了几次代码,但仍然收到错误消息:
错误 CS1503 参数 2:无法从“字符串”转换为 'System.Windows.PropertyPath'
非常感谢您的帮助:)
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
namespace Save_The_Humans
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Random random = new Random();
public MainWindow()
{
InitializeComponent();
}
private void button_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()
{
From = from,
To = to,
Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6)))
};
Storyboard.SetTarget(animation, enemy);
Storyboard.SetTargetProperty(animation, propertyToAnimate);//THIS IS A PROBLEMATIC LINE, the "propertyToAnimate" is underlined.
storyboard.Children.Add(animation);
storyboard.Begin();
}
}
}
【问题讨论】:
标签: c#