我正在阅读MSDN Animation教程,它描述了将 Storyboard 应用于元素的以下步骤:

  • 创建 Storyboard ;
  • 使用TargetName属性指定其目标元素名称;
  • (指定目标属性);
  • (添加事件触发器以启动动画);

  • 我看到一个概念性问题,从中得出我的困难,就是这样:



    我打算用的用例是模仿一个led面板(一个椭圆形的堆栈面板),其中每个led可以处于以下四种逻辑状态之一:开,关,快速闪烁和缓慢闪烁(非常类似于以太网路由器)。然后,我将创建动画BlinkingSlowBlinkingFast,然后在ViewModel进入各自的逻辑状态时触发。然后,我可以关心一下ViewModel中的行为,并通过适当触发和重用一些StaticResource Storyboard来让View照顾好自己。
    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:System="clr-namespace:System;assembly=mscorlib"
            xmlns:local="clr-namespace:blinking"
            x:Class="blinking.MainWindow"
            Title="MainWindow"
            Background="{x:Null}"
            WindowStartupLocation="CenterScreen">
    
        <Window.Resources>
            <System:Double x:Key="Diameter">40</System:Double>
            <Color x:Key="RedOn">Red</Color>
            <Color x:Key="RedOff">#FF570000</Color>
            <Storyboard x:Key="BlinkSlow" RepeatBehavior="Forever">
                <ColorAnimationUsingKeyFrames
                        Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                        Storyboard.TargetName="led4"
                        AutoReverse="True"
                        RepeatBehavior="Forever">
                    <DiscreteColorKeyFrame KeyTime="0" Value="{StaticResource RedOn}"/>
                    <DiscreteColorKeyFrame KeyTime="0:0:0.5" Value="{StaticResource RedOn}"/>
                    <EasingColorKeyFrame KeyTime="0:0:0.5" Value="{StaticResource RedOff}"/>
                    <DiscreteColorKeyFrame KeyTime="0:0:1" Value="{StaticResource RedOff}"/>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </Window.Resources>
    
        <Window.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                <BeginStoryboard Storyboard="{StaticResource BlinkSlow}"/>
            </EventTrigger>
        </Window.Triggers>
    
        <StackPanel x:Name="leds_container" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,20,4,0">
            <Ellipse x:Name="led1" Width="{StaticResource Diameter}" Height="{StaticResource Diameter}" Fill="#FFF90F0F" Margin="20,0,0,0"/>
            <Ellipse x:Name="led2" Width="{StaticResource Diameter}" Height="{StaticResource Diameter}" Fill="#FFF90F0F" Margin="20,0,0,0"/>
            <Ellipse x:Name="led3" Width="{StaticResource Diameter}" Height="{StaticResource Diameter}" Fill="#FFF90F0F" Margin="20,0,0,0"/>
            <Ellipse x:Name="led4" Width="{StaticResource Diameter}" Height="{StaticResource Diameter}" Fill="#FFF90F0F" Margin="20,0,0,0"/>
        </StackPanel>
    </Window>
    

    有什么建议吗?

    最佳答案

    您可以为此使用样式触发。

    像已经在资源部分中创建 Storyboard 一样,但是没有目标名称。

    然后,为椭圆创建一个样式,其中包括一个DataTrigger,以启动当前状态所需的动画。

    例如:

    <Window.Resources>
        <!--
        Other declarations
        -->
        <Style TargetType="{x:Type Ellipse}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=State, Mode=OneWay}" Value="BlinkSlow">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource BlinkSlow}" />
                    </DataTrigger.EnterActions>
                </DataTrigger>
                <!--
                Add DataTrigger for your other states too.
                -->
            </Style.Triggers>
        </Style>
    </Window.Resources>
    

    10-07 19:41
    查看更多