0
votes

I am using the following codes:

 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);
        storyboard.Children.Add(animation);
        storyboard.Begin();
    }
}

But, when I want to compile it I get the following error:

Error 1 The best overloaded method match for 'System.Windows.Media.Animation.Storyboard.SetTargetProperty(System.Windows.DependencyObject, System.Windows.PropertyPath)' has some invalid arguments c:\users\amin\documents\visual studio 2013\Projects\SaveTheHumans\SaveTheHumans\MainWindow.xaml.cs 58 13 SaveTheHumans

for the line:

Storyboard.SetTargetProperty(animation, propertyToAnimate);

Could you please tell me how I can fix the error and explain the cause of the issue as I am learning c#? Thanks.

4
It's great that you're learning C#, and that you went straight to WPF instead of winforms, but what you're doing now may be a bit too complex for you at the moment. Games are hard. Not to mention WPF not really being made for games. I'd suggest something like Monogame, so you can leverage XNA tutorials.Magus
if you're learning C#, you should be doing Hello, World! type of stuff in console application before trying to get into dynamic UIs in WPF. WPF is a complex framework not really suitable for the unexperienced, and you need to learn MVVM to use it properly.Federico Berasategui
@magus no need for winformsFederico Berasategui
Agreed, I'm saying I'm glad he didn't.Magus
Im using a book called Head First C# and in that,the first chapter is game developmentcicsosoft

4 Answers

0
votes

The system is asking you for a dependency object to validate the binding, this would be something like:

Canvas.LeftProperty

a better way of ensuring that your animation runs is to use the SetTargetName property rather than SetTarget,

here is a post showing an example Storyboard targetting multiple objects, using SetTarget method, doesn't work

Good luck on your c# endeavours.

0
votes

I don't know enough about the context of your usage, but can tell you what the error means. This is more high-level and should help anytime you see this type of error regardless of what you may be trying to implement...

It's saying that there is no signature for SetTargetProperty that takes in the quantity and types of parameters you supplied. It's telling you that the closes match that was found takes in a System.Windows.DependencyObject and a System.Windows.PropertyPath; you are passing in a DoubleAnimation and a string. You may need to pass different parameters, a different number of parameters, or cast or convert the parameters in some way before passing them. Look at the overloaded signatures for the SetTargetProperty method to determine what it can accept.

0
votes

We just need to use PropertyPath. It works for me.

Storyboard.SetTarget(animation, enemy);
PropertyPath pp = new PropertyPath(propertyToAnimate);
Storyboard.SetTargetProperty(animation, pp);
0
votes

You can use:

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

instead of:

Storyboard.SetTargetProperty(animation, propertyToAnimate);

Currently method Storyboard.SetTargetProperty() gets object PropertyPath as a second argument instead of a string (as previously). But you can get the object PropertyPath using that string new PropertyPath(string path).

You can download "WPF Guide to Head First C#" to overcome these discrepancies between WPF and XAML App: http://cdn.oreilly.com/oreilly/pdfs/hfcsharp3e_WPF_download.pdf