如何在 Silverlight 4 中等待状态更改转换完成?
我需要更改控件的状态,然后执行一些操作。具体来说,我想在隐藏控件之前运行动画。我想做这样的事情:
VisualStateManager.GoToState(control, "Hidden", true); // wait until the transition animation is finished
ParentControl.Children.Remove(control);
问题是过渡动画是异步运行的,因此在动画开始后控件立即从可视化树中删除。
那么如何等待动画结束呢?
I need to change state of a control and then do some action. To be specific, I want to run an animation before a control is hidden. I would like to do something like that:
VisualStateManager.GoToState(control, "Hidden", true); // wait until the transition animation is finished
ParentControl.Children.Remove(control);
The problem is that the transition animation is run asynchronously and thus the control is removed from the visual tree right after the animation is started.
So how do I wait for the animation to finish?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将 Storyboard.Completed 事件处理程序附加到 Storyboard 或将 VisualStateGroup.CurrentStateChanged 事件处理程序附加到 VisualStateGroup:
}
You can attach a Storyboard.Completed event handler to the Storyboard or attach a VisualStateGroup.CurrentStateChanged event handler to the VisualStateGroup:
}
事实上,可以在代码中附加 Completed 处理程序:
此线程的示例: http ://forums.silverlight.net/forums/p/38027/276746.aspx
也使用附加行为在实时项目中为我工作!有点烦人的是,我必须对根 UserControl (在 VisualStateManager.GoToState 中使用)和 LayoutRoot 使用单独的依赖属性来获取实际的 VisualStateGroup 集合。
It is in fact possible to attach the Completed handler in code:
Example from this thread: http://forums.silverlight.net/forums/p/38027/276746.aspx
Working for me in live project using attached Behavior too! Slightly annoying though that I had to use separate dependency properties for the root UserControl (to use in VisualStateManager.GoToState) and the LayoutRoot to get the actual VisualStateGroup collection.
处理此问题的正确方法是侦听 VisualStateGroup 上的 CurrentStateChanged 事件,但根据我的经验,它充其量是不可靠的,最坏的情况是损坏。
第二种选择是将已完成事件挂接到故事板上,但此选项有其自身的陷阱。在某些情况下,视觉状态管理器会在内部生成动画,因此您设置的 Completed 事件将不会被调用。
The correct way of handling this issue would be listening to CurrentStateChanged event on VisualStateGroup, but from my experience it is not reliable at best and broken at worst.
Second option is to hook Completed event on your Storyboard, but this option got pitfalls of its own. In some cases visual state manager generates animations internally, so Completed event you set will not get called.