views:

121

answers:

2

I'm trying to animate a private variable named radius, which works. However while its changing I'm trying to execute a function which is getting to be quite of a problem.

the code i have is below, it wont run because it has the following error An object reference is required for the non-static field, method, or property 'AppPart.SetChildrenPosition()'

specifically new SetChildrenPositionDelegate(SetChildrenPosition) this part in this sentance part.Dispatcher.BeginInvoke(new SetChildrenPositionDelegate(SetChildrenPosition), new Object());

thnx to anyone able to help me.

class AppPart : Shape
{
    public string name
    { get; set; }

    public List<AppPart> parts
    { get; set; }

    private double radius
    {
        get { return (double)GetValue(radiusProperty); }
        set { SetValue(radiusProperty, value); }
    }
    public static readonly DependencyProperty radiusProperty = DependencyProperty.Register(
            "radius",
            typeof(double),
            typeof(AppPart),
            new PropertyMetadata(
            new PropertyChangedCallback(radiusChangedCallback)));



    private delegate void SetChildrenPositionDelegate();

    private void SetChildrenPosition()
    {
        //do something with radius
    }

    private static void radiusChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        AppPart part = d as AppPart;
        part.Dispatcher.BeginInvoke(new SetChildrenPositionDelegate(SetChildrenPosition), new Object());
    }

    private void AnimateRadius(double start, double end)
    {
        DoubleAnimation ani = new DoubleAnimation();
        ani.From = start;
        ani.To = end;
        ani.FillBehavior = FillBehavior.HoldEnd;
        ani.Duration = new Duration(new TimeSpan(0, 0, 0, 3, 0));
        ani.Completed += delegate
        {
            Console.WriteLine("ani ended");
        };
        this.BeginAnimation(AppPart.radiusProperty, ani);
    }
}
+1  A: 

Sure - you just need to give the delegate a target. I'd personally split it up like this:

AppPart part = d as AppPart;
// This creates a delegate instance associated with "part" - so it will
// effectively call part.SetChildrenPosition() accordingly
SetChildrenPositionDelegate action = part.SetChildrenPosition;
part.Dispatcher.BeginInvoke(action, new Object());

(Do you need the new Object() part, by the way?)

Jon Skeet
+1  A: 

Try: part.Dispatcher.BeginInvoke(() => part.SetChildrenPosition()));

Justice