2011-08-08 41 views
0

我有使我对动画有一个DoubleAnimation是做对象的方法:不能够与代码动画TranslateTransform.XProperty

public void animDouble(DependencyObject target, DependencyProperty property, double to, TimeSpan duration, double? from = null, TimeSpan? beginTime = null, IEasingFunction e = null) 
{ 

    DoubleAnimation animation = new DoubleAnimation(); 
    animation.To = to; 

    if (beginTime == null) 
     beginTime = TimeSpan.FromSeconds(0); 

    if (from != null) 
     animation.From = from; 


    animation.BeginTime = beginTime; 
    animation.Duration = duration; 


    if (e != null) 
     animation.EasingFunction = e; 

    //start animating 
    Storyboard.SetTarget(animation, target); // what object will be animated? 
    Storyboard.SetTargetProperty(animation, new PropertyPath(property)); // what property will be animated 
    Storyboard sb = new Storyboard(); 
    sb.Children.Add(animation); 
    sb.Begin(); 
} 

,所以如果我呼吁BR1例如,我寄宿生想动画它的高度,我会调用该方法为:

animDouble(br1, FrameworkElement.HeightProperty, 150, TimeSpan.FromSeconds(5)); 

,如果我想动画它的宽度,我会做的事:

animDouble(br1, FrameworkElement.WidthProperty, 150, TimeSpan.FromSeconds(5)); 

我也可以用相同的方法动画它的可见性。

由于某些原因,我无法为其x属性进行动画制作,以便将其沿x轴或y轴进行转换。当我将该方法称为:

a.animDouble(br1, TranslateTransform.XProperty, 150, TimeSpan.FromSeconds(5)); 

寄宿生不动画。我没有任何错误。

+2

请不要只打你的标题“C#”。这就是标签的用途。 –

回答

0

不知怎的,我本来期望一个错误,好吧,反正Border拥有没有这样的属性,如果你想将您控制您需要在边界RenderTransformLayoutTransform设置为TranslateTransform,那么你可以通过将自己转变为目标的方法。

整个故事板是非常多余的,因为你只有一个动画,你可以调用BeginAnimation目标本身

0

它曾与注册名的人。我发现一个链接here

我不知道registerName方法是什么,但我想我需要它。从页面我设法得到基本的动画。我有时无法一次动画两件事有时候。 if you are interested in seeing the method take a look tat this question。我认为这是一个非常漂亮的类,可以使用代码创建动画。将名称空间复制到Visual Studio并复制我发布的第一个示例,以便您可以看到它的工作原理。

+0

这与它无关,你似乎对** [动画](http://msdn.microsoft.com/en-us/library/ms752312.aspx)**的工作方式感到非常困惑...... –