WPF - 关键帧动画不起作用

发布于 2024-08-14 15:23:13 字数 3679 浏览 3 评论 0原文

我的 Window.Resources 中有一个关键帧动画故事板和一个单独的旋转转换。

旋转变换起作用,因为我可以改变角度并看到内容旋转。我知道故事板正在被调用,因为在单击按钮后我花了几次时间才获得 PropertyPath。

然而现在它什么也没做——没有错误,但也没有旋转!

有人可以帮忙吗?

谢谢,

安迪

<Window
        x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="400">
    <Window.Resources>

        <Storyboard x:Key="myStoryboard">
            <Rotation3DAnimationUsingKeyFrames
                       Storyboard.Target="{Binding TemplatedParent}"     
                       Storyboard.TargetProperty="(Viewport2DVisual3D.Transform).(RotateTransform3D.Rotation)" >
                <Rotation3DAnimationUsingKeyFrames.KeyFrames>
                    <LinearRotation3DKeyFrame KeyTime="0:0:1">
                        <LinearRotation3DKeyFrame.Value>
                            <AxisAngleRotation3D Axis="0,1,0" Angle="0" />
                        </LinearRotation3DKeyFrame.Value>
                    </LinearRotation3DKeyFrame>
                </Rotation3DAnimationUsingKeyFrames.KeyFrames>
            </Rotation3DAnimationUsingKeyFrames>
        </Storyboard>

        <RotateTransform3D x:Key="myRotateTransform3D" >
            <RotateTransform3D.Rotation>
                <AxisAngleRotation3D Axis="0,1,0" Angle="30" />
            </RotateTransform3D.Rotation>
        </RotateTransform3D>

        <!-- Front, left square -->
        <MeshGeometry3D
                    x:Key="squareMeshFront"
                    Positions="-1,-1,1 1,-1,1 1,1,1 -1,1,1"
                    TriangleIndices="0 1 2 0 2 3"
                    TextureCoordinates="0,1 1,1 1,0 0,0" />
        <!-- Bottom -->
        <MeshGeometry3D
                    x:Key="squareMeshBottom"
                    Positions="-1,-1,1 1,-1,1 1,-1,-1 1,1,-1"
                    TriangleIndices="0 1 2 0 2 3"
                    TextureCoordinates="0,1 1,1 1,0 0,0" />


        <DiffuseMaterial x:Key="visualHostMaterial" Brush="White" Viewport2DVisual3D.IsVisualHostMaterial="True" />
    </Window.Resources>

    <Viewport3D>


        <Viewport3D.Camera>
            <PerspectiveCamera Position="0,0,9" LookDirection="0,0,-1" />
        </Viewport3D.Camera>

        <Viewport2DVisual3D Material="{StaticResource visualHostMaterial}" Geometry="{StaticResource squareMeshFront}" Transform="{StaticResource myRotateTransform3D}" >

            <StackPanel Background="Blue" Width="120" Height="80">
                <Button Height="30" Margin="20">
                    <Button.Content>Click Me</Button.Content>
                    <Button.Triggers>
                        <EventTrigger RoutedEvent="Button.Click">
                            <EventTrigger.Actions>
                                <BeginStoryboard Storyboard="{StaticResource myStoryboard}" >
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                    </Button.Triggers>
                </Button>
            </StackPanel>
        </Viewport2DVisual3D>

        <ModelVisual3D>
            <ModelVisual3D.Content>
                <AmbientLight Color="White" />
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>
</Window>

I've got a KeyFrame Animation storyboard and a separate rotation transformation in my Window.Resources.

The rotation transformation works as I can alter the angle and see the content rotate. I know the storyboard is being called, because it took me a few goes to get the PropertyPath right after I clicked on the button.

However now it does nothing - no error, but no rotation either!

Can anyone help please?

Thanks,

Andy

<Window
        x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="400">
    <Window.Resources>

        <Storyboard x:Key="myStoryboard">
            <Rotation3DAnimationUsingKeyFrames
                       Storyboard.Target="{Binding TemplatedParent}"     
                       Storyboard.TargetProperty="(Viewport2DVisual3D.Transform).(RotateTransform3D.Rotation)" >
                <Rotation3DAnimationUsingKeyFrames.KeyFrames>
                    <LinearRotation3DKeyFrame KeyTime="0:0:1">
                        <LinearRotation3DKeyFrame.Value>
                            <AxisAngleRotation3D Axis="0,1,0" Angle="0" />
                        </LinearRotation3DKeyFrame.Value>
                    </LinearRotation3DKeyFrame>
                </Rotation3DAnimationUsingKeyFrames.KeyFrames>
            </Rotation3DAnimationUsingKeyFrames>
        </Storyboard>

        <RotateTransform3D x:Key="myRotateTransform3D" >
            <RotateTransform3D.Rotation>
                <AxisAngleRotation3D Axis="0,1,0" Angle="30" />
            </RotateTransform3D.Rotation>
        </RotateTransform3D>

        <!-- Front, left square -->
        <MeshGeometry3D
                    x:Key="squareMeshFront"
                    Positions="-1,-1,1 1,-1,1 1,1,1 -1,1,1"
                    TriangleIndices="0 1 2 0 2 3"
                    TextureCoordinates="0,1 1,1 1,0 0,0" />
        <!-- Bottom -->
        <MeshGeometry3D
                    x:Key="squareMeshBottom"
                    Positions="-1,-1,1 1,-1,1 1,-1,-1 1,1,-1"
                    TriangleIndices="0 1 2 0 2 3"
                    TextureCoordinates="0,1 1,1 1,0 0,0" />


        <DiffuseMaterial x:Key="visualHostMaterial" Brush="White" Viewport2DVisual3D.IsVisualHostMaterial="True" />
    </Window.Resources>

    <Viewport3D>


        <Viewport3D.Camera>
            <PerspectiveCamera Position="0,0,9" LookDirection="0,0,-1" />
        </Viewport3D.Camera>

        <Viewport2DVisual3D Material="{StaticResource visualHostMaterial}" Geometry="{StaticResource squareMeshFront}" Transform="{StaticResource myRotateTransform3D}" >

            <StackPanel Background="Blue" Width="120" Height="80">
                <Button Height="30" Margin="20">
                    <Button.Content>Click Me</Button.Content>
                    <Button.Triggers>
                        <EventTrigger RoutedEvent="Button.Click">
                            <EventTrigger.Actions>
                                <BeginStoryboard Storyboard="{StaticResource myStoryboard}" >
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                    </Button.Triggers>
                </Button>
            </StackPanel>
        </Viewport2DVisual3D>

        <ModelVisual3D>
            <ModelVisual3D.Content>
                <AmbientLight Color="White" />
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>
</Window>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉酒的小男人 2024-08-21 15:23:13

问题出在 Storyboard.Target="{Binding TemplatedParent}" 中。为 Viewport2DVisual3D 控件命名:,并设置 Storyboard.TargetName="vp" 而不是什么你有...

干杯

The problem is in Storyboard.Target="{Binding TemplatedParent}". Give a name to Viewport2DVisual3D control: <Viewport2DVisual3D x:Name="vp" .../>, and set Storyboard.TargetName="vp" instead of what you have...

Cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文