当控件可见时始终运行的 WPF 动画

Vac*_*ano 3 wpf animation

我有一个(蹩脚的)用户要求使控件超级可见。

遗憾的是,这意味着背景会闪烁(Ug)。

Border因此,持有 a的控件TextBlock仅在相当罕见的情况下可见。

我看过一些动画示例,它们都有一个“触发器”。最常见的是当用户单击某些内容时。

有没有办法让动画一直运行(当然如果控件可见)?

pus*_*raj 5

就这样,RepeatBehavior="Forever"将保持动画运行直到停止或删除

您可以在控制负载上启用自动反转来触发彩色动画并让它永远运行

<Border Background="Transparent">
    <TextBlock Text="some text" />
    <Border.Triggers>
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation To="SkyBlue"
                                    Storyboard.TargetProperty="Background.Color"
                                    RepeatBehavior="Forever"
                                    AutoReverse="True" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Border.Triggers>
</Border>
Run Code Online (Sandbox Code Playgroud)

如果您需要在可见性变化时触发动画,那么这里有一种方法,请注意,当 IsVisible 属性变为 true 时应用动画,当它变为 false 时停止。

<Border Background="Transparent">
    <TextBlock Text="some text" />
    <Border.Style>
        <Style TargetType="Border">
            <Style.Triggers>
                <Trigger Property="IsVisible"
                         Value="true">
                    <Trigger.EnterActions>
                        <BeginStoryboard x:Name="startFlashing">
                            <Storyboard>
                                <ColorAnimation To="SkyBlue"
                                                Storyboard.TargetProperty="Background.Color"
                                                RepeatBehavior="Forever"
                                                AutoReverse="True" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <StopStoryboard BeginStoryboardName="startFlashing" />
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>
Run Code Online (Sandbox Code Playgroud)

通常,在可见性设置为 false 后,如果动画仍在运行或停止,则没有明显的差异。