如何同步在WPF用户控件各地动画 [英] How to synchronize animations across usercontrols in WPF

查看:173
本文介绍了如何同步在WPF用户控件各地动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了画布面板几个用户控件。我有一个绑定到用户控件视图模型,我有触发在视图模型的属性的动画。从红到透明的非常简单的切换颜色来模拟闪烁。

 <情节提要X:键=alertAnimation的repeatBehavior =永远的自动翻转=真>
    < ColorAnimationUsingKeyFrames Storyboard.TargetProperty =。(Border.BorderBrush)(SolidColorBrush.Color)
                                  Storyboard.TargetName =TileBorder
                                  的BeginTime =00:00:00
                                  的repeatBehavior =永远
                                  自动翻转=真
                                  >
        < D​​iscreteColorKeyFrame值=红楼梦/>
        < D​​iscreteColorKeyFrame KeyTime =00:00:00.500VALUE =透明/>
    < / ColorAnimationUsingKeyFrames>< /故事板>

这工作得很好。但是,由于我异步加载多个用户控件闪烁的动画不同步,使他们在不同的时间都闪烁。现在有任何东西的屏幕需要所有眨眼以相同的速度/时间闪烁的要求。有没有一种方法来同步这些动画?我似乎无法找到任何地方的例子,适合什么,我试图完成。有没有办法使用的ParallelTimeline,所有的动画从一个单一的控制器添加到它和启动/停止他们的方法吗?任何示例如何做到这一点呢?

编辑4/20
它会更好有一个style.xaml文件中定义的动画,并有一个全球性故事板,每个控制增加了它的闪烁动画,并有主UI开始故事板?


解决方案

我找不到控制在多个用户控件,所以我最终没有使用动画中的用户控件,而是使用动画来简单地改变多个动画的好方法在的DependencyProperty一个静态的对象,有需要以某种方式绑定到该属性闪烁所有用户控件。有多种方法可以产生理想的效果,我想,但最终我现在为我的所有控件知道什么时候关闭,因此一切都同步闪烁了,然后的方式。

我从以下站点的想法和样品code:
<一href=\"http://dotnetlearning.word$p$pss.com/2009/10/05/synchronizing-animations-across-different-threads/\"相对=nofollow>如何同步跨越用户控件在WPF

我做了一些修改,以使其但总体来说做了什么,我需要做的事。

I am creating several usercontrols in a wrap panel. I have a view model bound to the usercontrol and I have an animation triggered to a property in the view model. Very simple toggle color from red to transparent to simulate flashing.

<Storyboard x:Key="alertAnimation" RepeatBehavior="Forever" AutoReverse="True" >
    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
                                  Storyboard.TargetName="TileBorder"
                                  BeginTime="00:00:00"
                                  RepeatBehavior="Forever"
                                  AutoReverse="True"
                                  >
        <DiscreteColorKeyFrame Value="Red"/>
        <DiscreteColorKeyFrame KeyTime="00:00:00.500" Value="Transparent" />
    </ColorAnimationUsingKeyFrames>

</Storyboard>

This works just fine. However, since I load multiple usercontrols asynchronously the blinking animations are not synchronized so they all blink at different times. There is now a requirement that anything blinking on the screen needs to all blink at the same rate/time. Is there a way to sync these animations? I cannot seem to find an example anywhere that fits what I am trying to accomplish. Is there a way to use the ParallelTimeline, add all the animations to it and start/stop them from a single controller? Any examples how to accomplish this out there?

EDIT 4/20 Would it be better to have the animation defined in a style.xaml file and have a "global" storyboard that each controls adds its "blink" animation to and have the main UI start the storyboard?

解决方案

I could not find a good way to control multiple animations across multiple usercontrols so I ended up not using animation in the usercontrol but instead use an animation to simply change a dependencyproperty in a static object and have all usercontrols that need to blink in some fashion bind to that property. There are various ways to produce the desired results I wanted but ultimately I now had a way for all my controls to know when blink was on and then off so that everything is synchronized.

I got the idea and sample code from the following site: How to synchronize animations across usercontrols in wpf

I made a few modifications to it but overall it did exactly what I needed it to do.

这篇关于如何同步在WPF用户控件各地动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆