动画旋转PathGeometry

时间:2017-04-16 18:22:36

标签: wpf xaml animation rotation pathgeometry

我有一个路径有三个定义的PathGeometries:一个圆圈,一条连接线和一个代表风扇叶片的路径。我想使用路径的Tag属性来触发旋转风扇叶片几何体的动画。由于我需要多次重复使用,如果可能的话,我还想以单一的方式包含路径和故事板。

到目前为止,我已经构建了路径,创建了一个故事板,在PathGeometry上创建了一个我想要旋转的旋转变换,并创建了必要的触发器。

我无法弄清楚为什么以下不起作用:

 <Style x:Key="fanPath" TargetType="{x:Type Path}">
        <Setter Property="Stroke" Value="Black"/>
        <Setter Property="StrokeThickness" Value="1"/>
        <Setter Property="Data">
            <Setter.Value>
                <GeometryGroup>
                    <PathGeometry>
                        <PathFigure StartPoint="15,30" IsFilled="False">
                            <LineSegment Point="15,50"/>
                        </PathFigure>
                    </PathGeometry>
                    <EllipseGeometry Center="15,15" RadiusX="15" RadiusY="15"/>
                    <!-- Want to rotate the following -->
                    <PathGeometry>
                        <PathGeometry.Transform>
                            <RotateTransform x:Name="rotate" CenterX="15" CenterY="15"/>
                        </PathGeometry.Transform>
                        <PathFigure StartPoint="10,5" IsClosed="True">
                            <LineSegment Point="20,5"/>
                            <LineSegment Point="10,25"/>
                            <LineSegment Point="20,25"/>
                        </PathFigure>
                        <PathFigure StartPoint="5,10" IsClosed="True">
                            <LineSegment Point="5,20"/>
                            <LineSegment Point="25,10"/>
                            <LineSegment Point="25,20"/>
                        </PathFigure>
                    </PathGeometry>
                </GeometryGroup>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Tag" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard Name="fanRotate">
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="rotate.Angle" From="0"
                                 To="90" RepeatBehavior="Forever"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <StopStoryboard BeginStoryboardName="fanRotate"/>
                </Trigger.ExitActions>
            </Trigger>
        </Style.Triggers>
    </Style>

我已经检查过我的Tag属性设置正确,我已经检查过手动更改旋转变换的Angle属性是否按预期工作。我相信我的问题在于将Storyboard.TargetProperty属性链接到正确的位置(rotate.Angle),但我无法弄清楚我遇到的核心问题。

1 个答案:

答案 0 :(得分:0)

您需要通过以下路径访问Angle媒体资源:

Storyboard.TargetProperty="(Path.Data).(GeometryGroup.Children)[2].(PathGeometry.Transform).Angle"

这可以满足您的需求:

enter image description here

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="fanPath" TargetType="{x:Type Path}">
            <Setter Property="Stroke" Value="Black"/>
            <Setter Property="StrokeThickness" Value="1"/>
            <Setter Property="Data">
                <Setter.Value>
                <GeometryGroup>
                    <PathGeometry>
                        <PathFigure StartPoint="15,30" IsFilled="False">
                            <LineSegment Point="15,50"/>
                        </PathFigure>
                    </PathGeometry>
                    <EllipseGeometry Center="15,15" RadiusX="15" RadiusY="15"/>
                    <!-- Want to rotate the following -->
                    <PathGeometry>
                        <PathGeometry.Transform>
                            <RotateTransform x:Name="rotate" CenterX="15" CenterY="15"/>
                        </PathGeometry.Transform>
                        <PathFigure StartPoint="10,5" IsClosed="True">
                            <LineSegment Point="20,5"/>
                            <LineSegment Point="10,25"/>
                            <LineSegment Point="20,25"/>
                        </PathFigure>
                        <PathFigure StartPoint="5,10" IsClosed="True">
                            <LineSegment Point="5,20"/>
                            <LineSegment Point="25,10"/>
                            <LineSegment Point="25,20"/>
                        </PathFigure>
                    </PathGeometry>
                </GeometryGroup>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Tag" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard Name="fanRotate">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="(Path.Data).(GeometryGroup.Children)[2].(PathGeometry.Transform).Angle" From="0"
                                 To="90" RepeatBehavior="Forever"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <StopStoryboard BeginStoryboardName="fanRotate"/>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Path x:Name="paththing" Width="300" Height="300" Fill="Aqua" Stretch="Fill" Style="{StaticResource fanPath}"/>
        <Button Grid.Row="1" x:Name="button" Content="Go" VerticalAlignment="Bottom">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="paththing" Storyboard.TargetProperty="Tag">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="True"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
        </Button>
    </Grid>
</Window>