Problem using delegates, static, and dependencyproperties
- by red-X
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);
}
}