我动画的边界在Silverlight调整,但是我还需要逐步消除周围的保证金(目前为50)。混合似乎不会为边距变化生成补间 - 它只是一次从50跳到0。有没有办法做到这一点?动画保证金变化在Silverlight
7
A
回答
8
问题是,边距实际上是不是依赖项对象的类型“System.Windows.Thickness”,因此Left,Top,Right和Bottom不是依赖项属性,因此无法使用DoubleAnimation进行动画处理允许补间)。
是做什么用动画的保证金是ObjectAnimation这不补。这就是为什么你会看到保证金从原来的位置跳转到新的位置。作为另一个常见示例,当您尝试在Visible和Collapsed之间设置Visibility属性的动画效果时会发生同样的情况。
你要么需要做的基于定时器的动画以动画保证金或实现自己的动画类型厚度的对象。
3
0
Here is an updated version,让您从内XAML
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace NiceCards.Animations
{
public class ThicknessAnimationX
{
public static readonly DependencyProperty ElementProperty = DependencyProperty.RegisterAttached("Element", typeof(DependencyObject), typeof(DoubleAnimation), new PropertyMetadata(new PropertyChangedCallback(OnElementPropertyChanged)));
// The time along the animation from 0-1
public static DependencyProperty TimeProperty = DependencyProperty.RegisterAttached("Time", typeof(double), typeof(DoubleAnimation), new PropertyMetadata(OnTimeChanged));
// The object being animated
public static DependencyProperty TargetProperty = DependencyProperty.RegisterAttached("Target", typeof(DependencyObject), typeof(ThicknessAnimationX), null);
public static DependencyProperty TargetPropertyProperty = DependencyProperty.RegisterAttached("TargetProperty", typeof(DependencyProperty), typeof(DependencyObject), null);
public static readonly DependencyProperty FromProperty = DependencyProperty.RegisterAttached("From", typeof(Thickness), typeof(DoubleAnimation), null);
public static readonly DependencyProperty ToProperty = DependencyProperty.RegisterAttached("To", typeof(Thickness), typeof(DoubleAnimation), null);
public static void SetElement(DependencyObject o, DependencyObject value)
{
o.SetValue(ElementProperty, value);
}
public static DependencyObject GetElement(DependencyObject o)
{
return (DependencyObject)o.GetValue(ElementProperty);
}
private static void OnElementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null)
{
DoubleAnimation doubleAnimation = (DoubleAnimation)d;
doubleAnimation.SetValue(TargetProperty, e.NewValue);
doubleAnimation.From = 0;
doubleAnimation.To = 1;
doubleAnimation.SetValue(TargetPropertyProperty, FrameworkElement.MarginProperty);
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(ThicknessAnimationX.Time)"));
Storyboard.SetTarget(doubleAnimation, doubleAnimation);
}
}
private static void OnTimeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
DoubleAnimation animation = (DoubleAnimation)sender;
double time = GetTime(animation);
Thickness from = (Thickness)sender.GetValue(FromProperty);
Thickness to = (Thickness)sender.GetValue(ToProperty);
DependencyProperty targetProperty = (DependencyProperty)sender.GetValue(TargetPropertyProperty);
DependencyObject target = (DependencyObject)sender.GetValue(TargetProperty);
target.SetValue(targetProperty, new Thickness((to.Left - from.Left) * time + from.Left,
(to.Top - from.Top) * time + from.Top,
(to.Right - from.Right) * time + from.Right,
(to.Bottom - from.Bottom) * time + from.Bottom));
}
public static double GetTime(DoubleAnimation animation)
{
return (double)animation.GetValue(TimeProperty);
}
public static void SetTime(DoubleAnimation animation, double value)
{
animation.SetValue(TimeProperty, value);
}
public static Thickness GetFrom(DoubleAnimation animation)
{
return (Thickness)animation.GetValue(FromProperty);
}
public static void SetFrom(DoubleAnimation animation, Thickness value)
{
animation.SetValue(FromProperty, value);
}
public static Thickness GetTo(DoubleAnimation animation)
{
return (Thickness)animation.GetValue(ToProperty);
}
public static void SetTo(DoubleAnimation animation, Thickness value)
{
animation.SetValue(ToProperty, value);
}
}
}
动画然后你就可以做到这一点在XAML
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="Positions">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.2"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Left">
<Storyboard>
<DoubleAnimation Duration="0:0:0.3" NiceCards:ThicknessAnimationX.To="0,0,0,0" NiceCards:ThicknessAnimationX.Element="{Binding ElementName=rectangle1}" Storyboard.TargetName="rectangle1" Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Right">
<Storyboard>
<DoubleAnimation Duration="0:0:0.3" NiceCards:ThicknessAnimationX.To="0,200,0,0" NiceCards:ThicknessAnimationX.Element="{Binding ElementName=rectangle1}" Storyboard.TargetName="rectangle1" Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle Height="100" HorizontalAlignment="Left" Margin="23,25,0,0" x:Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="200" Fill="#FF1BAA00"/>
注意,如果您不要将Target属性设置为XAML中的DoubleAnimation,您将无法在Blend中显示控件/页面。为了解决这个问题,只需添加一个假目标属性(在上面的代码我增加了透明度属性,它是一个双值),它会在运行时无论如何将覆盖
相关问题
- 1. 保证金动画?
- 2. 动画保证金/厚度
- 3. Silverlight和数据绑定属性动画(保证金)
- 4. jQuery动画和CSS保证金
- 5. jQuery滑块。保证金动画
- 6. 保证金自动变为负值
- 7. 保证金:自动自动;
- 8. 无法改变保证金
- 9. 位置div保证金顶部30px,但保留保证金在中心使用保证金自动?
- 10. 改变保证金编程在WPF/C#
- 11. CSS动态保证金?
- 12. CSS保证金自动
- 13. 浮动和保证金
- 14. 保证金左:自动在IE6和IE7
- 15. Bootstrap Row在保证金后面启动
- 16. CSS保证金
- 17. 保证金
- 18. 按钮动画仅适用于特定保证金
- 19. 保证金变化会导致多个div受到影响
- 20. 我的表单右侧的保证金奇迹般变化?
- 21. UINavigationController动画变化
- 22. 事件在保证金
- 23. WPF DataGridCell保证金
- 24. HTML表保证金
- 25. GWT UiBinder保证金
- 26. 折叠保证金
- 27. GWT:Anchor的保证金
- 28. 保证金问题
- 29. 动画BEFORE活动变化
- 30. 画布在使用保证金时不起作用
我试图在XAML中使用这个按你的例如,我在SL5中遇到了一系列错误。我已经添加了一个'的xmlns:someName'声明,但它似乎XAML不知道什么是'someName:ThicknessAnimationX'属性。 – Shaamaan 2014-01-15 09:02:57