Silverlight 中的动画边距变化
我正在 Silverlight 中对边框大小进行动画调整,但是我还需要逐渐删除其周围的边距(当前为 50)。 Blend 似乎不会为边距变化生成补间 - 它只是一次性从 50 跳到 0。有办法实现这一点吗?
I am animating a border resize in Silverlight however I also need to gradually remove the margin around it (currently 50). Blend doesn't seem to generate a tween for margin change - it just jumps from 50 to 0 in one go. Is there a way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是边距实际上是“System.Windows.Thickness”类型,它不是依赖项对象,因此 Left、Top、Right 和 Bottom 不是依赖项属性,因此无法使用 DoubleAnimation 进行动画处理(允许补间) 。
用于对边距进行动画处理的是不补间的 ObjectAnimation。这就是为什么您会看到边距从原始位置跳到新位置的原因。作为另一个常见的示例,当您尝试在 Visible 和 Collapsed 之间设置 Visibility 属性的动画时,会发生同样的情况。
您要么需要执行基于计时器的动画才能为边距设置动画,要么为厚度对象实现您自己的动画类型。
The problem is that a Margin is really of type "System.Windows.Thickness" which is NOT a dependency object, thus Left, Top, Right, and Bottom are NOT Dependency Properties and thus cannot be animated using DoubleAnimation (which allows for tweening).
What is used to animate the Margin is an ObjectAnimation which does not tween. This is why you see the margin jump from its original location to its new location. As another common example, the same happens when you try to animate the Visibility property between Visible and Collapsed.
You would either need to do timer based animation in order to animate margin or implement your own Animation type for Thickness objects.
Ben Lemmon 给出了一个优雅的解决方案: http ://blogs.msdn.com/blemmon/archive/2009/03/18/animating-margins-in-silverlight.aspx
Ben Lemmon gives an elegant solution: http://blogs.msdn.com/blemmon/archive/2009/03/18/animating-margins-in-silverlight.aspx
这是一个更新版本,它允许您从 XAML 中制作动画
然后您可以在 XAML 中执行此操作
请注意,如果您未在 XAML 中将 Target 属性设置为 DoubleAnimation,则将无法在 Blend 中显示控件/页面。要解决此问题,只需添加一个假目标属性(在上面的代码中我添加了不透明度属性,它是一个双精度值),并且无论如何它都会在运行时被覆盖
Here is an updated version that allows you to animate from within XAML
And then you can do this in XAML
Note that if you don't set a Target property to a DoubleAnimation in XAML, you won't be able to display the control/page in Blend. To fix this, just add a fake target property (in the code above I added the opacity property which is a double value), And it will be overriden at runtime anyway