【发布时间】:2010-10-06 07:27:55
【问题描述】:
我在单击按钮时触发了此方法。 MyObj 是一个扩展的 Control 类型,具有两个属性 CenterX 和 CenterY,支持 dp CenterXProperty 和 CenterYProperty。
随着动画播放,MyObj 的 CenterX 和 CenterY 属性发生变化,但我看不到对象的任何移动。
private void MoveMyObjectsWithAnimation(MyObj item, Point point)
{
Duration duration = new Duration(TimeSpan.FromSeconds(3));
Storyboard sb = new Storyboard();
sb.Duration = duration;
DoubleAnimation da = new DoubleAnimation();
da.Duration = duration;
da.EasingFunction = new SineEase();
DoubleAnimation da1 = new DoubleAnimation();
da1.Duration = duration;
da1.EasingFunction = new SineEase();
Storyboard.SetTarget(da, item);
Storyboard.SetTargetProperty(da, new PropertyPath(MyObj.CenterXProperty));
da.From = item.CenterX;
da.To = point.X;
Storyboard.SetTarget(da1, item);
Storyboard.SetTargetProperty(da1, new PropertyPath(MyObj.CenterYProperty));
da1.From = item.CenterY;
da1.To = point.Y;
sb.Children.Add(da);
sb.Children.Add(da1);
sb.Begin();
}
以下是 MyObj 类中声明的属性系统。
public double CenterX
{
get
{
return (double)GetValue(CenterXProperty)+25;
}
set
{
Canvas.SetLeft(this, value - 25);
SetValue(CenterXProperty, value);
OnPropertyChanged(new PropertyChangedEventArgs("CenterX"));
}
}
public double CenterY
{
get
{
return (double)GetValue(CenterYProperty) + 25;
}
set
{
Canvas.SetTop(this, value - 25);
SetValue(CenterYProperty, value);
OnPropertyChanged(new PropertyChangedEventArgs("CenterY"));
}
}
public static readonly DependencyProperty CenterXProperty =
DependencyProperty.Register("CenterX", typeof(double), typeof(MyObj), null);
public static readonly DependencyProperty CenterYProperty =
DependencyProperty.Register("CenterY", typeof(double), typeof(MyObj), null);
我做错了什么?
【问题讨论】:
-
OnPropertyChanged 在依赖属性上是多余的。 SetValue 在幕后执行此操作。消除那些对初学者的呼吁。
标签: c#-3.0 silverlight-3.0 windows-phone-7 dependency-properties storyboard