WPF中UserControl上的不透明度动画

时间:2015-08-26 17:09:54

标签: c# wpf xaml storyboard

我有一个WPF应用程序,其中一个视图上有动画。动画无限重复并且很简单 - 只有三个箭头使用故事板从100%渐变到0%不透明度。

我最初将每个箭头都作为图像,但我使用保存为XAML(路径)的矢量图形重新绘制图像。

我用新的UserControl替换了图像标签,如下所示:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <control:Arrow x:Name="arrow1" Grid.Column="0" Grid.Row="0">
        <UserControl.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                <BeginStoryboard>
                    <Storyboard RepeatBehavior="Forever">
                        <DoubleAnimation Storyboard.TargetName="arrow1" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:1" BeginTime="0:0:0" />
                        <DoubleAnimation Storyboard.TargetName="arrow1" Storyboard.TargetProperty="Opacity" From="0" To="0" Duration="0:0:0.6" BeginTime="0:0:1" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </UserControl.Triggers>
    </control:Arrow>
    <control:Arrow x:Name="arrow2" Grid.Column="0" Grid.Row="1">
        <control:Arrow.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                <BeginStoryboard>
                    <Storyboard RepeatBehavior="Forever">
                        <DoubleAnimation Storyboard.TargetName="arrow2" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:1" BeginTime="0:0:0.2" />
                        <DoubleAnimation Storyboard.TargetName="arrow2" Storyboard.TargetProperty="Opacity" From="0" To="0" Duration="0:0:0.4" BeginTime="0:0:1.2" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </control:Arrow.Triggers>
    </control:Arrow>
    <control:Arrow x:Name="arrow3" Grid.Column="0" Grid.Row="2">
        <control:Arrow.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                <BeginStoryboard>
                    <Storyboard RepeatBehavior="Forever">
                        <DoubleAnimation Storyboard.TargetName="arrow3" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:1" BeginTime="0:0:0.4" />
                        <DoubleAnimation Storyboard.TargetName="arrow3" Storyboard.TargetProperty="Opacity" From="0" To="0" Duration="0:0:0.2" BeginTime="0:0:1.4" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </control:Arrow.Triggers>
    </control:Arrow>
</Grid>

动画之前有效,但现在不行,我不知道为什么。我认为不透明度对于图像和UserControls的工作方式相同,但也许我错过了一些东西。我已经在这里搜索了一个解决方案,谷歌无济于事。

这是UserControl XAML:

<UserControl x:Class="KioskPractice.UIElements.Arrow"
             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:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             >
    <Grid>
        <Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Name="svg4163" Width="645.99999" Height="146.19125">
            <Canvas.RenderTransform>
                <TranslateTransform X="0" Y="0"/>
            </Canvas.RenderTransform>
            <Canvas.Resources/>
            <!--Unknown tag: sodipodi:namedview-->
            <!--Unknown tag: metadata-->
            <Canvas Name="layer1">
                <Canvas.RenderTransform>
                    <TranslateTransform X="-34.142857" Y="-73.552293"/>
                </Canvas.RenderTransform>
                <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path4150" Fill="{StaticResource AirlineChoiceBlue}" StrokeThickness="1" Stroke="{StaticResource AirlineChoiceBlue}" StrokeLineJoin="Miter" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" Data="m 679.64285 74.222299 0 50.000001 -320 95 -324.999993 -95 0 -50.000001 324.999993 95.000001 z"/>
            </Canvas>
        </Canvas>
    </Grid>
</UserControl>

1 个答案:

答案 0 :(得分:2)

提供的代码仅通过从DoubleAnimation标记中删除Storyboard.TargetName =“arrow1”(arrow2和arrow3)来工作