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.
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