2010-03-31 48 views
1

我正在尝试动画一个名为radius的私有变量,该变量起作用。然而,尽管它正在发生变化,我正试图执行一个正在成为问题的函数。使用委托,静态和依赖项属性的问题

代码我有低于,它不会运行,因为它具有以下错误 一个对象引用是所必需的非静态字段,方法或属性“AppPart.SetChildrenPosition()”

具体 新的SetChildrenPositionDelegate(SetChildrenPosition) 这部分在这份礼物 part.Dispatcher.BeginInvoke(新的SetChildrenPositionDelegate(SetChildrenPosition),new Object());

thnx给任何能够帮助我的人。

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

当然 - 你只需要给委托人一个目标。我个人把它分解成这样:

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()); 

(?你需要的new Object()一部分的方式)

1

尝试:part.Dispatcher.BeginInvoke(() => part.SetChildrenPosition()));