WPF矩形在DoubleAnimation StoryBoard中可见时触发



我有一个在WPF中淡入/淡出矩形的DoubleAnimation

<Canvas>
    <Rectangle Height="150" Width="150">
        <Rectangle.Fill>
            <SolidColorBrush x:Name="OpacityBrush" Color="DarkBlue" />
        </Rectangle.Fill>
        <Rectangle.Triggers>
            <EventTrigger RoutedEvent="Rectangle.Loaded">
                <BeginStoryboard>
                    <StoryBoard>
                        <DoubleAnimation Storyboard.TargetName="OpacityBrush" Storyboard.TargetProperty="Opacity" From="0.0" To="0.6" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />
                        <DoubleAnimation Storyboard.TargetName="OpacityBrush" Storyboard.TargetProperty="Opacity" From="0.6" To="0.0" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />
                    </StoryBoard>
                </BeginStoryboard>
            </EventTrigger>
        </Rectangle.Triggers>
    </Rectangle>
</Canvas>

这很好,但我希望这只发生在矩形是可见的。目前,当它加载时,它会在后台动画(我假设)。

我怎么能改变它,使它会开始动画时,它是可见的和停止时,它是隐藏/折叠?

还是无所谓?我只是担心它会占用(动画)资源,因为应用程序中有很多矩形,而且大多数时候它们是隐藏的。

谢谢。

试试这个

<<p> 1方法/strong>
<Canvas>
    <Rectangle Height="150"  Width="150">
        <Rectangle.Fill>
            <SolidColorBrush x:Name="OpacityBrush" Color="DarkBlue" />
        </Rectangle.Fill>
        <Rectangle.Triggers>
            <EventTrigger RoutedEvent="Rectangle.Loaded">
                <BeginStoryboard>
                    <Storyboard Name="Anm">
                        <DoubleAnimation Storyboard.TargetName="OpacityBrush" Storyboard.TargetProperty="Opacity" From="0.0" To="0.6" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />
                        <DoubleAnimation Storyboard.TargetName="OpacityBrush" Storyboard.TargetProperty="Opacity" From="0.6" To="0.0" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Rectangle.Triggers>
        <Rectangle.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Source={RelativeSource Self}, Path=Visibility}" Value="{x:Static Visibility.Collapsed}">
                        <DataTrigger.EnterActions>
                            <StopStoryboard BeginStoryboardName="Anm"/>
                        </DataTrigger.EnterActions>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Rectangle.Style>
    </Rectangle>
</Canvas>
<<p> 第二方法/strong>
 <Canvas>
    <Rectangle Height="150"  Width="150">
        <Rectangle.Fill>
            <SolidColorBrush x:Name="OpacityBrush" Color="DarkBlue" />
        </Rectangle.Fill>
        <Rectangle.Style>
            <Style TargetType="Rectangle">
                <Style.Triggers>
                    <Trigger Property="Visibility" Value="Visible">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard Name="Anm">
                                    <DoubleAnimation  Storyboard.TargetProperty="Opacity" From="0.0" To="0.6" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />
                                    <DoubleAnimation  Storyboard.TargetProperty="Opacity" From="0.6" To="0.0" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <StopStoryboard BeginStoryboardName="Anm"></StopStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Rectangle.Style>
    </Rectangle>
</Canvas>

相关内容

  • 没有找到相关文章

最新更新