简体   繁体   English

WPF中UserControl上的不透明度动画

[英]Opacity animation on a UserControl in WPF

I have a WPF application with an animation on one of my views. 我有一个WPF应用程序,其中一个视图上有动画。 The animation repeats indefinitely and is pretty simple - just three arrows that fade from 100% to 0% opacity using a storyboard. 动画可以无限重复,而且非常简单-使用情节提要,只有三个箭头可以从100%变为0%不透明度。

I initially had each arrow as an image, but I redrew the image using vector graphics saved as XAML (Paths). 最初,每个箭头都是图像,但是我使用保存为XAML(路径)的矢量图形重新绘制了图像。

I replaced the image tags with the new UserControl, like so: 我将图像标签替换为新的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>

The animation worked before, but now it's not and I'm not sure why. 动画以前曾起作用,但现在不起作用了,我不确定为什么。 I figured opacity worked the same for images and UserControls, but perhaps I'm missing something. 我发现不透明性对于图像和UserControls同样有效,但是也许我缺少了一些东西。 I've already searched for a solution here and Google to no avail. 我已经在这里搜索了解决方案,但Google都没有结果。

Here's the UserControl XAML: 这是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>

提供的代码仅通过从DoubleAnimation标记中删除Storyboard.TargetName =“ arrow1”(箭头2和arrow3)而起作用

Instead of managing each instance, if it is supposed to pulse all of the time and it is not interacted with, move the storyboard to the user control. 如果应该一直将其实例化并且不与之交互,则可以将情节提要板移至用户控件,而不是管理每个实例。 Note: I had to use a different color, as yours was not included in your xaml. 注意:我必须使用其他颜色,因为您的xaml中没有包含您的颜色。

<UserControl x:Class="WpfApplication13.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" >
    <UserControl.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard>
                <Storyboard RepeatBehavior="Forever">
                    <DoubleAnimation Storyboard.TargetName="path4150" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:1" BeginTime="0:0:0" />
                    <DoubleAnimation Storyboard.TargetName="path4150" Storyboard.TargetProperty="Opacity" From="0" To="0" Duration="0:0:0.6" BeginTime="0:0:1" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </UserControl.Triggers>
    <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/>
            <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="Blue" StrokeThickness="1" Stroke="Blue" 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>

Then your usage just looks something like this: 然后您的用法看起来像这样:

<control:Arrow x:Name="arrow1" Grid.Column="0" Grid.Row="0" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM