2010-03-31 51 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()));