我正在尝试为名为radius的私有变量设置动画,该变量有效。然而,当它改变时,我正在尝试执行一个非常有问题的功能。
我的代码如下,它不会运行,因为它有以下错误 非静态字段,方法或属性'AppPart.SetChildrenPosition()'
需要对象引用具体地 新的SetChildrenPositionDelegate(SetChildrenPosition) 这一部分在这个序列中 part.Dispatcher.BeginInvoke(new SetChildrenPositionDelegate(SetChildrenPosition),new Object());
对任何能够帮助我的人都是如此。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);
}
}
答案 0 :(得分: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 :(得分:1)
尝试:part.Dispatcher.BeginInvoke(() => part.SetChildrenPosition()));