0

Can anyone explain to me what I am doing wrong here? I am most certainly a beginner at C# and I do not want to leave it up to XAML when it comes to animating this image on the screen.

        _sb = new Storyboard();

        DoubleAnimation moveXAnim = new DoubleAnimation();
        moveXAnim.Duration = TimeSpan.FromMilliseconds(5000);
        moveXAnim.From = 0;
        moveXAnim.To = 300;

        _sb.Children.Add(moveXAnim);

        Storyboard.SetTarget(moveXAnim, Person);
        Storyboard.SetTargetProperty(moveXAnim, "(Canvas.Left)");

        _sb.Begin();

All the examples I find are XAML.

  • As an aside, "Leaving it up to Xaml", the Xaml processor simply converts Xaml markup to C# objects and processes them accordingly. Anything one can do in Xaml, one can do in code behind. – ΩmegaMan Mar 10 '14 at 00:52
  • 1
    `I am most certainly a beginner at C# and I do not want to leave it up to XAML` - If you're a beginner in C# you shold be doing `Hello, World!` type of stuff in console applications before trying to get into complex animation stuff. And no, UIs in XAML-based technologies are not supposed to be done in procedural code. **That's what XAML is for**. If you want to do this properly I suggest you take the time to learn the basics of both C# and XAML and only then go ahead and try to do animations and the like. – Federico Berasategui Mar 10 '14 at 01:07
  • Put your image to animate in a parent canvas. – AbinZZ Mar 10 '14 at 04:35
  • You should really adop the XAML way. It's what XAML is meant for. Think it like this : Isn't it obvious it is to be done in XAML if all the answers you find are XAML-based? – VasileF Mar 11 '14 at 07:45
  • http://stackoverflow.com/questions/4214155/wpf-easiest-way-to-move-image-to-x-y-programmatically – Heena Mar 11 '14 at 11:01

1 Answers1

0

What makes you think it does not work? I assume that Person is a UIElement.

Is your Person element contained inside a Canvas? The example below should work for your code>

    <Canvas>
        <TextBlock
            Text="Hi"
            x:Name="Person" />
    </Canvas>
Kasper Holdum
  • 12,993
  • 6
  • 45
  • 74