C# 参数2:无法从';字符串';至';System.Windows.PropertyPath';

C# 参数2:无法从';字符串';至';System.Windows.PropertyPath';,c#,C#,我对C#和编程非常陌生,我试图学习第一本C#书。 已多次重写代码,但仍收到错误消息: 错误CS1503参数2:无法从“字符串”转换为 'System.Windows.PropertyPath' 非常感谢您的帮助:) 使用系统; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Media.Animation; 名称空间拯救人类 { /// ///MainWindow.xaml的交互逻辑 /// 公共部分类主窗口:窗口

我对C#和编程非常陌生,我试图学习第一本C#书。 已多次重写代码,但仍收到错误消息:

错误CS1503参数2:无法从“字符串”转换为 'System.Windows.PropertyPath'

非常感谢您的帮助:)

使用系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Media.Animation;
名称空间拯救人类
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
随机=新随机();
公共主窗口()
{
初始化组件();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
AddEnemy();
}
私有无效加法器()
{
ContentControl敌人=新ContentControl();
defey.Template=资源[“EnemyTemplate”]作为ControlTemplate;
AnimateEnemy(敌人,0,playrea.ActualWidth-100,”(Canvas.Left)”;
AnimateEnemy(敌人,随机。下一个((int)playrea.ActualHeight-100),
random.Next((int)playrea.ActualHeight-100),“(Canvas.Top)”;
游戏区。儿童。添加(敌人);
}
私有void AnimateEnemy(ContentControl、double-from、double-to、string-propertyToAnimate)
{
故事板故事板=新故事板{AutoReverse=true,RepeatBehavior=RepeatBehavior.Forever};
DoubleAnimation=新的DoubleAnimation()
{
From=From,
To=To,
持续时间=新的持续时间(TimeSpan.FromSeconds(random.Next(4,6)))
};
故事板。设定目标(动画,敌人);
Storyboard.SetTargetProperty(动画,propertyToAnimate);//这是一条有问题的线,“propertyToAnimate”带下划线。
故事板。儿童。添加(动画);
故事板。开始();
}
}
}

您没有为方法提供正确的参数类型:当您提供类型为
string
的对象时,第二个参数的类型为
PropertyPath

解决方案非常简单:

Storyboard.SetTargetProperty(animation, new PropertyPath(propertyToAnimate));

来源:

您没有为方法提供正确的参数类型:当您提供类型为
string
的对象时,第二个参数的类型为
PropertyPath

解决方案非常简单:

Storyboard.SetTargetProperty(animation, new PropertyPath(propertyToAnimate));

来源:

像这样更改代码,然后重试

 Storyboard.SetTargetProperty(animation,new PropertyPath(propertyToAnimate));

因为第二个参数是PropertyPath type not string

请像这样更改代码并重试

 Storyboard.SetTargetProperty(animation,new PropertyPath(propertyToAnimate));

因为第二个参数是PropertyPath类型而不是string

谢谢@user3185569!它看起来已解决,但现在正在获取消息Storyboard.SetTargetProperty(动画,新属性路径(“propertyToAnimate”);然后试着跑。有什么建议吗?非常感谢@Slava它应该是
新的PropertyPath(propertyToAnimate)
,没有双引号。谢谢,@user3185569!它看起来已解决,但现在正在获取消息Storyboard.SetTargetProperty(动画,新属性路径(“propertyToAnimate”);然后试着跑。有什么建议吗?非常感谢@Slava它应该是
新的PropertyPath(propertyToAnimate)
,没有双引号。谢谢,@Sreemat!它看起来已解决,但现在正在获取消息Storyboard.SetTargetProperty(动画,新属性路径(“propertyToAnimate”);然后试着跑。有什么建议吗?非常感谢无法让您理解@slavast此处propertyToAnimate中的值@slava不要作为字符串传递new PropertyPath(“propertyToAnimate”)将其作为参数传递new PropertyPath(propertyToAnimate)@slava我发现的所有内容是:Property value,Type:SystemInvalidOperationExeption抱歉解释如此混乱,我对C#非常熟悉,从第一本书开始学习。也许我应该以常规的方式学习,而不是重新编写别人的代码?谢谢,@Sreemat!它看起来已解决,但现在正在获取消息Storyboard.SetTargetProperty(动画,新属性路径(“propertyToAnimate”);然后试着跑。有什么建议吗?非常感谢无法让您理解@slavast此处propertyToAnimate中的值@slava不要作为字符串传递new PropertyPath(“propertyToAnimate”)将其作为参数传递new PropertyPath(propertyToAnimate)@slava我发现的所有内容是:Property value,Type:SystemInvalidOperationExeption抱歉解释如此混乱,我对C#非常熟悉,从第一本书开始学习。也许我应该以常规的方式学习,而不是重新编写别人的代码?