2
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Random random = new Random();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void startButton_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, new PropertyPath(propertyToAnimate));
            storyboard.Children.Add(animation);
              //problem
            storyboard.Begin();
        }

    }

$ My error(An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll

Additional information: Cannot resolve all property references in the property path '(canvas.left)'. Verify that applicable objects support the properties.)

can somebody help me with this?

4
  • You probably need the full namespace Commented Jan 8, 2017 at 0:15
  • what you mean with full namespace? Commented Jan 8, 2017 at 10:12
  • Try using the case that reflects the actual type/property, eg (Canvas.Left). Commented Jan 8, 2017 at 15:07
  • See also msdn.microsoft.com/en-us/library/… Commented Jan 8, 2017 at 15:10

1 Answer 1

1

Property names are case-sensitive. It should be Canvas.Left and Canvas.Top:

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You are my hero!!! Thnx always forgot look on it... I was thinking I put on redactor some wrong canvas scale or property and focus only it, instead looking what =( Thnx one more time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.