При запуске программы , запускается а после того как нажимаешь на кнопку старта, выдаёт данную ошибку:
Полю "MainWindow.propertyToAnimate" нигде не присваивается значение, поэтому оно всегда будет иметь значение по умолчанию null.

using System; using System.Collections.Generic; using System.Linq;
using System.Text; using System.Threading.Tasks; using System.Windows;
using System.Windows.Controls; using System.Windows.Data; using
System.Windows.Documents; using System.Windows.Input; using
System.Windows.Media; using System.Windows.Media.Imaging; using
System.Windows.Navigation; using System.Windows.Shapes; using
System.Windows.Media.Animation;
namespace WpfApp1 {
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public sealed partial class MainWindow : Window {
Random random = new Random();
private PropertyPath propertyToAnimate;
public MainWindow()
{
InitializeComponent();
}
public Duration Duration { get; private set; }
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 properetyToAnimate)
{
Storyboard storyboard = new Storyboard() { AutoReverse = true, RepeatBehavior = RepeatBehavior.Forever };
DoubleAnimation animation = new DoubleAnimation();
{
double From = from;
double To = to;
Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6)));
};
Storyboard.SetTarget(animation, enemy);
PropertyPath propertyToAnimate = this.propertyToAnimate;
Storyboard.SetTargetProperty(animation, propertyToAnimate);
storyboard.Children.Add(animation);
storyboard.Begin();
}
}
}