tags:

views:

32

answers:

1

Hi I am trying to programmatically control a WPF animation but am getting the error above, could someone help with the error - not very familiar with c# - thanks

using System;

using System.Collections.Generic; using System.Linq; using System.Text; 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 WpfApplication10 { /// /// Interaction logic for Window1.xaml ///

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }
       AnimationClock clock;
       void StartAnimation()
    {
        DoubleAnimation animate = new DoubleAnimation();
        animate.To = 300;
        animate.Duration = new Duration(TimeSpan.FromSeconds(5));
        animate.RepeatBehavior = RepeatBehavior.Forever;
        clock = animate.CreateClock();
        test.ApplyAnimationClock(Ellipse.WidthProperty, clock);
    }
    void PauseAnimation()
    {
        clock = new AnimationClock();
        clock.Controller.Pause();
    }
    void ResumeAnimation()
    {
        clock.Controller.Resume();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        PauseAnimation(); 
    }

   }

}

A: 

It means you cannot create an instance of the "clock" object using "new". You can do it using the animation.CreateClock() method like the one in your StartAnimation() method. Anyway, a little tweak on your code should make it work. Hope the code below gives you an idea:

using System;
using System.Windows.Media.Animation;
using System.Windows;
using System.Collections.Generic; 
using System.Linq; 
using System.Text;
using System.Windows.Shapes;

namespace WpfApplication10 { /// /// Interaction logic for Window1.xaml ///

public partial class Window1: Window
{
    public Window1()
    {
        InitializeComponent();

        DoubleAnimation animate = new DoubleAnimation();
        animate.To = 300;
        animate.Duration = new Duration(TimeSpan.FromSeconds(5));
        animate.RepeatBehavior = RepeatBehavior.Forever;
        clock = animate.CreateClock();
    }

    AnimationClock clock;
    void StartAnimation()
    {        
        test.ApplyAnimationClock(Ellipse.WidthProperty, clock);
    }

    void PauseAnimation()
    {
        clock.Controller.Pause();
    }

    void ResumeAnimation()
    {
        clock.Controller.Resume();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        StartAnimation(); 
    }

   }
}
karmicpuppet