【问题标题】:Argument 2: cannot convert from 'string' to 'System.Windows.PropertyPath'参数 2:无法从 'string' 转换为 'System.Windows.PropertyPath'
【发布时间】: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#


    【解决方案1】:

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

    解决方法很简单:

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

    来源:MSDN

    【讨论】:

    • 谢谢@user3185569!看起来已解决,但现在收到消息 Storyboard.SetTargetProperty(animation,new PropertyPath("propertyToAnimate"));然后试图运行。有什么建议吗?非常感谢!
    • @Slava 应该是new PropertyPath(propertyToAnimate),不带双引号。
    【解决方案2】:

    这样改代码试试

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

    由于第二个参数是 PropertyPath 类型而不是字符串

    【讨论】:

    • 谢谢@Sreemat!看起来已解决,但现在收到消息 Storyboard.SetTargetProperty(animation,new PropertyPath("propertyToAnimate"));然后试图运行。有什么建议吗?非常感谢! ——
    • 无法让你理解@Slava
    • 在此处@Slava 发布 propertyToAnimate 中的值
    • 不要作为字符串传递 new PropertyPath("propertyToAnimate") 作为参数传递 new PropertyPath(propertyToAnimate) @Slava
    • 我发现的所有内容是:属性值,类型:SystemInvalidOperationExeption 抱歉,我的解释如此混乱,我是 C# 的初学者,从 Head First Book 开始学习。也许我应该以常规方式学习,而不是通过重写其他人的代码?
    猜你喜欢
    • 2021-06-23
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多