-1

我已经创建了如下所示的立方体并使用滚动来旋转它。我需要在使用计时器单击按钮时以编程方式旋转它。我该怎么做?

<DockPanel Margin="0" >


        <ScrollBar Name="hscroll" 
          DockPanel.Dock="Bottom"
          Orientation="Horizontal" 
          Minimum="-180" Maximum="180" 
          LargeChange="10" SmallChange="1" Value="0" />
        <ScrollBar Name="vscroll" 
          DockPanel.Dock="Right"
          Orientation="Vertical"
          Minimum="-180" Maximum="180" 
          LargeChange="10" SmallChange="1" Value="0" />

        <!--
           - The dockOuter control prevents the rendering
           - from including an implicit margin around dockCube
          -->
        <DockPanel Margin="0" Name="dockOuter" Background="White">
            <DockPanel Margin="0" Name="dockCube" Background="White">
                <Viewport3D Margin="0" Name="viewCube">
                    <ModelVisual3D>
                        <ModelVisual3D.Content>
                            <Model3DGroup>
                                <!-- Lights -->
                                <AmbientLight Color="Gray" />
                                <DirectionalLight Color="Gray" Direction="1,-2,-3" />
                                <DirectionalLight Color="Gray" Direction="-1,2,3" />

                                <!-- Top -->
                                <GeometryModel3D>
                                    <GeometryModel3D.Geometry>
                                        <MeshGeometry3D
                                            Positions = "-1,1,1 1,1,1 1,1,-1 -1,1,-1"
                                            TriangleIndices = "0 1 2     2,3,0"
                                            TextureCoordinates="0,1 1,1 1,0 0,0"
                                        />
                                    </GeometryModel3D.Geometry>
                                    <GeometryModel3D.Material>
                                        <DiffuseMaterial>
                                            <DiffuseMaterial.Brush>
                                                <ImageBrush ImageSource="Top.png"/>
                                            </DiffuseMaterial.Brush>
                                        </DiffuseMaterial>
                                    </GeometryModel3D.Material>
                                </GeometryModel3D>

                                <!-- Front -->
                                <GeometryModel3D>
                                    <GeometryModel3D.Geometry>
                                        <MeshGeometry3D
                                            Positions = "-1,-1,1 1,-1,1 1,1,1 -1,1,1"
                                            TriangleIndices = "0 1 2     2,3,0"
                                            TextureCoordinates="0,1 1,1 1,0 0,0"
                                        />
                                    </GeometryModel3D.Geometry>
                                    <GeometryModel3D.Material>
                                        <DiffuseMaterial>
                                            <DiffuseMaterial.Brush>
                                                <ImageBrush ImageSource="Front.png"/>
                                            </DiffuseMaterial.Brush>
                                        </DiffuseMaterial>
                                    </GeometryModel3D.Material>
                                </GeometryModel3D>

                                <!-- Right -->
                                <GeometryModel3D>
                                    <GeometryModel3D.Geometry>
                                        <MeshGeometry3D
                                            Positions = "1,-1,1 1,-1,-1 1,1,-1 1,1,1"
                                            TriangleIndices = "0 1 2     2,3,0"
                                            TextureCoordinates="0,1 1,1 1,0 0,0"
                                        />
                                    </GeometryModel3D.Geometry>
                                    <GeometryModel3D.Material>
                                        <DiffuseMaterial>
                                            <DiffuseMaterial.Brush>
                                                <ImageBrush ImageSource="Right.png"/>
                                            </DiffuseMaterial.Brush>
                                        </DiffuseMaterial>
                                    </GeometryModel3D.Material>
                                </GeometryModel3D>

                                <!-- Left -->
                                <GeometryModel3D>
                                    <GeometryModel3D.Geometry>
                                        <MeshGeometry3D
                                            Positions = "-1,-1,-1 -1,-1,1 -1,1,1 -1,1,-1"
                                            TriangleIndices = "0 1 2     2,3,0"
                                            TextureCoordinates="0,1 1,1 1,0 0,0"
                                        />
                                    </GeometryModel3D.Geometry>
                                    <GeometryModel3D.Material>
                                        <DiffuseMaterial>
                                            <DiffuseMaterial.Brush>
                                                <ImageBrush ImageSource="Left.png"/>
                                            </DiffuseMaterial.Brush>
                                        </DiffuseMaterial>
                                    </GeometryModel3D.Material>
                                </GeometryModel3D>

                                <!-- Back -->
                                <GeometryModel3D>
                                    <GeometryModel3D.Geometry>
                                        <MeshGeometry3D
                                            Positions = "1,-1,-1 -1,-1,-1 -1,1,-1 1,1,-1"
                                            TriangleIndices = "0 1 2     2,3,0"
                                            TextureCoordinates="0,1 1,1 1,0 0,0"
                                        />
                                    </GeometryModel3D.Geometry>
                                    <GeometryModel3D.Material>
                                        <DiffuseMaterial>
                                            <DiffuseMaterial.Brush>
                                                <ImageBrush ImageSource="Back.png"/>
                                            </DiffuseMaterial.Brush>
                                        </DiffuseMaterial>
                                    </GeometryModel3D.Material>
                                </GeometryModel3D>

                                <!-- Bottom -->
                                <GeometryModel3D>
                                    <GeometryModel3D.Geometry>
                                        <MeshGeometry3D
                                            Positions = "-1,-1,-1 1,-1,-1 1,-1,1 -1,-1,1"
                                            TriangleIndices = "0 1 2     2,3,0"
                                            TextureCoordinates="0,1 1,1 1,0 0,0"
                                        />
                                    </GeometryModel3D.Geometry>
                                    <GeometryModel3D.Material>
                                        <DiffuseMaterial>
                                            <DiffuseMaterial.Brush>
                                                <ImageBrush ImageSource="Bottom.png"/>
                                            </DiffuseMaterial.Brush>
                                        </DiffuseMaterial>
                                    </GeometryModel3D.Material>
                                </GeometryModel3D>

                            </Model3DGroup>
                        </ModelVisual3D.Content>
                    </ModelVisual3D>

                    <Viewport3D.Camera>
                        <PerspectiveCamera
                          Position = "3, 3, 3"
                          LookDirection = "-1, -1, -1"
                          UpDirection = "0, 1, 0"
                          FieldOfView = "60">
                            <PerspectiveCamera.Transform>
                                <Transform3DGroup>
                                    <RotateTransform3D>
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D
                                              Axis="0 1 0" 
                                              Angle="{Binding ElementName=hscroll, Path=Value}" />
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                    <RotateTransform3D>
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D
                                              Axis="1 0 0" 
                                              Angle="{Binding ElementName=vscroll, Path=Value}" />
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                </Transform3DGroup>
                            </PerspectiveCamera.Transform>
                        </PerspectiveCamera>
                    </Viewport3D.Camera>
                </Viewport3D>
            </DockPanel>
        </DockPanel>
    </DockPanel>
4

1 回答 1

1

由于您以非平凡的方式使用 WPF,我将假设您熟悉(并使用)MVVM模式。如果没有,请查看它,这是管理 WPF 应用程序的最佳方式。

目前,您的相机旋转绑定到滚动条值。所以为了挑剔,你的立方体保持不变,但你的相机围绕它旋转,让它看起来好像你的立方体在旋转。要从代码中影响这一点,首先在视图模型中创建两个属性并将滚动条值绑定到这些属性。然后将您的旋转绑定到这些值,而不是直接绑定到滚动条。到目前为止没有任何改变,当你测试它时,你的应用程序应该和以前一样工作。但是现在您的程序中有两个可以通过代码设置的变量。例如,在视图模型中的Timer实例的 Tick 事件上增加它们。

请注意,WPF 通常将StoryBoard用于动画,无需 C# 代码即可在 XAML 中使用。

于 2013-02-24T10:22:19.687 回答