开发者

Problem using delegates, static, and dependencyproperties

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 SetChildrenPositionDele开发者_如何学Gogate(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);
    }
}


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?)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜